diff options
-rw-r--r-- | mocha_test/each.js | 283 | ||||
-rw-r--r-- | mocha_test/eachOf.js | 333 | ||||
-rwxr-xr-x | test/test-async.js | 625 |
3 files changed, 616 insertions, 625 deletions
diff --git a/mocha_test/each.js b/mocha_test/each.js new file mode 100644 index 0000000..014e9df --- /dev/null +++ b/mocha_test/each.js @@ -0,0 +1,283 @@ +var async = require('../lib'); +var expect = require('chai').expect; +var assert = require('assert'); + +describe("each", function() { + + function eachIteratee(args, x, callback) { + setTimeout(function(){ + args.push(x); + callback(); + }, x*25); + } + + function eachNoCallbackIteratee(done, x, callback) { + expect(x).to.equal(1); + callback(); + done(); + } + + it('each', function(done) { + var args = []; + async.each([1,3,2], eachIteratee.bind(this, args), function(err){ + assert(err === null, err + " passed instead of 'null'"); + expect(args).to.eql([1,2,3]); + done(); + }); + }); + + it('each extra callback', function(done) { + var count = 0; + async.each([1,3,2], function(val, callback) { + count++; + var done_ = count == 3; + callback(); + assert.throws(callback); + if (done_) { + done(); + } + }); + }); + + it('each empty array', function(done) { + async.each([], function(x, callback){ + assert(false, 'iteratee should not be called'); + callback(); + }, function(err){ + if (err) throw err; + assert(true, 'should call callback'); + }); + setTimeout(done, 25); + }); + + + it('each empty array, with other property on the array', function(done) { + var myArray = []; + myArray.myProp = "anything"; + async.each(myArray, function(x, callback){ + assert(false, 'iteratee should not be called'); + callback(); + }, function(err){ + if (err) throw err; + assert(true, 'should call callback'); + }); + setTimeout(done, 25); + }); + + + it('each error', function(done) { + async.each([1,2,3], function(x, callback){ + callback('error'); + }, function(err){ + expect(err).to.equal('error'); + }); + setTimeout(done, 50); + }); + + it('each no callback', function(done) { + async.each([1], eachNoCallbackIteratee.bind(this, done)); + }); + + it('eachSeries', function(done) { + var args = []; + async.eachSeries([1,3,2], eachIteratee.bind(this, args), function(err){ + assert(err === null, err + " passed instead of 'null'"); + expect(args).to.eql([1,3,2]); + done(); + }); + }); + + it('eachSeries empty array', function(done) { + async.eachSeries([], function(x, callback){ + assert(false, 'iteratee should not be called'); + callback(); + }, function(err){ + if (err) throw err; + assert(true, 'should call callback'); + }); + setTimeout(done, 25); + }); + + it('eachSeries array modification', function(done) { + var arr = [1, 2, 3, 4]; + async.eachSeries(arr, function (x, callback) { + async.setImmediate(callback); + }, function () { + assert(true, 'should call callback'); + }); + + arr.pop(); + arr.splice(0, 1); + + setTimeout(done, 50); + }); + + // bug #782. Remove in next major release + it('eachSeries single item', function(done) { + var sync = true; + async.eachSeries([1], function (i, cb) { + cb(null); + }, function () { + assert(sync, "callback not called on same tick"); + }); + sync = false; + done(); + }); + + // bug #782. Remove in next major release + it('eachSeries single item', function(done) { + var sync = true; + async.eachSeries([1], function (i, cb) { + cb(null); + }, function () { + assert(sync, "callback not called on same tick"); + }); + sync = false; + done(); + }); + + it('eachSeries error', function(done) { + var call_order = []; + async.eachSeries([1,2,3], function(x, callback){ + call_order.push(x); + callback('error'); + }, function(err){ + expect(call_order).to.eql([1]); + expect(err).to.equal('error'); + }); + setTimeout(done, 50); + }); + + it('eachSeries no callback', function(done) { + async.eachSeries([1], eachNoCallbackIteratee.bind(this, done)); + }); + + + it('eachLimit', function(done) { + 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){ + assert(err === null, err + " passed instead of 'null'"); + expect(args).to.eql(arr); + done(); + }); + }); + + it('eachLimit empty array', function(done) { + async.eachLimit([], 2, function(x, callback){ + assert(false, 'iteratee should not be called'); + callback(); + }, function(err){ + if (err) throw err; + assert(true, 'should call callback'); + }); + setTimeout(done, 25); + }); + + it('eachLimit limit exceeds size', function(done) { + 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; + expect(args).to.eql(arr); + done(); + }); + }); + + it('eachLimit limit equal size', function(done) { + 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; + expect(args).to.eql(arr); + done(); + }); + }); + + it('eachLimit zero limit', function(done) { + async.eachLimit([0,1,2,3,4,5], 0, function(x, callback){ + assert(false, 'iteratee should not be called'); + callback(); + }, function(err){ + if (err) throw err; + assert(true, 'should call callback'); + }); + setTimeout(done, 25); + }); + + it('eachLimit error', function(done) { + 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){ + expect(call_order).to.eql([0,1,2]); + expect(err).to.equal('error'); + }); + setTimeout(done, 25); + }); + + it('eachLimit no callback', function(done) { + async.eachLimit([1], 1, eachNoCallbackIteratee.bind(this, done)); + }); + + it('eachLimit synchronous', function(done) { + var args = []; + var arr = [0,1,2]; + async.eachLimit(arr, 5, function(x,callback){ + args.push(x); + callback(); + }, function(err){ + if (err) throw err; + expect(args).to.eql(arr); + done(); + }); + }); + + it('eachLimit does not continue replenishing after error', function(done) { + 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(){ + expect(started).to.equal(3); + done(); + }, maxTime); + }); + + it('forEach alias', function(done) { + assert.strictEqual(async.each, async.forEach); + done(); + }); + + it('forEachSeries alias', function(done) { + assert.strictEqual(async.eachSeries, async.forEachSeries); + done(); + }); + + it('forEachLimit alias', function(done) { + assert.strictEqual(async.eachLimit, async.forEachLimit); + done(); + }); +}); diff --git a/mocha_test/eachOf.js b/mocha_test/eachOf.js new file mode 100644 index 0000000..c705bad --- /dev/null +++ b/mocha_test/eachOf.js @@ -0,0 +1,333 @@ +var async = require('../lib'); +var expect = require('chai').expect; +var assert = require('assert'); + +describe("eachOf", function() { + + function forEachOfNoCallbackIteratee(done, x, key, callback) { + expect(x).to.equal(1); + expect(key).to.equal("a"); + callback(); + done(); + } + + function forEachOfIteratee(args, value, key, callback) { + setTimeout(function(){ + args.push(key, value); + callback(); + }, value*25); + } + + it('eachOf alias', function(done) { + expect(async.eachOf).to.equal(async.forEachOf); + done(); + }); + + it('eachOfLimit alias', function(done) { + expect(async.eachOfLimit).to.equal(async.forEachOfLimit); + done(); + }); + + it('eachOfSeries alias', function(done) { + expect(async.eachOfSeries).to.equal(async.forEachOfSeries); + done(); + }); + + it('forEachOf', function(done) { + var args = []; + async.forEachOf({ a: 1, b: 2 }, forEachOfIteratee.bind(this, args), function(err){ + assert(err === null, err + " passed instead of 'null'"); + expect(args).to.eql(["a", 1, "b", 2]); + done(); + }); + }); + + it('forEachOf - instant resolver', function(done) { + 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 + expect(args).to.eql(["a", 1, "b", 2]); + done(); + }); + }); + + it('forEachOf empty object', function(done) { + async.forEachOf({}, function(value, key, callback){ + assert(false, 'iteratee should not be called'); + callback(); + }, function(err) { + if (err) throw err; + assert(true, 'should call callback'); + }); + setTimeout(done, 25); + }); + + it('forEachOf empty array', function(done) { + async.forEachOf([], function(value, key, callback){ + assert(false, 'iteratee should not be called'); + callback(); + }, function(err) { + if (err) throw err; + assert(true, 'should call callback'); + }); + setTimeout(done, 25); + }); + + it('forEachOf error', function(done) { + async.forEachOf({ a: 1, b: 2 }, function(value, key, callback) { + callback('error'); + }, function(err){ + expect(err).to.equal('error'); + }); + setTimeout(done, 50); + }); + + it('forEachOf no callback', function(done) { + async.forEachOf({ a: 1 }, forEachOfNoCallbackIteratee.bind(this, done)); + }); + + + it('forEachOf with array', function(done) { + var args = []; + async.forEachOf([ "a", "b" ], forEachOfIteratee.bind(this, args), function(err){ + if (err) throw err; + expect(args).to.eql([0, "a", 1, "b"]); + done(); + }); + }); + + it('forEachOf with Set (iterators)', function(done) { + if (typeof Set !== 'function') + return 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; + expect(args).to.eql([0, "a", 1, "b"]); + done(); + }); + }); + + it('forEachOf with Map (iterators)', function(done) { + if (typeof Map !== 'function') + return 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; + expect(args).to.eql([0, [1, "a"], 1, [2, "b"]]); + done(); + }); + }); + + it('forEachOfSeries', function(done) { + var args = []; + async.forEachOfSeries({ a: 1, b: 2 }, forEachOfIteratee.bind(this, args), function(err){ + assert(err === null, err + " passed instead of 'null'"); + expect(args).to.eql([ "a", 1, "b", 2 ]); + done(); + }); + }); + + it('forEachOfSeries empty object', function(done) { + async.forEachOfSeries({}, function(x, callback){ + assert(false, 'iteratee should not be called'); + callback(); + }, function(err){ + if (err) throw err; + assert(true, 'should call callback'); + }); + setTimeout(done, 25); + }); + + it('forEachOfSeries error', function(done) { + var call_order = []; + async.forEachOfSeries({ a: 1, b: 2 }, function(value, key, callback){ + call_order.push(value, key); + callback('error'); + }, function(err){ + expect(call_order).to.eql([ 1, "a" ]); + expect(err).to.equal('error'); + }); + setTimeout(done, 50); + }); + + it('forEachOfSeries no callback', function(done) { + async.forEachOfSeries({ a: 1 }, forEachOfNoCallbackIteratee.bind(this, done)); + }); + + it('forEachOfSeries with array', function(done) { + var args = []; + async.forEachOfSeries([ "a", "b" ], forEachOfIteratee.bind(this, args), function(err){ + if (err) throw err; + expect(args).to.eql([ 0, "a", 1, "b" ]); + done(); + }); + }); + + it('forEachOfSeries with Set (iterators)', function(done) { + if (typeof Set !== 'function') + return 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; + expect(args).to.eql([0, "a", 1, "b"]); + done(); + }); + }); + + it('forEachOfSeries with Map (iterators)', function(done) { + if (typeof Map !== 'function') + return 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; + expect(args).to.eql([0, [1, "a"], 1, [2, "b"]]); + done(); + }); + }); + + it('forEachOfLimit', function(done) { + 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){ + assert(err === null, err + " passed instead of 'null'"); + expect(args).to.eql([ 1, "a", 2, "b", 3, "c", 4, "d" ]); + done(); + }); + }); + + it('forEachOfLimit empty object', function(done) { + async.forEachOfLimit({}, 2, function(value, key, callback){ + assert(false, 'iteratee should not be called'); + callback(); + }, function(err){ + if (err) throw err; + assert(true, 'should call callback'); + }); + setTimeout(done, 25); + }); + + it('forEachOfLimit limit exceeds size', function(done) { + 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; + expect(args).to.eql([ "a", 1, "b", 2, "c", 3, "d", 4, "e", 5 ]); + done(); + }); + }); + + it('forEachOfLimit limit equal size', function(done) { + 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; + expect(args).to.eql([ "a", 1, "b", 2, "c", 3, "d", 4, "e", 5 ]); + done(); + }); + }); + + it('forEachOfLimit zero limit', function(done) { + async.forEachOfLimit({ a: 1, b: 2 }, 0, function(x, callback){ + assert(false, 'iteratee should not be called'); + callback(); + }, function(err){ + if (err) throw err; + assert(true, 'should call callback'); + }); + setTimeout(done, 25); + }); + + it('forEachOfLimit error', function(done) { + 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){ + expect(call_order).to.eql([ 1, "a", 2, "b" ]); + expect(err).to.equal('error'); + }); + setTimeout(done, 25); + }); + + it('forEachOfLimit no callback', function(done) { + async.forEachOfLimit({ a: 1 }, 1, forEachOfNoCallbackIteratee.bind(this, done)); + }); + + it('forEachOfLimit synchronous', function(done) { + var args = []; + var obj = { a: 1, b: 2 }; + async.forEachOfLimit(obj, 5, forEachOfIteratee.bind(this, args), function(err){ + if (err) throw err; + expect(args).to.eql([ "a", 1, "b", 2 ]); + done(); + }); + }); + + it('forEachOfLimit with array', function(done) { + var args = []; + var arr = [ "a", "b" ]; + async.forEachOfLimit(arr, 1, forEachOfIteratee.bind(this, args), function (err) { + if (err) throw err; + expect(args).to.eql([ 0, "a", 1, "b" ]); + done(); + }); + }); + + it('forEachOfLimit with Set (iterators)', function(done) { + if (typeof Set !== 'function') + return 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; + expect(args).to.eql([0, "a", 1, "b"]); + done(); + }); + }); + + it('forEachOfLimit with Map (iterators)', function(done) { + if (typeof Map !== 'function') + return 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; + expect(args).to.eql([0, [1, "a"], 1, [2, "b"]]); + done(); + }); + }); +}); diff --git a/test/test-async.js b/test/test-async.js index 53d8cb4..ae3f3c4 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){ |