diff options
Diffstat (limited to 'test/test-async.js')
-rwxr-xr-x | test/test-async.js | 845 |
1 files changed, 0 insertions, 845 deletions
diff --git a/test/test-async.js b/test/test-async.js index 53d8cb4..e35bdcf 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -16,33 +16,6 @@ if (!Function.prototype.bind) { }; } -function eachIteratee(args, x, callback) { - setTimeout(function(){ - args.push(x); - callback(); - }, x*25); -} - -function forEachOfIteratee(args, value, key, callback) { - setTimeout(function(){ - args.push(key, value); - callback(); - }, value*25); -} - -function eachNoCallbackIteratee(test, x, callback) { - test.equal(x, 1); - callback(); - test.done(); -} - -function forEachOfNoCallbackIteratee(test, x, key, callback) { - test.equal(x, 1); - test.equal(key, "a"); - callback(); - test.done(); -} - function getFunctionsObject(call_order) { return { one: function(callback){ @@ -706,604 +679,6 @@ exports['iterator.next'] = function(test){ test.done(); }; -exports['each'] = function(test){ - var args = []; - async.each([1,3,2], eachIteratee.bind(this, args), function(err){ - test.ok(err === null, err + " passed instead of 'null'"); - test.same(args, [1,2,3]); - test.done(); - }); -}; - -exports['each extra callback'] = function(test){ - var count = 0; - async.each([1,3,2], function(val, callback) { - count++; - var done = count == 3; - callback(); - test.throws(callback); - if (done) { - test.done(); - } - }); -}; - -exports['each empty array'] = function(test){ - test.expect(1); - async.each([], function(x, callback){ - test.ok(false, 'iteratee should not be called'); - callback(); - }, function(err){ - if (err) throw err; - test.ok(true, 'should call callback'); - }); - setTimeout(test.done, 25); -}; - - -exports['each empty array, with other property on the array'] = function(test){ - test.expect(1); - var myArray = []; - myArray.myProp = "anything"; - async.each(myArray, function(x, callback){ - test.ok(false, 'iteratee should not be called'); - callback(); - }, function(err){ - if (err) throw err; - test.ok(true, 'should call callback'); - }); - setTimeout(test.done, 25); -}; - - -exports['each error'] = function(test){ - test.expect(1); - async.each([1,2,3], function(x, callback){ - callback('error'); - }, function(err){ - test.equals(err, 'error'); - }); - setTimeout(test.done, 50); -}; - -exports['each no callback'] = function(test){ - async.each([1], eachNoCallbackIteratee.bind(this, test)); -}; - -exports['forEach alias'] = function (test) { - test.strictEqual(async.each, async.forEach); - test.done(); -}; - -exports['forEachOf'] = function(test){ - var args = []; - async.forEachOf({ a: 1, b: 2 }, forEachOfIteratee.bind(this, args), function(err){ - test.ok(err === null, err + " passed instead of 'null'"); - test.same(args, ["a", 1, "b", 2]); - test.done(); - }); -}; - -exports['forEachOf - instant resolver'] = function(test){ - test.expect(1); - var args = []; - async.forEachOf({ a: 1, b: 2 }, function(x, k, cb) { - args.push(k, x); - cb(); - }, function(){ - // ensures done callback isn't called before all items iterated - test.same(args, ["a", 1, "b", 2]); - test.done(); - }); -}; - -exports['forEachOf empty object'] = function(test){ - test.expect(1); - async.forEachOf({}, function(value, key, callback){ - test.ok(false, 'iteratee should not be called'); - callback(); - }, function(err) { - if (err) throw err; - test.ok(true, 'should call callback'); - }); - setTimeout(test.done, 25); -}; - -exports['forEachOf empty array'] = function(test){ - test.expect(1); - async.forEachOf([], function(value, key, callback){ - test.ok(false, 'iteratee should not be called'); - callback(); - }, function(err) { - if (err) throw err; - test.ok(true, 'should call callback'); - }); - setTimeout(test.done, 25); -}; - -exports['forEachOf error'] = function(test){ - test.expect(1); - async.forEachOf({ a: 1, b: 2 }, function(value, key, callback) { - callback('error'); - }, function(err){ - test.equals(err, 'error'); - }); - setTimeout(test.done, 50); -}; - -exports['forEachOf no callback'] = function(test){ - async.forEachOf({ a: 1 }, forEachOfNoCallbackIteratee.bind(this, test)); -}; - -exports['eachOf alias'] = function(test){ - test.equals(async.eachOf, async.forEachOf); - test.done(); -}; - -exports['forEachOf with array'] = function(test){ - var args = []; - async.forEachOf([ "a", "b" ], forEachOfIteratee.bind(this, args), function(err){ - if (err) throw err; - test.same(args, [0, "a", 1, "b"]); - test.done(); - }); -}; - -exports['forEachOf with Set (iterators)'] = function(test){ - if (typeof Set !== 'function') - return test.done(); - - var args = []; - var set = new Set(); - set.add("a"); - set.add("b"); - async.forEachOf(set, forEachOfIteratee.bind(this, args), function(err){ - if (err) throw err; - test.same(args, [0, "a", 1, "b"]); - test.done(); - }); -}; - -exports['forEachOf with Map (iterators)'] = function(test){ - if (typeof Map !== 'function') - return test.done(); - - var args = []; - var map = new Map(); - map.set(1, "a"); - map.set(2, "b"); - async.forEachOf(map, forEachOfIteratee.bind(this, args), function(err){ - if (err) throw err; - test.same(args, [0, [1, "a"], 1, [2, "b"]]); - test.done(); - }); -}; - -exports['eachSeries'] = function(test){ - var args = []; - async.eachSeries([1,3,2], eachIteratee.bind(this, args), function(err){ - test.ok(err === null, err + " passed instead of 'null'"); - test.same(args, [1,3,2]); - test.done(); - }); -}; - -exports['eachSeries empty array'] = function(test){ - test.expect(1); - async.eachSeries([], function(x, callback){ - test.ok(false, 'iteratee should not be called'); - callback(); - }, function(err){ - if (err) throw err; - test.ok(true, 'should call callback'); - }); - setTimeout(test.done, 25); -}; - -exports['eachSeries array modification'] = function(test) { - test.expect(1); - var arr = [1, 2, 3, 4]; - async.eachSeries(arr, function (x, callback) { - async.setImmediate(callback); - }, function () { - test.ok(true, 'should call callback'); - }); - - arr.pop(); - arr.splice(0, 1); - - setTimeout(test.done, 50); -}; - -// bug #782. Remove in next major release -exports['eachSeries single item'] = function (test) { - test.expect(1); - var sync = true; - async.eachSeries([1], function (i, cb) { - cb(null); - }, function () { - test.ok(sync, "callback not called on same tick"); - }); - sync = false; - test.done(); -}; - -// bug #782. Remove in next major release -exports['eachSeries single item'] = function (test) { - test.expect(1); - var sync = true; - async.eachSeries([1], function (i, cb) { - cb(null); - }, function () { - test.ok(sync, "callback not called on same tick"); - }); - sync = false; - test.done(); -}; - -exports['eachSeries error'] = function(test){ - test.expect(2); - var call_order = []; - async.eachSeries([1,2,3], function(x, callback){ - call_order.push(x); - callback('error'); - }, function(err){ - test.same(call_order, [1]); - test.equals(err, 'error'); - }); - setTimeout(test.done, 50); -}; - -exports['eachSeries no callback'] = function(test){ - async.eachSeries([1], eachNoCallbackIteratee.bind(this, test)); -}; - - -exports['eachLimit'] = function(test){ - var args = []; - var arr = [0,1,2,3,4,5,6,7,8,9]; - async.eachLimit(arr, 2, function(x,callback){ - setTimeout(function(){ - args.push(x); - callback(); - }, x*5); - }, function(err){ - test.ok(err === null, err + " passed instead of 'null'"); - test.same(args, arr); - test.done(); - }); -}; - -exports['eachLimit empty array'] = function(test){ - test.expect(1); - async.eachLimit([], 2, function(x, callback){ - test.ok(false, 'iteratee should not be called'); - callback(); - }, function(err){ - if (err) throw err; - test.ok(true, 'should call callback'); - }); - setTimeout(test.done, 25); -}; - -exports['eachLimit limit exceeds size'] = function(test){ - var args = []; - var arr = [0,1,2,3,4,5,6,7,8,9]; - async.eachLimit(arr, 20, eachIteratee.bind(this, args), function(err){ - if (err) throw err; - test.same(args, arr); - test.done(); - }); -}; - -exports['eachLimit limit equal size'] = function(test){ - var args = []; - var arr = [0,1,2,3,4,5,6,7,8,9]; - async.eachLimit(arr, 10, eachIteratee.bind(this, args), function(err){ - if (err) throw err; - test.same(args, arr); - test.done(); - }); -}; - -exports['eachLimit zero limit'] = function(test){ - test.expect(1); - async.eachLimit([0,1,2,3,4,5], 0, function(x, callback){ - test.ok(false, 'iteratee should not be called'); - callback(); - }, function(err){ - if (err) throw err; - test.ok(true, 'should call callback'); - }); - setTimeout(test.done, 25); -}; - -exports['eachLimit error'] = function(test){ - test.expect(2); - var arr = [0,1,2,3,4,5,6,7,8,9]; - var call_order = []; - - async.eachLimit(arr, 3, function(x, callback){ - call_order.push(x); - if (x === 2) { - callback('error'); - } - }, function(err){ - test.same(call_order, [0,1,2]); - test.equals(err, 'error'); - }); - setTimeout(test.done, 25); -}; - -exports['eachLimit no callback'] = function(test){ - async.eachLimit([1], 1, eachNoCallbackIteratee.bind(this, test)); -}; - -exports['eachLimit synchronous'] = function(test){ - var args = []; - var arr = [0,1,2]; - async.eachLimit(arr, 5, function(x,callback){ - args.push(x); - callback(); - }, function(err){ - if (err) throw err; - test.same(args, arr); - test.done(); - }); -}; - - -exports['eachLimit does not continue replenishing after error'] = function (test) { - var started = 0; - var arr = [0,1,2,3,4,5,6,7,8,9]; - var delay = 10; - var limit = 3; - var maxTime = 10 * arr.length; - - async.eachLimit(arr, limit, function(x, callback) { - started ++; - if (started === 3) { - return callback(new Error ("Test Error")); - } - setTimeout(function(){ - callback(); - }, delay); - }, function(){}); - - setTimeout(function(){ - test.equal(started, 3); - test.done(); - }, maxTime); -}; - -exports['forEachSeries alias'] = function (test) { - test.strictEqual(async.eachSeries, async.forEachSeries); - test.done(); -}; - -exports['forEachOfSeries'] = function(test){ - var args = []; - async.forEachOfSeries({ a: 1, b: 2 }, forEachOfIteratee.bind(this, args), function(err){ - test.ok(err === null, err + " passed instead of 'null'"); - test.same(args, [ "a", 1, "b", 2 ]); - test.done(); - }); -}; - -exports['forEachOfSeries empty object'] = function(test){ - test.expect(1); - async.forEachOfSeries({}, function(x, callback){ - test.ok(false, 'iteratee should not be called'); - callback(); - }, function(err){ - if (err) throw err; - test.ok(true, 'should call callback'); - }); - setTimeout(test.done, 25); -}; - -exports['forEachOfSeries error'] = function(test){ - test.expect(2); - var call_order = []; - async.forEachOfSeries({ a: 1, b: 2 }, function(value, key, callback){ - call_order.push(value, key); - callback('error'); - }, function(err){ - test.same(call_order, [ 1, "a" ]); - test.equals(err, 'error'); - }); - setTimeout(test.done, 50); -}; - -exports['forEachOfSeries no callback'] = function(test){ - async.forEachOfSeries({ a: 1 }, forEachOfNoCallbackIteratee.bind(this, test)); -}; - -exports['forEachOfSeries with array'] = function(test){ - var args = []; - async.forEachOfSeries([ "a", "b" ], forEachOfIteratee.bind(this, args), function(err){ - if (err) throw err; - test.same(args, [ 0, "a", 1, "b" ]); - test.done(); - }); -}; - -exports['forEachOfSeries with Set (iterators)'] = function(test){ - if (typeof Set !== 'function') - return test.done(); - - var args = []; - var set = new Set(); - set.add("a"); - set.add("b"); - async.forEachOfSeries(set, forEachOfIteratee.bind(this, args), function(err){ - if (err) throw err; - test.same(args, [0, "a", 1, "b"]); - test.done(); - }); -}; - -exports['forEachOfSeries with Map (iterators)'] = function(test){ - if (typeof Map !== 'function') - return test.done(); - - var args = []; - var map = new Map(); - map.set(1, "a"); - map.set(2, "b"); - async.forEachOfSeries(map, forEachOfIteratee.bind(this, args), function(err){ - if (err) throw err; - test.same(args, [0, [1, "a"], 1, [2, "b"]]); - test.done(); - }); -}; - -exports['eachOfLimit alias'] = function(test){ - test.equals(async.eachOfLimit, async.forEachOfLimit); - test.done(); -}; - - -exports['eachOfSeries alias'] = function(test){ - test.equals(async.eachOfSeries, async.forEachOfSeries); - test.done(); -}; - -exports['forEachLimit alias'] = function (test) { - test.strictEqual(async.eachLimit, async.forEachLimit); - test.done(); -}; - -exports['forEachOfLimit'] = function(test){ - var args = []; - var obj = { a: 1, b: 2, c: 3, d: 4 }; - async.forEachOfLimit(obj, 2, function(value, key, callback){ - setTimeout(function(){ - args.push(value, key); - callback(); - }, value * 5); - }, function(err){ - test.ok(err === null, err + " passed instead of 'null'"); - test.same(args, [ 1, "a", 2, "b", 3, "c", 4, "d" ]); - test.done(); - }); -}; - -exports['forEachOfLimit empty object'] = function(test){ - test.expect(1); - async.forEachOfLimit({}, 2, function(value, key, callback){ - test.ok(false, 'iteratee should not be called'); - callback(); - }, function(err){ - if (err) throw err; - test.ok(true, 'should call callback'); - }); - setTimeout(test.done, 25); -}; - -exports['forEachOfLimit limit exceeds size'] = function(test){ - var args = []; - var obj = { a: 1, b: 2, c: 3, d: 4, e: 5 }; - async.forEachOfLimit(obj, 10, forEachOfIteratee.bind(this, args), function(err){ - if (err) throw err; - test.same(args, [ "a", 1, "b", 2, "c", 3, "d", 4, "e", 5 ]); - test.done(); - }); -}; - -exports['forEachOfLimit limit equal size'] = function(test){ - var args = []; - var obj = { a: 1, b: 2, c: 3, d: 4, e: 5 }; - async.forEachOfLimit(obj, 5, forEachOfIteratee.bind(this, args), function(err){ - if (err) throw err; - test.same(args, [ "a", 1, "b", 2, "c", 3, "d", 4, "e", 5 ]); - test.done(); - }); -}; - -exports['forEachOfLimit zero limit'] = function(test){ - test.expect(1); - async.forEachOfLimit({ a: 1, b: 2 }, 0, function(x, callback){ - test.ok(false, 'iteratee should not be called'); - callback(); - }, function(err){ - if (err) throw err; - test.ok(true, 'should call callback'); - }); - setTimeout(test.done, 25); -}; - -exports['forEachOfLimit error'] = function(test){ - test.expect(2); - var obj = { a: 1, b: 2, c: 3, d: 4, e: 5 }; - var call_order = []; - - async.forEachOfLimit(obj, 3, function(value, key, callback){ - call_order.push(value, key); - if (value === 2) { - callback('error'); - } - }, function(err){ - test.same(call_order, [ 1, "a", 2, "b" ]); - test.equals(err, 'error'); - }); - setTimeout(test.done, 25); -}; - -exports['forEachOfLimit no callback'] = function(test){ - async.forEachOfLimit({ a: 1 }, 1, forEachOfNoCallbackIteratee.bind(this, test)); -}; - -exports['forEachOfLimit synchronous'] = function(test){ - var args = []; - var obj = { a: 1, b: 2 }; - async.forEachOfLimit(obj, 5, forEachOfIteratee.bind(this, args), function(err){ - if (err) throw err; - test.same(args, [ "a", 1, "b", 2 ]); - test.done(); - }); -}; - -exports['forEachOfLimit with array'] = function(test){ - var args = []; - var arr = [ "a", "b" ]; - async.forEachOfLimit(arr, 1, forEachOfIteratee.bind(this, args), function (err) { - if (err) throw err; - test.same(args, [ 0, "a", 1, "b" ]); - test.done(); - }); -}; - -exports['forEachOfLimit with Set (iterators)'] = function(test){ - if (typeof Set !== 'function') - return test.done(); - - var args = []; - var set = new Set(); - set.add("a"); - set.add("b"); - async.forEachOfLimit(set, 1, forEachOfIteratee.bind(this, args), function(err){ - if (err) throw err; - test.same(args, [0, "a", 1, "b"]); - test.done(); - }); -}; - -exports['forEachOfLimit with Map (iterators)'] = function(test){ - if (typeof Map !== 'function') - return test.done(); - - var args = []; - var map = new Map(); - map.set(1, "a"); - map.set(2, "b"); - async.forEachOfLimit(map, 1, forEachOfIteratee.bind(this, args), function(err){ - if (err) throw err; - test.same(args, [0, [1, "a"], 1, [2, "b"]]); - test.done(); - }); -}; - exports['reduce'] = function(test){ var call_order = []; async.reduce([1,2,3], 0, function(a, x, callback){ @@ -2002,226 +1377,6 @@ exports['whilst optional callback'] = function (test) { test.done(); }; - -exports['memoize'] = { - - 'memoize': function (test) { - test.expect(5); - var call_order = []; - - var fn = function (arg1, arg2, callback) { - 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.ok(err === null, err + " passed instead of 'null'"); - 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(); - }); - }); - }); -}, - - '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(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(); - } -}, - - 'unmemoize': function(test) { - test.expect(4); - var call_order = []; - - var fn = function (arg1, arg2, callback) { - call_order.push(['fn', 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(); - }); - }); - }); -}, - - 'unmemoize a not memoized function': function(test) { - test.expect(1); - - var fn = function (arg1, arg2, callback) { - callback(null, arg1 + arg2); - }; - - var fn2 = async.unmemoize(fn); - fn2(1, 2, function(err, result) { - test.equal(result, 3); - }); - - test.done(); -}, - - 'error': function (test) { - test.expect(1); - var testerr = new Error('test'); - var fn = function (arg1, arg2, callback) { - callback(testerr, arg1 + arg2); - }; - async.memoize(fn)(1, 2, function (err) { - test.equal(err, testerr); - }); - test.done(); -}, - - 'multiple calls': function (test) { - test.expect(3); - var fn = function (arg1, arg2, callback) { - test.ok(true); - setTimeout(function(){ - callback(null, arg1, arg2); - }, 10); - }; - var fn2 = async.memoize(fn); - fn2(1, 2, function(err, result) { - test.equal(result, 1, 2); - }); - fn2(1, 2, function(err, result) { - test.equal(result, 1, 2); - test.done(); - }); -}, - - 'custom hash function': function (test) { - test.expect(2); - var testerr = new Error('test'); - - var fn = function (arg1, arg2, callback) { - callback(testerr, arg1 + arg2); - }; - var fn2 = async.memoize(fn, function () { - return 'custom hash'; - }); - fn2(1, 2, function (err, result) { - test.equal(result, 3); - fn2(2, 2, function (err, result) { - test.equal(result, 3); - test.done(); - }); - }); -}, - - 'manually added memo value': function (test) { - test.expect(1); - var fn = async.memoize(function() { - test(false, "Function should never be called"); - }); - fn.memo["foo"] = ["bar"]; - fn("foo", function(val) { - test.equal(val, "bar"); - test.done(); - }); -}, - - 'avoid constructor key return undefined': function (test) { - test.expect(1); - var fn = async.memoize(function(name, callback) { - setTimeout(function(){ - callback(null, name); - }, 100); - }); - fn('constructor', function(error, results) { - test.equal(results, 'constructor'); - test.done(); - }); -}, - - 'avoid __proto__ key return undefined': function (test) { - // Skip test if there is a Object.create bug (node 0.10 and some Chrome 30x versions) - var x = Object.create(null); - /* jshint proto: true */ - x.__proto__ = 'foo'; - if (x.__proto__ !== 'foo') { - return test.done(); - } - - test.expect(1); - var fn = async.memoize(function(name, callback) { - setTimeout(function(){ - callback(null, name); - }, 100); - }); - fn('__proto__', function(error, results) { - test.equal(results, '__proto__'); - test.done(); - }); -}, - - 'allow hasOwnProperty as key': function (test) { - test.expect(1); - var fn = async.memoize(function(name, callback) { - setTimeout(function(){ - callback(null, name); - }, 100); - }); - fn('hasOwnProperty', function(error, results) { - test.equal(results, 'hasOwnProperty'); - test.done(); - }); -} - -}; - - exports['ensureAsync'] = { 'defer sync functions': function (test) { test.expect(6); |