Skip to content

Commit 876e86c

Browse files
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 <christian.aurichzm@gmail.com>
1 parent 9873287 commit 876e86c

2 files changed

Lines changed: 16 additions & 14 deletions

File tree

‎test/parallel/test-util-throttle-timing.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
const common = require('../common');
55
const assert = require('node:assert');
6+
const { listenerCount } = require('node:events');
67
const { mock } = require('node:test');
78
const { setImmediate } = require('node:timers/promises');
89
const { internalBinding } = require('internal/test/binding');
@@ -167,4 +168,19 @@ process.on('unhandledRejection', common.mustNotCall());
167168
assert.deepStrictEqual(await Promise.all([first, recursive]), [1, 2]);
168169
assert.deepStrictEqual(values, [1, 2]);
169170
}
171+
172+
{
173+
const { signal } = new AbortController();
174+
const throttled = throttle(common.mustCall((value) => value, 2), 1, 10, {
175+
signal,
176+
});
177+
assert.strictEqual(listenerCount(signal, 'abort'), 0);
178+
const first = throttled(1);
179+
assert.strictEqual(listenerCount(signal, 'abort'), 0);
180+
const second = throttled(2);
181+
assert.strictEqual(listenerCount(signal, 'abort'), 1);
182+
mock.timers.tick(10);
183+
assert.deepStrictEqual(await Promise.all([first, second]), [1, 2]);
184+
assert.strictEqual(listenerCount(signal, 'abort'), 0);
185+
}
170186
})().then(common.mustCall()).finally(() => mock.reset());

‎test/parallel/test-util-throttle.js‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -262,20 +262,6 @@ throttle(() => {}, 1, 1, {
262262
);
263263
}
264264

265-
{
266-
const { signal } = new AbortController();
267-
const throttled = throttle(common.mustCall((value) => value, 2), 1, 10, {
268-
signal,
269-
});
270-
assert.strictEqual(abortListenerCount(signal), 0);
271-
const first = throttled(1);
272-
assert.strictEqual(abortListenerCount(signal), 0);
273-
const second = throttled(2);
274-
assert.strictEqual(abortListenerCount(signal), 1);
275-
assert.deepStrictEqual(await Promise.all([first, second]), [1, 2]);
276-
assert.strictEqual(abortListenerCount(signal), 0);
277-
}
278-
279265
{
280266
const { signal } = new AbortController();
281267
const throttled = throttle(common.mustCall(), 1, TIMEOUT_MAX, { signal });

0 commit comments

Comments
 (0)