-
-
Notifications
You must be signed in to change notification settings - Fork 37.3k
test: deflake util.throttle tests #66034
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nodejs-github-bot
merged 1 commit into
nodejs:main
from
panva:deflake-util-debounce-throttle
Sep 15, 2026
+181
−113
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| // Flags: --expose-internals | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const assert = require('node:assert'); | ||
| const { mock } = require('node:test'); | ||
| const { setImmediate } = require('node:timers/promises'); | ||
| const { internalBinding } = require('internal/test/binding'); | ||
|
|
||
| // Throttle uses the libuv clock as well as timers. Control both before loading | ||
| // the implementation, which captures setTimeout and clearTimeout. | ||
| mock.timers.enable({ apis: ['Date', 'setTimeout'] }); | ||
| mock.method(internalBinding('timers'), 'getLibuvNow', () => Date.now()); | ||
| const { throttle } = require('node:util'); | ||
|
|
||
| process.on('unhandledRejection', common.mustNotCall()); | ||
|
|
||
| (async () => { | ||
| { | ||
| const values = []; | ||
| const times = []; | ||
| const start = Date.now(); | ||
| const throttled = throttle(common.mustCall(function(value) { | ||
| assert.strictEqual(this, throttled); | ||
| values.push(value); | ||
| times.push(Date.now() - start); | ||
| return value; | ||
| }, 5), 2, 40); | ||
|
|
||
| assert.strictEqual(typeof throttled.cancel, 'function'); | ||
| assert.strictEqual(typeof throttled.hasImmediateCapacity, 'function'); | ||
| assert.strictEqual(typeof throttled.ref, 'function'); | ||
| assert.strictEqual(typeof throttled.unref, 'function'); | ||
| assert.strictEqual(throttled.pending, null); | ||
| assert.strictEqual(throttled.pendingCount, 0); | ||
| assert.strictEqual(throttled.activeCount, 0); | ||
| assert.strictEqual(throttled.hasImmediateCapacity(), true); | ||
|
|
||
| const first = throttled(1); | ||
| const second = throttled(2); | ||
| const third = throttled(3); | ||
| const fourth = throttled(4); | ||
| const fifth = throttled(5); | ||
|
|
||
| assert(first instanceof Promise); | ||
| assert(second instanceof Promise); | ||
| assert.notStrictEqual(first, second); | ||
| assert.deepStrictEqual(values, [1, 2]); | ||
| assert.strictEqual(throttled.hasImmediateCapacity(), false); | ||
| assert.strictEqual(throttled.pending, fifth); | ||
| assert.strictEqual(throttled.pendingCount, 3); | ||
| assert.strictEqual(throttled.unref(), throttled); | ||
| assert.strictEqual(throttled.ref(), throttled); | ||
|
|
||
| mock.timers.tick(39); | ||
| assert.deepStrictEqual(values, [1, 2]); | ||
| mock.timers.tick(1); | ||
| assert.deepStrictEqual(values, [1, 2, 3, 4]); | ||
| assert.strictEqual(throttled.pendingCount, 1); | ||
| mock.timers.tick(39); | ||
| assert.deepStrictEqual(values, [1, 2, 3, 4]); | ||
| mock.timers.tick(1); | ||
|
|
||
| assert.deepStrictEqual( | ||
| await Promise.all([first, second, third, fourth, fifth]), | ||
| [1, 2, 3, 4, 5], | ||
| ); | ||
| assert.deepStrictEqual(values, [1, 2, 3, 4, 5]); | ||
| assert.strictEqual(throttled.pending, null); | ||
| assert.strictEqual(throttled.pendingCount, 0); | ||
| assert.strictEqual(throttled.activeCount, 0); | ||
| assert.deepStrictEqual(times, [0, 0, 40, 40, 80]); | ||
| } | ||
|
|
||
| { | ||
| const values = []; | ||
| const throttled = throttle(common.mustCall((value) => { | ||
| values.push(value); | ||
| return value; | ||
| }, 2), 1, 20, { maxPending: 1 }); | ||
| const first = throttled(1); | ||
| const second = throttled(2); | ||
| const dropped = throttled(3); | ||
|
|
||
| assert.deepStrictEqual(values, [1]); | ||
| assert.strictEqual(throttled.pendingCount, 1); | ||
| await setImmediate(); | ||
| await assert.rejects(dropped, { code: 'ERR_THROTTLED' }); | ||
| mock.timers.tick(19); | ||
| assert.deepStrictEqual(values, [1]); | ||
| mock.timers.tick(1); | ||
| assert.deepStrictEqual(await Promise.all([first, second]), [1, 2]); | ||
| assert.deepStrictEqual(values, [1, 2]); | ||
| } | ||
|
|
||
| { | ||
| const values = []; | ||
| const throttled = throttle(common.mustCall((value) => { | ||
| values.push(value); | ||
| return value; | ||
| }, 3), 2, 30, { overflow: 'drop' }); | ||
| const first = throttled(1); | ||
| const second = throttled(2); | ||
| await assert.rejects(throttled(3), { code: 'ERR_THROTTLED' }); | ||
| assert.deepStrictEqual(await Promise.all([first, second]), [1, 2]); | ||
|
|
||
| mock.timers.tick(29); | ||
| assert.strictEqual(throttled.hasImmediateCapacity(), false); | ||
| await assert.rejects(throttled(4), { code: 'ERR_THROTTLED' }); | ||
| mock.timers.tick(1); | ||
| assert.strictEqual(throttled.hasImmediateCapacity(), true); | ||
| assert.strictEqual(await throttled(5), 5); | ||
| assert.deepStrictEqual(values, [1, 2, 5]); | ||
| } | ||
|
|
||
| // A rolling window releases one slot at a time when timers run on schedule. | ||
| // If dispatch is delayed until both slots expire, both calls may run together. | ||
| for (const delayed of [false, true]) { | ||
| const times = []; | ||
| const start = Date.now(); | ||
| const throttled = throttle(common.mustCall((value) => { | ||
| times.push(Date.now() - start); | ||
| return value; | ||
| }, 4), 2, 80, { strict: true }); | ||
|
|
||
| const first = throttled(1); | ||
| mock.timers.tick(40); | ||
| const second = throttled(2); | ||
| const third = throttled(3); | ||
| const fourth = throttled(4); | ||
|
|
||
| mock.timers.tick(39); | ||
| assert.deepStrictEqual(times, [0, 40]); | ||
| if (delayed) { | ||
| mock.timers.tick(41); | ||
| } else { | ||
| mock.timers.tick(1); | ||
| assert.deepStrictEqual(times, [0, 40, 80]); | ||
| mock.timers.tick(39); | ||
| assert.deepStrictEqual(times, [0, 40, 80]); | ||
| mock.timers.tick(1); | ||
| } | ||
|
|
||
| assert.deepStrictEqual( | ||
| await Promise.all([first, second, third, fourth]), | ||
| [1, 2, 3, 4], | ||
| ); | ||
| assert.deepStrictEqual(times, delayed ? [0, 40, 120, 120] : [0, 40, 80, 120]); | ||
| } | ||
|
|
||
| { | ||
| let recursive; | ||
| const values = []; | ||
| const throttled = throttle(common.mustCall((value) => { | ||
| values.push(value); | ||
| if (value === 1) recursive = throttled(2); | ||
| return value; | ||
| }, 2), 1, 20); | ||
|
|
||
| const first = throttled(1); | ||
| assert.deepStrictEqual(values, [1]); | ||
| assert.strictEqual(throttled.pending, recursive); | ||
| assert.strictEqual(throttled.pendingCount, 1); | ||
| mock.timers.tick(19); | ||
| assert.deepStrictEqual(values, [1]); | ||
| mock.timers.tick(1); | ||
| assert.deepStrictEqual(await Promise.all([first, recursive]), [1, 2]); | ||
| assert.deepStrictEqual(values, [1, 2]); | ||
| } | ||
| })().then(common.mustCall()).finally(() => mock.reset()); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.