diff options
author | Alexander Early <alexander.early@gmail.com> | 2017-04-08 21:09:16 -0700 |
---|---|---|
committer | Alexander Early <alexander.early@gmail.com> | 2017-04-08 21:09:16 -0700 |
commit | d20b9c56dbf7a0d66c26b6aa5bcb745d2baa1ab3 (patch) | |
tree | 0ae78384b92e4c790550e710cedc7559dbaae2bf | |
parent | 8f58dbd1da3da50ee820a80c1d5088ca37a1d53a (diff) | |
download | async-d20b9c56dbf7a0d66c26b6aa5bcb745d2baa1ab3.tar.gz |
refactor flaky queue tests
-rw-r--r-- | lib/internal/DoublyLinkedList.js | 3 | ||||
-rw-r--r-- | mocha_test/queue.js | 93 |
2 files changed, 51 insertions, 45 deletions
diff --git a/lib/internal/DoublyLinkedList.js b/lib/internal/DoublyLinkedList.js index 0e3b394..86c5cfd 100644 --- a/lib/internal/DoublyLinkedList.js +++ b/lib/internal/DoublyLinkedList.js @@ -66,9 +66,8 @@ DLL.prototype.pop = function() { DLL.prototype.toArray = function () { var arr = Array(this.length); - var idx = 0; var curr = this.head; - for(idx = 0; idx < this.length; idx++) { + for(var idx = 0; idx < this.length; idx++) { arr[idx] = curr.data; curr = curr.next; } diff --git a/mocha_test/queue.js b/mocha_test/queue.js index 77d4710..a0a248c 100644 --- a/mocha_test/queue.js +++ b/mocha_test/queue.js @@ -222,16 +222,21 @@ describe('queue', function(){ it('push without callback', function(done) { this.retries(3); // test can be flakey - var call_order = [], - delays = [40,20,60,20]; + var call_order = []; + var delays = [40,10,60,10]; + var concurrencyList = []; + var running = 0; // worker1: --1-4 // worker2: -2---3 // order of completion: 2,1,4,3 var q = async.queue(function (task, callback) { + running++; + concurrencyList.push(running); setTimeout(function () { call_order.push('process ' + task); + running--; callback('error', 'arg'); }, delays.shift()); }, 2); @@ -242,6 +247,8 @@ describe('queue', function(){ q.push(4); q.drain = function () { + expect(running).to.eql(0); + expect(concurrencyList).to.eql([1, 2, 2, 2]); expect(call_order).to.eql([ 'process 2', 'process 1', @@ -353,58 +360,58 @@ describe('queue', function(){ }); it('pause', function(done) { - this.retries(3); // sometimes can be flakey to timing issues - - var call_order = [], - task_timeout = 80, - pause_timeout = task_timeout * 2.5, - resume_timeout = task_timeout * 4.5, - tasks = [ 1, 2, 3, 4, 5, 6 ], - - elapsed = (function () { - var start = Date.now(); - return function () { - return Math.round((Date.now() - start) / task_timeout) * task_timeout; - }; - })(); + var call_order = []; + var running = 0; + var concurrencyList = []; + var pauseCalls = ['process 1', 'process 2', 'process 3']; var q = async.queue(function (task, callback) { + running++; call_order.push('process ' + task); - call_order.push('timeout ' + elapsed()); - callback(); - }); - - function pushTask () { - var task = tasks.shift(); - if (!task) { return; } + concurrencyList.push(running); setTimeout(function () { - q.push(task); - pushTask(); - }, task_timeout); - } - pushTask(); + running--; + callback(); + }, 10) + }, 2); - setTimeout(function () { + q.push(1); + q.push(2, after2); + q.push(3); + + function after2() { q.pause(); - expect(q.paused).to.equal(true); - }, pause_timeout); + expect(concurrencyList).to.eql([1, 2, 2]); + expect(call_order).to.eql(pauseCalls); - setTimeout(function () { - q.resume(); - expect(q.paused).to.equal(false); - }, resume_timeout); + setTimeout(whilePaused, 5); + setTimeout(afterPause, 10); + } - setTimeout(function () { + function whilePaused() { + q.push(4); + } + + function afterPause() { + expect(concurrencyList).to.eql([1, 2, 2]); + expect(call_order).to.eql(pauseCalls); + q.resume(); + q.push(5); + q.push(6); + q.drain = drain; + } + function drain () { + expect(concurrencyList).to.eql([1, 2, 2, 1, 2, 2]); expect(call_order).to.eql([ - 'process 1', 'timeout ' + task_timeout, - 'process 2', 'timeout ' + task_timeout * 2, - 'process 3', 'timeout ' + task_timeout * 5, - 'process 4', 'timeout ' + task_timeout * 5, - 'process 5', 'timeout ' + task_timeout * 5, - 'process 6', 'timeout ' + task_timeout * 6 + 'process 1', + 'process 2', + 'process 3', + 'process 4', + 'process 5', + 'process 6' ]); done(); - }, (task_timeout * tasks.length) + pause_timeout + resume_timeout); + } }); it('pause in worker with concurrency', function(done) { |