From e7af9830e98fcda7e7a11e0b13b667163cc8c940 Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Thu, 13 Sep 2018 07:35:15 -0700 Subject: timers: run nextTicks after each immediate and timer In order to better match the browser behaviour, run nextTicks (and subsequently the microtask queue) after each individual Timer and Immediate, rather than after the whole list is processed. The current behaviour is somewhat of a performance micro-optimization and also partly dictated by how timer handles were implemented. PR-URL: https://github.com/nodejs/node/pull/22842 Fixes: https://github.com/nodejs/node/issues/22257 Reviewed-By: Anna Henningsen Reviewed-By: Gus Caplan Reviewed-By: Jeremiah Senkpiel Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater --- test/parallel/test-timers-next-tick.js | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'test/parallel/test-timers-next-tick.js') diff --git a/test/parallel/test-timers-next-tick.js b/test/parallel/test-timers-next-tick.js index 1db1d18c3a..89ebf6c8c1 100644 --- a/test/parallel/test-timers-next-tick.js +++ b/test/parallel/test-timers-next-tick.js @@ -2,14 +2,29 @@ const common = require('../common'); -// This test documents an internal implementation detail of the Timers: -// since timers of different durations are stored in separate lists, -// a nextTick queue will clear after each list of timers. While this -// behaviour is not documented it could be relied on by Node's users. +// This test verifies that the next tick queue runs after each +// individual Timeout, as well as each individual Immediate. setTimeout(common.mustCall(() => { - process.nextTick(() => { clearTimeout(t2); }); + process.nextTick(() => { + // Confirm that clearing Timeouts from a next tick doesn't explode. + clearTimeout(t2); + clearTimeout(t3); + }); }), 1); -const t2 = setTimeout(common.mustNotCall(), 2); +const t2 = setTimeout(common.mustNotCall(), 1); +const t3 = setTimeout(common.mustNotCall(), 1); +setTimeout(common.mustCall(), 1); common.busyLoop(5); + +setImmediate(common.mustCall(() => { + process.nextTick(() => { + // Confirm that clearing Immediates from a next tick doesn't explode. + clearImmediate(i2); + clearImmediate(i3); + }); +})); +const i2 = setImmediate(common.mustNotCall()); +const i3 = setImmediate(common.mustNotCall()); +setImmediate(common.mustCall()); -- cgit v1.2.3