Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
58 changes: 53 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,18 @@ class MockTimers {
);
}

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

#restoreOriginalAbortSignalTimeout() {
if (this.#realAbortSignalTimeout) {
ObjectDefineProperty(AbortSignal, 'timeout', this.#realAbortSignalTimeout);
} else {
delete AbortSignal.timeout;
}
}
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 +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) {
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 +666,9 @@ class MockTimers {
'Date': () => {
ObjectDefineProperty(globalThis, 'Date', this.#nativeDateDescriptor);
},
'AbortSignal.timeout': () => {
this.#restoreOriginalAbortSignalTimeout();
},
},
};

Expand Down Expand Up @@ -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]
Expand All @@ -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(
Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-mock-timers-abortsignal-timeout.js
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();
}
}
Comment thread
Renegade334 marked this conversation as resolved.
Outdated
Loading