diff options
Diffstat (limited to 'test/test-async.js')
-rwxr-xr-x | test/test-async.js | 193 |
1 files changed, 169 insertions, 24 deletions
diff --git a/test/test-async.js b/test/test-async.js index 1b6be04..539acc8 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -518,6 +518,42 @@ exports['auto removeListener has side effect on loop iterator'] = function(test) }); }; +// Issue 410 on github: https://github.com/caolan/async/issues/410 +exports['auto calls callback multiple times'] = function(test) { + if (typeof process === 'undefined') { + // node only test + return; + } + var finalCallCount = 0; + var domain = require('domain').create(); + domain.on('error', function (e) { + // ignore test error + if (!e._test_error) { + return test.done(e); + } + }); + domain.run(function () { + async.auto({ + task1: function(callback) { callback(null); }, + task2: ['task1', function(callback) { callback(null); }] + }, + + // Error throwing final callback. This should only run once + function(err) { + finalCallCount++; + var e = new Error("An error"); + e._test_error = true; + throw e; + }); + }); + setTimeout(function () { + test.equal(finalCallCount, 1, + "Final auto callback should only be called once" + ); + test.done(); + }, 10); +}; + exports['waterfall'] = function(test){ test.expect(6); var call_order = []; @@ -2282,28 +2318,115 @@ exports['cargo bulk task'] = function (test) { }, 800); }; +exports['cargo drain once'] = function (test) { + + var c = async.cargo(function (tasks, callback) { + callback(); + }, 3); + + var drainCounter = 0; + c.drain = function () { + drainCounter++; + } + + for(var i = 0; i < 10; i++){ + c.push(i); + } + + setTimeout(function(){ + test.equal(drainCounter, 1); + test.done(); + }, 500); +}; + +exports['cargo drain twice'] = function (test) { + + var c = async.cargo(function (tasks, callback) { + callback(); + }, 3); + + var loadCargo = function(){ + for(var i = 0; i < 10; i++){ + c.push(i); + } + }; + + var drainCounter = 0; + c.drain = function () { + drainCounter++; + } + + loadCargo(); + setTimeout(loadCargo, 500); + + setTimeout(function(){ + test.equal(drainCounter, 2); + test.done(); + }, 1000); +}; + exports['memoize'] = function (test) { test.expect(4); var call_order = []; var fn = function (arg1, arg2, callback) { - call_order.push(['fn', arg1, arg2]); - callback(null, arg1 + arg2); + async.setImmediate(function () { + call_order.push(['fn', arg1, arg2]); + callback(null, arg1 + arg2); + }); }; var fn2 = async.memoize(fn); fn2(1, 2, function (err, result) { test.equal(result, 3); + fn2(1, 2, function (err, result) { + test.equal(result, 3); + fn2(2, 2, function (err, result) { + test.equal(result, 4); + test.same(call_order, [['fn',1,2], ['fn',2,2]]); + test.done(); + }); + }); }); +}; + +exports['memoize maintains asynchrony'] = function (test) { + test.expect(3); + var call_order = []; + + var fn = function (arg1, arg2, callback) { + call_order.push(['fn', arg1, arg2]); + async.setImmediate(function () { + call_order.push(['cb', arg1, arg2]); + callback(null, arg1 + arg2); + }); + }; + + var fn2 = async.memoize(fn); fn2(1, 2, function (err, result) { test.equal(result, 3); - }); - fn2(2, 2, function (err, result) { - test.equal(result, 4); - }); - - test.same(call_order, [['fn',1,2], ['fn',2,2]]); - test.done(); + fn2(1, 2, function (err, result) { + test.equal(result, 3); + async.nextTick(memoize_done); + call_order.push('tick3'); + }); + call_order.push('tick2'); + }); + call_order.push('tick1'); + + function memoize_done() { + var async_call_order = [ + ['fn',1,2], // initial async call + 'tick1', // async caller + ['cb',1,2], // async callback + // ['fn',1,2], // memoized // memoized async body + 'tick2', // handler for first async call + // ['cb',1,2], // memoized // memoized async response body + 'tick3' // handler for memoized async call + ]; + test.same(call_order, async_call_order); + test.done(); + } }; exports['unmemoize'] = function(test) { @@ -2312,24 +2435,24 @@ exports['unmemoize'] = function(test) { var fn = function (arg1, arg2, callback) { call_order.push(['fn', arg1, arg2]); - callback(null, arg1 + arg2); + async.setImmediate(function () { + callback(null, arg1 + arg2); + }); }; var fn2 = async.memoize(fn); var fn3 = async.unmemoize(fn2); fn3(1, 2, function (err, result) { test.equal(result, 3); + fn3(1, 2, function (err, result) { + test.equal(result, 3); + fn3(2, 2, function (err, result) { + test.equal(result, 4); + test.same(call_order, [['fn',1,2], ['fn',1,2], ['fn',2,2]]); + test.done(); + }); + }); }); - fn3(1, 2, function (err, result) { - test.equal(result, 3); - }); - fn3(2, 2, function (err, result) { - test.equal(result, 4); - }); - - test.same(call_order, [['fn',1,2], ['fn',1,2], ['fn',2,2]]); - - test.done(); } exports['unmemoize a not memoized function'] = function(test) { @@ -2389,11 +2512,11 @@ exports['memoize custom hash function'] = function (test) { }); fn2(1, 2, function (err, result) { test.equal(result, 3); + fn2(2, 2, function (err, result) { + test.equal(result, 3); + test.done(); + }); }); - fn2(2, 2, function (err, result) { - test.equal(result, 3); - }); - test.done(); }; exports['memoize manually added memo value'] = function (test) { @@ -2523,3 +2646,25 @@ exports['queue events'] = function(test) { q.push('poo', function () {calls.push('poo cb');}); q.push('moo', function () {calls.push('moo cb');}); }; + +exports['queue empty'] = function(test) { + var calls = []; + var q = async.queue(function(task, cb) { + // nop + calls.push('process ' + task); + async.setImmediate(cb); + }, 3); + + q.drain = function() { + test.ok( + q.length() == 0 && q.running() == 0, + 'queue should be empty now and no more workers should be running' + ); + calls.push('drain'); + test.same(calls, [ + 'drain' + ]); + test.done(); + }; + q.push([]); +}; |