diff options
author | James M Snell <jasnell@gmail.com> | 2017-08-03 18:08:11 -0700 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2017-08-07 17:33:59 -0700 |
commit | 7192e913f703c96eddff697c4ba0daef87930776 (patch) | |
tree | bd9083ff6c7d1062285c80695b8b817af5b0c5bd /test/parallel | |
parent | af70c3b4781d8df928d183580b8075cd566ce0b5 (diff) | |
download | node-new-7192e913f703c96eddff697c4ba0daef87930776.tar.gz |
test: improve multiple timers tests
PR-URL: https://github.com/nodejs/node/pull/14616
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Diffstat (limited to 'test/parallel')
-rw-r--r-- | test/parallel/test-timers-immediate.js | 20 | ||||
-rw-r--r-- | test/parallel/test-timers-non-integer-delay.js | 12 | ||||
-rw-r--r-- | test/parallel/test-timers-socket-timeout-removes-other-socket-unref-timer.js | 26 | ||||
-rw-r--r-- | test/parallel/test-timers-unref-leak.js | 23 | ||||
-rw-r--r-- | test/parallel/test-timers-unref.js | 60 | ||||
-rw-r--r-- | test/parallel/test-timers-unrefd-interval-still-fires.js | 23 | ||||
-rw-r--r-- | test/parallel/test-timers-unrefed-in-beforeexit.js | 21 | ||||
-rw-r--r-- | test/parallel/test-timers-zero-timeout.js | 14 |
8 files changed, 68 insertions, 131 deletions
diff --git a/test/parallel/test-timers-immediate.js b/test/parallel/test-timers-immediate.js index f5e0fa00a7..0227e38efa 100644 --- a/test/parallel/test-timers-immediate.js +++ b/test/parallel/test-timers-immediate.js @@ -23,9 +23,6 @@ const common = require('../common'); const assert = require('assert'); -let immediateC; -let immediateD; - let mainFinished = false; setImmediate(common.mustCall(function() { @@ -35,17 +32,12 @@ setImmediate(common.mustCall(function() { const immediateB = setImmediate(common.mustNotCall()); -setImmediate(function(x, y, z) { - immediateC = [x, y, z]; -}, 1, 2, 3); - -setImmediate(function(x, y, z, a, b) { - immediateD = [x, y, z, a, b]; -}, 1, 2, 3, 4, 5); +setImmediate(common.mustCall((...args) => { + assert.deepStrictEqual(args, [1, 2, 3]); +}), 1, 2, 3); -process.on('exit', function() { - assert.deepStrictEqual(immediateC, [1, 2, 3], 'immediateC args should match'); - assert.deepStrictEqual(immediateD, [1, 2, 3, 4, 5], '5 args should match'); -}); +setImmediate(common.mustCall((...args) => { + assert.deepStrictEqual(args, [1, 2, 3, 4, 5]); +}), 1, 2, 3, 4, 5); mainFinished = true; diff --git a/test/parallel/test-timers-non-integer-delay.js b/test/parallel/test-timers-non-integer-delay.js index bda4a31d81..017ef28d0f 100644 --- a/test/parallel/test-timers-non-integer-delay.js +++ b/test/parallel/test-timers-non-integer-delay.js @@ -20,7 +20,7 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict'; -require('../common'); +const common = require('../common'); /* * This test makes sure that non-integer timer delays do not make the process @@ -39,13 +39,11 @@ require('../common'); */ const TIMEOUT_DELAY = 1.1; -const NB_TIMEOUTS_FIRED = 50; +let N = 50; -let nbTimeoutFired = 0; -const interval = setInterval(function() { - ++nbTimeoutFired; - if (nbTimeoutFired === NB_TIMEOUTS_FIRED) { +const interval = setInterval(common.mustCall(() => { + if (--N === 0) { clearInterval(interval); process.exit(0); } -}, TIMEOUT_DELAY); +}, N), TIMEOUT_DELAY); diff --git a/test/parallel/test-timers-socket-timeout-removes-other-socket-unref-timer.js b/test/parallel/test-timers-socket-timeout-removes-other-socket-unref-timer.js index 5d57bbaae0..842c818070 100644 --- a/test/parallel/test-timers-socket-timeout-removes-other-socket-unref-timer.js +++ b/test/parallel/test-timers-socket-timeout-removes-other-socket-unref-timer.js @@ -6,6 +6,7 @@ const common = require('../common'); const net = require('net'); +const Countdown = require('../common/countdown'); const clients = []; @@ -19,7 +20,7 @@ const server = net.createServer(function onClient(client) { * the list of unref timers when traversing it, and exposes the * original issue in joyent/node#8897. */ - clients[0].setTimeout(1, function onTimeout() { + clients[0].setTimeout(1, () => { clients[1].setTimeout(0); clients[0].end(); clients[1].end(); @@ -31,19 +32,16 @@ const server = net.createServer(function onClient(client) { } }); -server.listen(0, common.localhostIPv4, function() { - let nbClientsEnded = 0; +server.listen(0, common.localhostIPv4, common.mustCall(() => { + const countdown = new Countdown(2, common.mustCall(() => server.close())); - function addEndedClient(client) { - ++nbClientsEnded; - if (nbClientsEnded === 2) { - server.close(); - } + { + const client = net.connect({ port: server.address().port }); + client.on('end', () => countdown.dec()); } - const client1 = net.connect({ port: this.address().port }); - client1.on('end', addEndedClient); - - const client2 = net.connect({ port: this.address().port }); - client2.on('end', addEndedClient); -}); + { + const client = net.connect({ port: server.address().port }); + client.on('end', () => countdown.dec()); + } +})); diff --git a/test/parallel/test-timers-unref-leak.js b/test/parallel/test-timers-unref-leak.js index 8eef00dd4f..afecf7f15c 100644 --- a/test/parallel/test-timers-unref-leak.js +++ b/test/parallel/test-timers-unref-leak.js @@ -1,27 +1,14 @@ 'use strict'; -require('../common'); -const assert = require('assert'); +const common = require('../common'); -let called = 0; -let closed = 0; - -const timeout = setTimeout(function() { - called++; -}, 10); +const timeout = setTimeout(common.mustCall(), 10); timeout.unref(); // Wrap `close` method to check if the handle was closed const close = timeout._handle.close; -timeout._handle.close = function() { - closed++; +timeout._handle.close = common.mustCall(function() { return close.apply(this, arguments); -}; +}); // Just to keep process alive and let previous timer's handle die -setTimeout(function() { -}, 50); - -process.on('exit', function() { - assert.strictEqual(called, 1); - assert.strictEqual(closed, 1); -}); +setTimeout(() => {}, 50); diff --git a/test/parallel/test-timers-unref.js b/test/parallel/test-timers-unref.js index 7f66a364e3..0078d2dae3 100644 --- a/test/parallel/test-timers-unref.js +++ b/test/parallel/test-timers-unref.js @@ -21,60 +21,55 @@ 'use strict'; -require('../common'); +const common = require('../common'); const assert = require('assert'); -let interval_fired = false; -let timeout_fired = false; let unref_interval = false; let unref_timer = false; -let unref_callbacks = 0; let checks = 0; const LONG_TIME = 10 * 1000; const SHORT_TIME = 100; -assert.doesNotThrow(function() { +assert.doesNotThrow(() => { setTimeout(() => {}, 10).unref().ref().unref(); }, 'ref and unref are chainable'); -assert.doesNotThrow(function() { +assert.doesNotThrow(() => { setInterval(() => {}, 10).unref().ref().unref(); }, 'ref and unref are chainable'); -setInterval(function() { - interval_fired = true; -}, LONG_TIME).unref(); +setInterval(common.mustNotCall('Interval should not fire'), LONG_TIME).unref(); +setTimeout(common.mustNotCall('Timer should not fire'), LONG_TIME).unref(); -setTimeout(function() { - timeout_fired = true; -}, LONG_TIME).unref(); - -const interval = setInterval(function() { +const interval = setInterval(common.mustCall(() => { unref_interval = true; clearInterval(interval); -}, SHORT_TIME); +}), SHORT_TIME); interval.unref(); -setTimeout(function() { +setTimeout(common.mustCall(() => { unref_timer = true; -}, SHORT_TIME).unref(); +}), SHORT_TIME).unref(); -const check_unref = setInterval(function() { +const check_unref = setInterval(() => { if (checks > 5 || (unref_interval && unref_timer)) clearInterval(check_unref); checks += 1; }, 100); -setTimeout(function() { - unref_callbacks++; - this.unref(); -}, SHORT_TIME); +{ + const timeout = + setTimeout(common.mustCall(() => { + timeout.unref(); + }), SHORT_TIME); +} -// Should not timeout the test -setInterval(function() { - this.unref(); -}, SHORT_TIME); +{ + // Should not timeout the test + const timeout = + setInterval(() => timeout.unref(), SHORT_TIME); +} // Should not assert on args.Holder()->InternalFieldCount() > 0. See #4261. { @@ -82,16 +77,3 @@ setInterval(function() { process.nextTick(t.unref.bind({})); process.nextTick(t.unref.bind(t)); } - -process.on('exit', function() { - assert.strictEqual(interval_fired, false, - 'Interval should not fire'); - assert.strictEqual(timeout_fired, false, - 'Timeout should not fire'); - assert.strictEqual(unref_timer, true, - 'An unrefd timeout should still fire'); - assert.strictEqual(unref_interval, true, - 'An unrefd interval should still fire'); - assert.strictEqual(unref_callbacks, 1, - 'Callback should only run once'); -}); diff --git a/test/parallel/test-timers-unrefd-interval-still-fires.js b/test/parallel/test-timers-unrefd-interval-still-fires.js index bf16013f00..a9e9af8430 100644 --- a/test/parallel/test-timers-unrefd-interval-still-fires.js +++ b/test/parallel/test-timers-unrefd-interval-still-fires.js @@ -5,23 +5,20 @@ const common = require('../common'); const TEST_DURATION = common.platformTimeout(1000); -const N = 3; -let nbIntervalFired = 0; +let N = 3; -const keepOpen = setTimeout(() => { - console.error('[FAIL] Interval fired %d/%d times.', nbIntervalFired, N); - throw new Error('Test timed out. keepOpen was not canceled.'); -}, TEST_DURATION); +const keepOpen = + setTimeout( + common.mustNotCall('Test timed out. keepOpen was not canceled.'), + TEST_DURATION); -const timer = setInterval(() => { - ++nbIntervalFired; - if (nbIntervalFired === N) { +const timer = setInterval(common.mustCall(() => { + if (--N === 0) { clearInterval(timer); - timer._onTimeout = () => { - throw new Error('Unrefd interval fired after being cleared.'); - }; + timer._onTimeout = + common.mustNotCall('Unrefd interal fired after being cleared'); clearTimeout(keepOpen); } -}, 1); +}, N), 1); timer.unref(); diff --git a/test/parallel/test-timers-unrefed-in-beforeexit.js b/test/parallel/test-timers-unrefed-in-beforeexit.js index 530d97674d..a38b55bf45 100644 --- a/test/parallel/test-timers-unrefed-in-beforeexit.js +++ b/test/parallel/test-timers-unrefed-in-beforeexit.js @@ -1,20 +1,7 @@ 'use strict'; -require('../common'); -const assert = require('assert'); +const common = require('../common'); -let once = 0; - -process.on('beforeExit', () => { - if (once > 1) - throw new RangeError('beforeExit should only have been called once!'); - - setTimeout(() => {}, 1).unref(); - once++; -}); - -process.on('exit', (code) => { - if (code !== 0) return; - - assert.strictEqual(once, 1); -}); +process.on('beforeExit', common.mustCall(() => { + setTimeout(common.mustNotCall(), 1).unref(); +})); diff --git a/test/parallel/test-timers-zero-timeout.js b/test/parallel/test-timers-zero-timeout.js index 7feb01854e..61a5b2131b 100644 --- a/test/parallel/test-timers-zero-timeout.js +++ b/test/parallel/test-timers-zero-timeout.js @@ -36,18 +36,14 @@ const assert = require('assert'); } { - let ncalled = 0; + let ncalled = 3; - const iv = setInterval(f, 0, 'foo', 'bar', 'baz'); - - function f(a, b, c) { + const f = common.mustCall((a, b, c) => { assert.strictEqual(a, 'foo'); assert.strictEqual(b, 'bar'); assert.strictEqual(c, 'baz'); - if (++ncalled === 3) clearTimeout(iv); - } + if (--ncalled === 0) clearTimeout(iv); + }, ncalled); - process.on('exit', function() { - assert.strictEqual(ncalled, 3); - }); + const iv = setInterval(f, 0, 'foo', 'bar', 'baz'); } |