diff options
Diffstat (limited to 'node_modules/fastq/test')
| -rw-r--r-- | node_modules/fastq/test/test.js | 90 |
1 files changed, 75 insertions, 15 deletions
diff --git a/node_modules/fastq/test/test.js b/node_modules/fastq/test/test.js index accbaf94..ceed7a7b 100644 --- a/node_modules/fastq/test/test.js +++ b/node_modules/fastq/test/test.js @@ -6,10 +6,22 @@ var test = require('tape') var buildQueue = require('../') test('concurrency', function (t) { - t.plan(2) + t.plan(6) t.throws(buildQueue.bind(null, worker, 0)) + t.throws(buildQueue.bind(null, worker, NaN)) t.doesNotThrow(buildQueue.bind(null, worker, 1)) + var queue = buildQueue(worker, 1) + t.throws(function () { + queue.concurrency = 0 + }) + t.throws(function () { + queue.concurrency = NaN + }) + t.doesNotThrow(function () { + queue.concurrency = 2 + }) + function worker (arg, cb) { cb(null, true) } @@ -137,10 +149,11 @@ test('drain', function (t) { }) test('pause && resume', function (t) { - t.plan(7) + t.plan(13) var queue = buildQueue(worker, 1) var worked = false + var expected = [42, 24] t.notOk(queue.paused, 'it should not be paused') @@ -151,26 +164,33 @@ test('pause && resume', function (t) { t.equal(result, true, 'result matches') }) + queue.push(24, function (err, result) { + t.error(err, 'no error') + t.equal(result, true, 'result matches') + }) + t.notOk(worked, 'it should be paused') t.ok(queue.paused, 'it should be paused') queue.resume() + queue.pause() + queue.resume() queue.resume() // second resume is a no-op - t.notOk(queue.paused, 'it should not be paused') - function worker (arg, cb) { - t.equal(arg, 42) + t.notOk(queue.paused, 'it should not be paused') + t.ok(queue.running() <= queue.concurrency, 'should respect the concurrency') + t.equal(arg, expected.shift()) worked = true - cb(null, true) + process.nextTick(function () { cb(null, true) }) } }) test('pause in flight && resume', function (t) { - t.plan(9) + t.plan(16) var queue = buildQueue(worker, 1) - var expected = [42, 24] + var expected = [42, 24, 12] t.notOk(queue.paused, 'it should not be paused') @@ -178,7 +198,11 @@ test('pause in flight && resume', function (t) { t.error(err, 'no error') t.equal(result, true, 'result matches') t.ok(queue.paused, 'it should be paused') - process.nextTick(function () { queue.resume() }) + process.nextTick(function () { + queue.resume() + queue.pause() + queue.resume() + }) }) queue.push(24, function (err, result) { @@ -187,40 +211,60 @@ test('pause in flight && resume', function (t) { t.notOk(queue.paused, 'it should not be paused') }) + queue.push(12, function (err, result) { + t.error(err, 'no error') + t.equal(result, true, 'result matches') + t.notOk(queue.paused, 'it should not be paused') + }) + queue.pause() function worker (arg, cb) { + t.ok(queue.running() <= queue.concurrency, 'should respect the concurrency') t.equal(arg, expected.shift()) process.nextTick(function () { cb(null, true) }) } }) test('altering concurrency', function (t) { - t.plan(7) + t.plan(24) var queue = buildQueue(worker, 1) - var count = 0 - - queue.pause() queue.push(24, workDone) queue.push(24, workDone) + queue.push(24, workDone) + + queue.pause() + queue.concurrency = 3 // concurrency changes are ignored while paused queue.concurrency = 2 queue.resume() t.equal(queue.running(), 2, '2 jobs running') + queue.concurrency = 3 + + t.equal(queue.running(), 3, '3 jobs running') + + queue.concurrency = 1 + + t.equal(queue.running(), 3, '3 jobs running') // running jobs can't be killed + + queue.push(24, workDone) + queue.push(24, workDone) + queue.push(24, workDone) + queue.push(24, workDone) + function workDone (err, result) { t.error(err, 'no error') t.equal(result, true, 'result matches') } function worker (arg, cb) { - t.equal(0, count, 'works in parallel') + t.ok(queue.running() <= queue.concurrency, 'should respect the concurrency') setImmediate(function () { - count++ cb(null, true) }) } @@ -580,3 +624,19 @@ test('unshift with worker throwing error', function (t) { t.match(err.message, /test error/, 'error message should be "test error"') }) }) + +test('pause/resume should trigger drain event', function (t) { + t.plan(1) + + var queue = buildQueue(worker, 1) + queue.pause() + queue.drain = function () { + t.pass('drain should be called') + } + + function worker (arg, cb) { + cb(null, true) + } + + queue.resume() +}) |
