From d20b9c56dbf7a0d66c26b6aa5bcb745d2baa1ab3 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Sat, 8 Apr 2017 21:09:16 -0700 Subject: refactor flaky queue tests --- lib/internal/DoublyLinkedList.js | 3 +- 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) { -- cgit v1.2.1 From 7c17e23176663a44d36c59e4489803b43ff572d1 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Sat, 8 Apr 2017 21:20:56 -0700 Subject: tweaked queue timeouts, removed redundant test --- mocha_test/queue.js | 58 ++++------------------------------------------------- 1 file changed, 4 insertions(+), 54 deletions(-) diff --git a/mocha_test/queue.js b/mocha_test/queue.js index a0a248c..192f7fc 100644 --- a/mocha_test/queue.js +++ b/mocha_test/queue.js @@ -10,7 +10,7 @@ describe('queue', function(){ it('basics', function(done) { var call_order = []; - var delays = [40,20,60,20]; + var delays = [40,10,60,10]; // worker1: --1-4 @@ -66,7 +66,7 @@ describe('queue', function(){ it('default concurrency', function(done) { var call_order = [], - delays = [40,20,60,20]; + delays = [40,10,60,10]; // order of completion: 1,2,3,4 @@ -300,7 +300,7 @@ describe('queue', function(){ it('bulk task', function(done) { var call_order = [], - delays = [40,20,60,20]; + delays = [40,10,60,10]; // worker1: --1-4 // worker2: -2---3 @@ -443,57 +443,6 @@ describe('queue', function(){ }; }); - it('pause with concurrency', function(done) { - var call_order = [], - task_timeout = 40, - pause_timeout = task_timeout / 2, - resume_timeout = task_timeout * 2.75, - 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 q = async.queue(function (task, callback) { - setTimeout(function () { - call_order.push('process ' + task); - call_order.push('timeout ' + elapsed()); - callback(); - }, task_timeout); - }, 2); - - q.push(tasks); - - setTimeout(function () { - q.pause(); - expect(q.paused).to.equal(true); - }, pause_timeout); - - setTimeout(function () { - q.resume(); - expect(q.paused).to.equal(false); - }, resume_timeout); - - setTimeout(function () { - expect(q.running()).to.equal(2); - }, resume_timeout + 10); - - setTimeout(function () { - expect(call_order).to.eql([ - 'process 1', 'timeout ' + task_timeout, - 'process 2', 'timeout ' + task_timeout, - 'process 3', 'timeout ' + task_timeout * 4, - 'process 4', 'timeout ' + task_timeout * 4, - 'process 5', 'timeout ' + task_timeout * 5, - 'process 6', 'timeout ' + task_timeout * 5 - ]); - done(); - }, (task_timeout * tasks.length) + pause_timeout + resume_timeout); - }); - it('start paused', function(done) { var q = async.queue(function (task, callback) { setTimeout(function () { @@ -505,6 +454,7 @@ describe('queue', function(){ q.push([1, 2, 3]); setTimeout(function () { + expect(q.running()).to.equal(0); q.resume(); }, 5); -- cgit v1.2.1 From a21a58298aff375ff40f173faa53da3e7dc1fc29 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Sat, 8 Apr 2017 21:27:44 -0700 Subject: tweak retry tests to be more descriptive when they fail --- mocha_test/retry.js | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/mocha_test/retry.js b/mocha_test/retry.js index d3a5d22..6185f3c 100644 --- a/mocha_test/retry.js +++ b/mocha_test/retry.js @@ -66,11 +66,10 @@ describe("retry", function () { callCount++; callback(error + callCount, erroredResult + callCount); // respond with indexed values } - var start = new Date().getTime(); + var start = Date.now(); async.retry({ times: times, interval: interval}, fn, function(err, result){ - var now = new Date().getTime(); - var duration = now - start; - assert(duration >= (interval * (times -1)), 'did not include interval'); + var duration = Date.now() - start; + expect(duration).to.be.above(interval * (times - 1) - 1); assert.equal(callCount, 3, "did not retry the correct number of times"); assert.equal(err, error + times, "Incorrect error was returned"); assert.equal(result, erroredResult + times, "Incorrect result was returned"); @@ -88,11 +87,10 @@ describe("retry", function () { callCount++; callback(error + callCount, erroredResult + callCount); // respond with indexed values } - var start = new Date().getTime(); + var start = Date.now(); async.retry({ times: times, interval: intervalFunc}, fn, function(err, result){ - var now = new Date().getTime(); - var duration = now - start; - assert(duration >= 300, 'did not include custom interval'); + var duration = Date.now() - start; + expect(duration).to.be.above(299); assert.equal(callCount, 3, "did not retry the correct number of times"); assert.equal(err, error + times, "Incorrect error was returned"); assert.equal(result, erroredResult + times, "Incorrect result was returned"); @@ -127,7 +125,7 @@ describe("retry", function () { it('retry does not precompute the intervals (#1226)', function(done) { var callTimes = []; function intervalFunc() { - callTimes.push(new Date().getTime()); + callTimes.push(Date.now()); return 100; }; function fn(callback) { @@ -226,11 +224,10 @@ describe("retry", function () { function errorTest(err) { return err && err !== special; } - var start = new Date().getTime(); + var start = Date.now(); async.retry({ interval: interval, errorFilter: errorTest }, fn, function(err, result){ - var now = new Date().getTime(); - var duration = now - start; - assert(duration >= (interval * (specialCount - 1)), 'did not include interval'); + var duration = Date.now() - start; + expect(duration).to.be.above(interval * (specialCount - 1) - 1); assert.equal(callCount, specialCount, "did not retry the correct number of times"); assert.equal(err, special, "Incorrect error was returned"); assert.equal(result, erroredResult + specialCount, "Incorrect result was returned"); -- cgit v1.2.1