diff options
author | U-Zyn Chua <chua@uzyn.com> | 2015-06-28 15:23:18 +0800 |
---|---|---|
committer | U-Zyn Chua <chua@uzyn.com> | 2015-06-28 15:23:18 +0800 |
commit | 57649026fe339aea13ec8d9be110d885c630a7e3 (patch) | |
tree | cfc76c9b7a7e7bda3ad50aa89c837e84d8185adc /test | |
parent | ea3576b6da334710d0da7446064e9acc0766136a (diff) | |
parent | e7948013f3c3741606159e0c86f03dbb9e20ff2b (diff) | |
download | async-57649026fe339aea13ec8d9be110d885c630a7e3.tar.gz |
Merged from master
Diffstat (limited to 'test')
-rwxr-xr-x | test/test-async.js | 221 |
1 files changed, 193 insertions, 28 deletions
diff --git a/test/test-async.js b/test/test-async.js index 2d7269b..5385b63 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -2172,6 +2172,7 @@ exports['selectSeries alias'] = function(test){ }; exports['reject'] = function(test){ + test.expect(1); async.reject([3,1,2], filterIterator, function(results){ test.same(results, [2]); test.done(); @@ -2179,6 +2180,7 @@ exports['reject'] = function(test){ }; exports['reject original untouched'] = function(test){ + test.expect(2); var a = [3,1,2]; async.reject(a, function(x, callback){ callback(x % 2); @@ -2190,6 +2192,7 @@ exports['reject original untouched'] = function(test){ }; exports['rejectSeries'] = function(test){ + test.expect(1); async.rejectSeries([3,1,2], filterIterator, function(results){ test.same(results, [2]); test.done(); @@ -2197,6 +2200,7 @@ exports['rejectSeries'] = function(test){ }; exports['some true'] = function(test){ + test.expect(1); async.some([3,1,2], function(x, callback){ setTimeout(function(){callback(x === 1);}, 0); }, function(result){ @@ -2206,6 +2210,7 @@ exports['some true'] = function(test){ }; exports['some false'] = function(test){ + test.expect(1); async.some([3,1,2], function(x, callback){ setTimeout(function(){callback(x === 10);}, 0); }, function(result){ @@ -2215,6 +2220,7 @@ exports['some false'] = function(test){ }; exports['some early return'] = function(test){ + test.expect(1); var call_order = []; async.some([1,2,3], function(x, callback){ setTimeout(function(){ @@ -2236,6 +2242,7 @@ exports['any alias'] = function(test){ }; exports['every true'] = function(test){ + test.expect(1); async.every([1,2,3], function(x, callback){ setTimeout(function(){callback(true);}, 0); }, function(result){ @@ -2245,6 +2252,7 @@ exports['every true'] = function(test){ }; exports['every false'] = function(test){ + test.expect(1); async.every([1,2,3], function(x, callback){ setTimeout(function(){callback(x % 2);}, 0); }, function(result){ @@ -2254,6 +2262,7 @@ exports['every false'] = function(test){ }; exports['every early return'] = function(test){ + test.expect(1); var call_order = []; async.every([1,2,3], function(x, callback){ setTimeout(function(){ @@ -2275,6 +2284,7 @@ exports['all alias'] = function(test){ }; exports['detect'] = function(test){ + test.expect(2); var call_order = []; async.detect([3,2,1], detectIterator.bind(this, call_order), function(result){ call_order.push('callback'); @@ -2287,6 +2297,7 @@ exports['detect'] = function(test){ }; exports['detect - mulitple matches'] = function(test){ + test.expect(2); var call_order = []; async.detect([3,2,2,1,2], detectIterator.bind(this, call_order), function(result){ call_order.push('callback'); @@ -2299,6 +2310,7 @@ exports['detect - mulitple matches'] = function(test){ }; exports['detectSeries'] = function(test){ + test.expect(2); var call_order = []; async.detectSeries([3,2,1], detectIterator.bind(this, call_order), function(result){ call_order.push('callback'); @@ -2311,6 +2323,7 @@ exports['detectSeries'] = function(test){ }; exports['detectSeries - multiple matches'] = function(test){ + test.expect(2); var call_order = []; async.detectSeries([3,2,2,1,2], detectIterator.bind(this, call_order), function(result){ call_order.push('callback'); @@ -2323,6 +2336,7 @@ exports['detectSeries - multiple matches'] = function(test){ }; exports['detectSeries - ensure stop'] = function (test) { + test.expect(1); async.detectSeries([1, 2, 3, 4, 5], function (num, cb) { if (num > 3) throw new Error("detectSeries did not stop iterating"); cb(num === 3); @@ -2333,6 +2347,8 @@ exports['detectSeries - ensure stop'] = function (test) { }; exports['sortBy'] = function(test){ + test.expect(2); + async.sortBy([{a:1},{a:15},{a:6}], function(x, callback){ setTimeout(function(){callback(null, x.a);}, 0); }, function(err, result){ @@ -2343,6 +2359,8 @@ exports['sortBy'] = function(test){ }; exports['sortBy inverted'] = function(test){ + test.expect(1); + async.sortBy([{a:1},{a:15},{a:6}], function(x, callback){ setTimeout(function(){callback(null, x.a*-1);}, 0); }, function(err, result){ @@ -2400,6 +2418,7 @@ var console_fn_tests = function(name){ }; exports[name + ' with multiple result params'] = function(test){ + test.expect(1); var fn = function(callback){callback(null,'one','two','three');}; var _console_fn = console[name]; var called_with = []; @@ -2433,16 +2452,18 @@ var console_fn_tests = function(name){ exports['times'] = { 'times': function(test) { - async.times(5, function(n, next) { - next(null, n); - }, function(err, results) { - test.ok(err === null, err + " passed instead of 'null'"); - test.same(results, [0,1,2,3,4]); - test.done(); - }); + test.expect(2); + async.times(5, function(n, next) { + next(null, n); + }, function(err, results) { + test.ok(err === null, err + " passed instead of 'null'"); + test.same(results, [0,1,2,3,4]); + test.done(); + }); }, 'times 3': function(test){ + test.expect(1); var args = []; async.times(3, function(n, callback){ setTimeout(function(){ @@ -2479,6 +2500,7 @@ exports['times'] = { }, 'timesSeries': function(test){ + test.expect(2); var call_order = []; async.timesSeries(5, function(n, callback){ setTimeout(function(){ @@ -2503,6 +2525,8 @@ exports['times'] = { }, 'timesLimit': function(test){ + test.expect(7); + var limit = 2; var running = 0; async.timesLimit(5, limit, function (i, next) { @@ -2528,6 +2552,7 @@ console_fn_tests('warn'); console_fn_tests('error');*/ exports['nextTick'] = function(test){ + test.expect(1); var call_order = []; async.nextTick(function(){call_order.push('two');}); call_order.push('one'); @@ -2586,6 +2611,7 @@ exports['noConflict - node only'] = function(test){ }; exports['concat'] = function(test){ + test.expect(3); var call_order = []; var iterator = function (x, cb) { setTimeout(function(){ @@ -2607,6 +2633,7 @@ exports['concat'] = function(test){ }; exports['concat error'] = function(test){ + test.expect(1); var iterator = function (x, cb) { cb(new Error('test error')); }; @@ -2617,6 +2644,7 @@ exports['concat error'] = function(test){ }; exports['concatSeries'] = function(test){ + test.expect(3); var call_order = []; var iterator = function (x, cb) { setTimeout(function(){ @@ -2638,8 +2666,9 @@ exports['concatSeries'] = function(test){ }; exports['until'] = function (test) { - var call_order = []; + test.expect(3); + var call_order = []; var count = 0; async.until( function () { @@ -2668,6 +2697,8 @@ exports['until'] = function (test) { }; exports['doUntil'] = function (test) { + test.expect(3); + var call_order = []; var count = 0; async.doUntil( @@ -2696,6 +2727,8 @@ exports['doUntil'] = function (test) { }; exports['doUntil callback params'] = function (test) { + test.expect(2); + var call_order = []; var count = 0; async.doUntil( @@ -2724,6 +2757,8 @@ exports['doUntil callback params'] = function (test) { }; exports['whilst'] = function (test) { + test.expect(3); + var call_order = []; var count = 0; @@ -2754,6 +2789,7 @@ exports['whilst'] = function (test) { }; exports['doWhilst'] = function (test) { + test.expect(3); var call_order = []; var count = 0; @@ -2783,8 +2819,8 @@ exports['doWhilst'] = function (test) { }; exports['doWhilst callback params'] = function (test) { + test.expect(2); var call_order = []; - var count = 0; async.doWhilst( function (cb) { @@ -2870,9 +2906,24 @@ exports['doDuring'] = function (test) { ); }; +exports['whilst optional callback'] = function (test) { + var counter = 0; + async.whilst( + function () { return counter < 2; }, + function (cb) { + counter++; + cb(); + } + ); + test.equal(counter, 2); + test.done(); +}; + exports['queue'] = { 'queue': function (test) { + test.expect(17); + var call_order = [], delays = [160,80,240,80]; @@ -2928,6 +2979,7 @@ exports['queue'] = { }, 'default concurrency': function (test) { + test.expect(17); var call_order = [], delays = [160,80,240,80]; @@ -2981,6 +3033,7 @@ exports['queue'] = { }, 'zero concurrency': function(test){ + test.expect(1); test.throws(function () { async.queue(function (task, callback) { callback(null, task); @@ -2990,6 +3043,7 @@ exports['queue'] = { }, 'error propagation': function(test){ + test.expect(1); var results = []; var q = async.queue(function (task, callback) { @@ -3022,11 +3076,12 @@ exports['queue'] = { // The original queue implementation allowed the concurrency to be changed only // on the same event loop during which a task was added to the queue. This -// test attempts to be a more rubust test. +// test attempts to be a more robust test. // Start with a concurrency of 1. Wait until a leter event loop and change // the concurrency to 2. Wait again for a later loop then verify the concurrency. // Repeat that one more time by chaning the concurrency to 5. 'changing concurrency': function (test) { + test.expect(3); var q = async.queue(function(task, callback){ setTimeout(function(){ @@ -3056,6 +3111,7 @@ exports['queue'] = { }, 'push without callback': function (test) { + test.expect(1); var call_order = [], delays = [160,80,240,80]; @@ -3087,6 +3143,7 @@ exports['queue'] = { }, 'push with non-function': function (test) { + test.expect(1); var q = async.queue(function () {}, 1); test.throws(function () { q.push({}, 1); @@ -3095,6 +3152,7 @@ exports['queue'] = { }, 'unshift': function (test) { + test.expect(1); var queue_order = []; var q = async.queue(function (task, callback) { @@ -3114,6 +3172,7 @@ exports['queue'] = { }, 'too many callbacks': function (test) { + test.expect(1); var q = async.queue(function (task, callback) { callback(); test.throws(function() { @@ -3126,6 +3185,7 @@ exports['queue'] = { }, 'bulk task': function (test) { + test.expect(9); var call_order = [], delays = [160,80,240,80]; @@ -3162,6 +3222,7 @@ exports['queue'] = { }, 'idle': function(test) { + test.expect(7); var q = async.queue(function (task, callback) { // Queue is busy when workers are running test.equal(q.idle(), false); @@ -3187,6 +3248,7 @@ exports['queue'] = { }, 'pause': function(test) { + test.expect(3); var call_order = [], task_timeout = 100, pause_timeout = 300, @@ -3240,6 +3302,7 @@ exports['queue'] = { }, 'pause with concurrency': function(test) { + test.expect(4); var call_order = [], task_timeout = 100, pause_timeout = 50, @@ -3291,6 +3354,7 @@ exports['queue'] = { }, 'start paused': function (test) { + test.expect(2); var q = async.queue(function (task, callback) { setTimeout(function () { callback(); @@ -3316,6 +3380,7 @@ exports['queue'] = { }, 'kill': function (test) { + test.expect(1); var q = async.queue(function (task, callback) { setTimeout(function () { test.ok(false, "Function should never be called"); @@ -3337,6 +3402,7 @@ exports['queue'] = { }, 'events': function(test) { + test.expect(4); var calls = []; var q = async.queue(function(task, cb) { // nop @@ -3384,6 +3450,7 @@ exports['queue'] = { }, 'empty': function(test) { + test.expect(2); var calls = []; var q = async.queue(function(task, cb) { // nop @@ -3406,6 +3473,7 @@ exports['queue'] = { }, 'saturated': function (test) { + test.expect(1); var saturatedCalled = false; var q = async.queue(function(task, cb) { async.setImmediate(cb); @@ -3425,16 +3493,16 @@ exports['queue'] = { }, 'started': function(test) { + test.expect(2); - var q = async.queue(function(task, cb) { + var q = async.queue(function(task, cb) { cb(null, task); - }); - - test.equal(q.started, false); - q.push([]); - test.equal(q.started, true); - test.done(); + }); + test.equal(q.started, false); + q.push([]); + test.equal(q.started, true); + test.done(); } }; @@ -3443,6 +3511,7 @@ exports['queue'] = { exports['priorityQueue'] = { 'priorityQueue': function (test) { + test.expect(17); var call_order = []; // order of completion: 2,1,4,3 @@ -3492,7 +3561,8 @@ exports['priorityQueue'] = { }; }, -' concurrency': function (test) { +'concurrency': function (test) { + test.expect(17); var call_order = [], delays = [160,80,240,80]; @@ -3553,6 +3623,7 @@ exports['priorityQueue'] = { exports['cargo'] = { 'cargo': function (test) { + test.expect(19); var call_order = [], delays = [160, 160, 80]; @@ -3619,6 +3690,7 @@ exports['cargo'] = { }, 'without callback': function (test) { + test.expect(1); var call_order = [], delays = [160,80,240,80]; @@ -3655,6 +3727,7 @@ exports['cargo'] = { }, 'bulk task': function (test) { + test.expect(7); var call_order = [], delays = [120,40]; @@ -3687,41 +3760,43 @@ exports['cargo'] = { }, 'drain once': function (test) { + test.expect(1); - var c = async.cargo(function (tasks, callback) { + var c = async.cargo(function (tasks, callback) { callback(); }, 3); var drainCounter = 0; c.drain = function () { - drainCounter++; + drainCounter++; }; for(var i = 0; i < 10; i++){ - c.push(i); + c.push(i); } setTimeout(function(){ - test.equal(drainCounter, 1); - test.done(); + test.equal(drainCounter, 1); + test.done(); }, 500); }, 'drain twice': function (test) { + test.expect(1); var c = async.cargo(function (tasks, callback) { - callback(); + callback(); }, 3); var loadCargo = function(){ - for(var i = 0; i < 10; i++){ - c.push(i); - } + for(var i = 0; i < 10; i++){ + c.push(i); + } }; var drainCounter = 0; c.drain = function () { - drainCounter++; + drainCounter++; }; loadCargo(); @@ -3734,6 +3809,7 @@ exports['cargo'] = { }, 'events': function(test) { + test.expect(4); var calls = []; var q = async.cargo(function(task, cb) { // nop @@ -3780,6 +3856,34 @@ exports['cargo'] = { q.push('moo', function () {calls.push('moo cb');}); }, +'expose payload': function (test) { + test.expect(5); + var called_once = false; + var cargo= async.cargo(function(tasks, cb) { + if (!called_once) { + test.equal(cargo.payload, 1); + test.ok(tasks.length === 1, 'should start with payload = 1'); + } else { + test.equal(cargo.payload, 2); + test.ok(tasks.length === 2, 'next call shold have payload = 2'); + } + called_once = true; + setTimeout(cb, 25); + }, 1); + + cargo.drain = function () { + test.done(); + }; + + test.equals(cargo.payload, 1); + + cargo.push([1, 2, 3]); + + setTimeout(function () { + cargo.payload = 2; + }, 15); +} + }; @@ -3958,6 +4062,7 @@ exports['memoize'] = { exports['ensureAsync'] = { 'defer sync functions': function (test) { + test.expect(6); var sync = true; async.ensureAsync(function (arg1, arg2, cb) { test.equal(arg1, 1); @@ -3974,6 +4079,7 @@ exports['ensureAsync'] = { }, 'do not defer async functions': function (test) { + test.expect(6); var sync = false; async.ensureAsync(function (arg1, arg2, cb) { test.equal(arg1, 1); @@ -3993,6 +4099,7 @@ exports['ensureAsync'] = { }, 'double wrapping': function (test) { + test.expect(6); var sync = true; async.ensureAsync(async.ensureAsync(function (arg1, arg2, cb) { test.equal(arg1, 1); @@ -4008,3 +4115,61 @@ exports['ensureAsync'] = { sync = false; } }; + +exports['constant'] = function (test) { + test.expect(5); + var f = async.constant(42, 1, 2, 3); + f(function (err, value, a, b, c) { + test.ok(!err); + test.ok(value === 42); + test.ok(a === 1); + test.ok(b === 2); + test.ok(c === 3); + test.done(); + }); +}; + +exports['asyncify'] = { + 'asyncify': function (test) { + var parse = async.asyncify(JSON.parse); + parse("{\"a\":1}", function (err, result) { + test.ok(!err); + test.ok(result.a === 1); + test.done(); + }); + }, + + 'variable numbers of arguments': function (test) { + async.asyncify(function (x, y, z) { + test.ok(arguments.length === 3); + test.ok(x === 1); + test.ok(y === 2); + test.ok(z === 3); + })(1, 2, 3, function () {}); + test.done(); + }, + + 'catch errors': function (test) { + async.asyncify(function () { + throw new Error("foo"); + })(function (err) { + test.ok(err); + test.ok(err.message === "foo"); + test.done(); + }); + }, + + 'dont catch errors in the callback': function (test) { + try { + async.asyncify(function () {})(function (err) { + if (err) { + return test.done(new Error("should not get an error here")); + } + throw new Error("callback error"); + }); + } catch (e) { + test.ok(e.message === "callback error"); + test.done(); + } + } +}; |