-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
test_runner: add mock-timers support for AbortSignal.timeout #60751
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
Changes from 2 commits
ef06137
fb02de7
9ffc42d
177ab11
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -23,6 +23,7 @@ const { | |||||||||||
| validateAbortSignal, | ||||||||||||
| validateNumber, | ||||||||||||
| validateStringArray, | ||||||||||||
| validateUint32, | ||||||||||||
| } = require('internal/validators'); | ||||||||||||
|
|
||||||||||||
| const { | ||||||||||||
|
|
@@ -34,6 +35,7 @@ const { | |||||||||||
| } = require('internal/errors'); | ||||||||||||
|
|
||||||||||||
| const { addAbortListener } = require('internal/events/abort_listener'); | ||||||||||||
| const { AbortController, AbortSignal } = require('internal/abort_controller'); | ||||||||||||
|
|
||||||||||||
| const { TIMEOUT_MAX } = require('internal/timers'); | ||||||||||||
|
|
||||||||||||
|
|
@@ -60,9 +62,17 @@ function abortIt(signal) { | |||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * @enum {('setTimeout'|'setInterval'|'setImmediate'|'Date'|'scheduler.wait')[]} Supported timers | ||||||||||||
| * @typedef {('setTimeout'|'setInterval'|'setImmediate'|'Date'|'scheduler.wait'|'AbortSignal.timeout')[]} SupportedApis | ||||||||||||
| * Supported timers that can be enabled via MockTimers.enable({ apis: [...] }) | ||||||||||||
| */ | ||||||||||||
| const SUPPORTED_APIS = ['setTimeout', 'setInterval', 'setImmediate', 'Date', 'scheduler.wait']; | ||||||||||||
| const SUPPORTED_APIS = [ | ||||||||||||
| 'setTimeout', | ||||||||||||
| 'setInterval', | ||||||||||||
| 'setImmediate', | ||||||||||||
| 'Date', | ||||||||||||
| 'scheduler.wait', | ||||||||||||
| 'AbortSignal.timeout', | ||||||||||||
| ]; | ||||||||||||
| const TIMERS_DEFAULT_INTERVAL = { | ||||||||||||
| __proto__: null, | ||||||||||||
| setImmediate: -1, | ||||||||||||
|
|
@@ -115,6 +125,7 @@ class MockTimers { | |||||||||||
| #realPromisifiedSetImmediate; | ||||||||||||
|
|
||||||||||||
| #nativeDateDescriptor; | ||||||||||||
| #realAbortSignalTimeout; | ||||||||||||
|
|
||||||||||||
| #timersInContext = []; | ||||||||||||
| #isEnabled = false; | ||||||||||||
|
|
@@ -297,6 +308,18 @@ class MockTimers { | |||||||||||
| ); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| #storeOriginalAbortSignalTimeout() { | ||||||||||||
| this.#realAbortSignalTimeout = ObjectGetOwnPropertyDescriptor(AbortSignal, 'timeout'); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| #restoreOriginalAbortSignalTimeout() { | ||||||||||||
| if (this.#realAbortSignalTimeout) { | ||||||||||||
| ObjectDefineProperty(AbortSignal, 'timeout', this.#realAbortSignalTimeout); | ||||||||||||
| } else { | ||||||||||||
| delete AbortSignal.timeout; | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| #createTimer(isInterval, callback, delay, ...args) { | ||||||||||||
| if (delay > TIMEOUT_MAX) { | ||||||||||||
| delay = 1; | ||||||||||||
|
|
@@ -604,6 +627,27 @@ class MockTimers { | |||||||||||
| this.#nativeDateDescriptor = ObjectGetOwnPropertyDescriptor(globalThis, 'Date'); | ||||||||||||
| globalThis.Date = this.#createDate(); | ||||||||||||
| }, | ||||||||||||
| 'AbortSignal.timeout': () => { | ||||||||||||
| this.#storeOriginalAbortSignalTimeout(); | ||||||||||||
| const mock = this; | ||||||||||||
| ObjectDefineProperty(AbortSignal, 'timeout', { | ||||||||||||
| __proto__: null, | ||||||||||||
| configurable: true, | ||||||||||||
| writable: true, | ||||||||||||
| value: function value(delay) { | ||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like GH is have a wee stroke with the diffs |
||||||||||||
| validateUint32(delay, 'delay', false); | ||||||||||||
| const controller = new AbortController(); | ||||||||||||
| // Don't keep an unused binding to the timer; mock tick controls it | ||||||||||||
| mock.#setTimeout( | ||||||||||||
| () => { | ||||||||||||
| controller.abort(); | ||||||||||||
| }, | ||||||||||||
| delay, | ||||||||||||
| ); | ||||||||||||
|
Comment on lines
637
to
642
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: the current looks a bit weird.
Suggested change
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like GH is have a wee stroke with the diffs |
||||||||||||
| return controller.signal; | ||||||||||||
| }, | ||||||||||||
| }); | ||||||||||||
| }, | ||||||||||||
| }, | ||||||||||||
| toReal: { | ||||||||||||
| '__proto__': null, | ||||||||||||
|
|
@@ -622,6 +666,9 @@ class MockTimers { | |||||||||||
| 'Date': () => { | ||||||||||||
| ObjectDefineProperty(globalThis, 'Date', this.#nativeDateDescriptor); | ||||||||||||
| }, | ||||||||||||
| 'AbortSignal.timeout': () => { | ||||||||||||
| this.#restoreOriginalAbortSignalTimeout(); | ||||||||||||
| }, | ||||||||||||
| }, | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
|
|
@@ -664,10 +711,11 @@ class MockTimers { | |||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * @typedef {{apis: SUPPORTED_APIS;now: number | Date;}} EnableOptions Options to enable the timers | ||||||||||||
| * @property {SUPPORTED_APIS} apis List of timers to enable, defaults to all | ||||||||||||
| * @typedef {{apis: SupportedApis;now: number | Date;}} EnableOptions Options to enable the timers | ||||||||||||
| * @property {SupportedApis} apis List of timers to enable, defaults to all | ||||||||||||
| * @property {number | Date} now The epoch to which the timers should be set to, defaults to 0 | ||||||||||||
| */ | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Enables the MockTimers replacing the native timers with the fake ones. | ||||||||||||
| * @param {EnableOptions} [options] | ||||||||||||
|
|
@@ -686,8 +734,8 @@ class MockTimers { | |||||||||||
|
|
||||||||||||
| internalOptions.apis ||= SUPPORTED_APIS; | ||||||||||||
|
|
||||||||||||
| validateStringArray(internalOptions.apis, 'options.apis'); | ||||||||||||
| // Check that the timers passed are supported | ||||||||||||
| validateStringArray(internalOptions.apis, 'options.apis'); | ||||||||||||
| ArrayPrototypeForEach(internalOptions.apis, (timer) => { | ||||||||||||
| if (!ArrayPrototypeIncludes(SUPPORTED_APIS, timer)) { | ||||||||||||
| throw new ERR_INVALID_ARG_VALUE( | ||||||||||||
|
|
||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| 'use strict'; | ||
|
|
||
| require('../common'); | ||
| const assert = require('assert'); | ||
| const { mock } = require('node:test'); | ||
|
|
||
| { | ||
| mock.timers.enable({ apis: ['AbortSignal.timeout'] }); | ||
|
|
||
| try { | ||
| const signal = AbortSignal.timeout(50); | ||
|
|
||
| assert.strictEqual(signal.aborted, false); | ||
|
|
||
| mock.timers.tick(49); | ||
| assert.strictEqual(signal.aborted, false); | ||
|
|
||
| mock.timers.tick(1); | ||
| assert.strictEqual(signal.aborted, true); | ||
| } finally { | ||
| mock.timers.reset(); | ||
| } | ||
| } | ||
|
Renegade334 marked this conversation as resolved.
Outdated
|
||
Uh oh!
There was an error while loading. Please reload this page.