From 876e86c6311eb2ec31941e474ee38bf227541a40 Mon Sep 17 00:00:00 2001 From: Christian Aurich Zanettini Martins Date: Thu, 3 Dec 2026 00:49:12 -0300 Subject: [PATCH] test: deflake test-util-throttle The abort listener check expects the second call to a throttle with a 10 ms window to be queued. If the process is descheduled for 10 ms between the two calls, the window has already expired and the second one runs right away without adding the listener. Move the check to test-util-throttle-timing.js, where the timers and the libuv clock are mocked. Signed-off-by: Christian Aurich Zanettini Martins --- test/parallel/test-util-throttle-timing.js | 16 ++++++++++++++++ test/parallel/test-util-throttle.js | 14 -------------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/test/parallel/test-util-throttle-timing.js b/test/parallel/test-util-throttle-timing.js index 56b6c7716a92..28d0c1f95a9f 100644 --- a/test/parallel/test-util-throttle-timing.js +++ b/test/parallel/test-util-throttle-timing.js @@ -3,6 +3,7 @@ const common = require('../common'); const assert = require('node:assert'); +const { listenerCount } = require('node:events'); const { mock } = require('node:test'); const { setImmediate } = require('node:timers/promises'); const { internalBinding } = require('internal/test/binding'); @@ -167,4 +168,19 @@ process.on('unhandledRejection', common.mustNotCall()); assert.deepStrictEqual(await Promise.all([first, recursive]), [1, 2]); assert.deepStrictEqual(values, [1, 2]); } + + { + const { signal } = new AbortController(); + const throttled = throttle(common.mustCall((value) => value, 2), 1, 10, { + signal, + }); + assert.strictEqual(listenerCount(signal, 'abort'), 0); + const first = throttled(1); + assert.strictEqual(listenerCount(signal, 'abort'), 0); + const second = throttled(2); + assert.strictEqual(listenerCount(signal, 'abort'), 1); + mock.timers.tick(10); + assert.deepStrictEqual(await Promise.all([first, second]), [1, 2]); + assert.strictEqual(listenerCount(signal, 'abort'), 0); + } })().then(common.mustCall()).finally(() => mock.reset()); diff --git a/test/parallel/test-util-throttle.js b/test/parallel/test-util-throttle.js index f03c5ef0da7d..8eb2ce2d1470 100644 --- a/test/parallel/test-util-throttle.js +++ b/test/parallel/test-util-throttle.js @@ -262,20 +262,6 @@ throttle(() => {}, 1, 1, { ); } - { - const { signal } = new AbortController(); - const throttled = throttle(common.mustCall((value) => value, 2), 1, 10, { - signal, - }); - assert.strictEqual(abortListenerCount(signal), 0); - const first = throttled(1); - assert.strictEqual(abortListenerCount(signal), 0); - const second = throttled(2); - assert.strictEqual(abortListenerCount(signal), 1); - assert.deepStrictEqual(await Promise.all([first, second]), [1, 2]); - assert.strictEqual(abortListenerCount(signal), 0); - } - { const { signal } = new AbortController(); const throttled = throttle(common.mustCall(), 1, TIMEOUT_MAX, { signal });