Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 49 additions & 5 deletions lib/internal/test_runner/mock/mock_timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const {
validateAbortSignal,
validateNumber,
validateStringArray,
validateUint32,
} = require('internal/validators');

const {
Expand All @@ -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');

Expand All @@ -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,
Expand Down Expand Up @@ -115,6 +125,7 @@ class MockTimers {
#realPromisifiedSetImmediate;

#nativeDateDescriptor;
#realAbortSignalTimeout;

#timersInContext = [];
#isEnabled = false;
Expand Down Expand Up @@ -297,6 +308,14 @@ class MockTimers {
);
}

#storeOriginalAbortSignalTimeout() {
this.#realAbortSignalTimeout = ObjectGetOwnPropertyDescriptor(AbortSignal, 'timeout');
}

#restoreOriginalAbortSignalTimeout() {
ObjectDefineProperty(AbortSignal, 'timeout', this.#realAbortSignalTimeout);
}
Comment thread
Renegade334 marked this conversation as resolved.

#createTimer(isInterval, callback, delay, ...args) {
if (delay > TIMEOUT_MAX) {
delay = 1;
Expand Down Expand Up @@ -604,6 +623,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) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
value(delay) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the current looks a bit weird.

Suggested change
mock.#setTimeout(
() => controller.abort,
delay,
);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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,
Expand All @@ -622,6 +662,9 @@ class MockTimers {
'Date': () => {
ObjectDefineProperty(globalThis, 'Date', this.#nativeDateDescriptor);
},
'AbortSignal.timeout': () => {
this.#restoreOriginalAbortSignalTimeout();
},
},
};

Expand Down Expand Up @@ -664,10 +707,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]
Expand All @@ -686,8 +730,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(
Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-mock-timers-abortsignal-timeout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

require('../common');
const assert = require('assert');
const { mock } = require('node:test');

const originalAbortSignalTimeout = AbortSignal.timeout;

Check failure on line 7 in test/parallel/test-mock-timers-abortsignal-timeout.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected linebreaks to be 'LF' but found 'CRLF'

Check failure on line 8 in test/parallel/test-mock-timers-abortsignal-timeout.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected linebreaks to be 'LF' but found 'CRLF'
mock.timers.enable({ apis: ['AbortSignal.timeout'] });

Check failure on line 9 in test/parallel/test-mock-timers-abortsignal-timeout.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected linebreaks to be 'LF' but found 'CRLF'

Check failure on line 10 in test/parallel/test-mock-timers-abortsignal-timeout.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected linebreaks to be 'LF' but found 'CRLF'
const signal = AbortSignal.timeout(50);

Check failure on line 11 in test/parallel/test-mock-timers-abortsignal-timeout.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected linebreaks to be 'LF' but found 'CRLF'
assert.strictEqual(signal.aborted, false);

Check failure on line 12 in test/parallel/test-mock-timers-abortsignal-timeout.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected linebreaks to be 'LF' but found 'CRLF'

Check failure on line 13 in test/parallel/test-mock-timers-abortsignal-timeout.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected linebreaks to be 'LF' but found 'CRLF'
mock.timers.tick(49);

Check failure on line 14 in test/parallel/test-mock-timers-abortsignal-timeout.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected linebreaks to be 'LF' but found 'CRLF'
assert.strictEqual(signal.aborted, false);

Check failure on line 15 in test/parallel/test-mock-timers-abortsignal-timeout.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected linebreaks to be 'LF' but found 'CRLF'

Check failure on line 16 in test/parallel/test-mock-timers-abortsignal-timeout.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected linebreaks to be 'LF' but found 'CRLF'
mock.timers.tick(1);
assert.strictEqual(signal.aborted, true);

mock.timers.reset();
assert.strictEqual(AbortSignal.timeout, originalAbortSignalTimeout);
Loading