From 4ff16585f6f1165ee40194473be6adab12df75f8 Mon Sep 17 00:00:00 2001 From: creestor Date: Sun, 4 Sep 2022 18:10:04 +0400 Subject: [PATCH 1/3] Fixed timer, added max avaliable timeout value --- lib/api.js | 3 ++- lib/now-and-timers.cjs | 11 ++++++++++- lib/test.js | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/api.js b/lib/api.js index dcab0fab9..c61e70342 100644 --- a/lib/api.js +++ b/lib/api.js @@ -19,6 +19,7 @@ import {getApplicableLineNumbers} from './line-numbers.js'; import {observeWorkerProcess} from './plugin-support/shared-workers.js'; import RunStatus from './run-status.js'; import scheduler from './scheduler.js'; +import nowAndTimers from './now-and-timers.cjs'; import serializeError from './serialize-error.js'; function resolveModules(modules) { @@ -52,7 +53,7 @@ class TimeoutTrigger { debounce() { if (this.timer === undefined) { - this.timer = setTimeout(() => this.trigger(), this.waitMs); + this.timer = nowAndTimers.setSafeTimeout(() => this.trigger(), this.waitMs); } else { this.timer.refresh(); } diff --git a/lib/now-and-timers.cjs b/lib/now-and-timers.cjs index 2576d7ef9..4e4cf1954 100644 --- a/lib/now-and-timers.cjs +++ b/lib/now-and-timers.cjs @@ -1,5 +1,14 @@ 'use strict'; const timers = require('timers'); -Object.assign(exports, timers); +// Slightly higher than 24 days +const MAX_INT32 = Math.pow(2, 31) - 1; + +const setSafeTimeout = (callback, ms) => { + const avaliableMs = ms < MAX_INT32 ? ms : MAX_INT32; + + return timers.setTimeout(callback, avaliableMs) +} + +Object.assign(exports, timers, { setSafeTimeout }); exports.now = Date.now; diff --git a/lib/test.js b/lib/test.js index d2bb93e2e..7020dda4a 100644 --- a/lib/test.js +++ b/lib/test.js @@ -405,7 +405,7 @@ export default class Test { this.clearTimeout(); this.timeoutMs = ms; - this.timeoutTimer = nowAndTimers.setTimeout(() => { + this.timeoutTimer = nowAndTimers.setSafeTimeout(() => { this.saveFirstError(new Error(message || 'Test timeout exceeded')); if (this.finishDueToTimeout) { From 090a2b06507366aa9045cf2a1703b1b61f5fccfb Mon Sep 17 00:00:00 2001 From: creestor Date: Mon, 5 Sep 2022 01:53:37 +0400 Subject: [PATCH 2/3] fix linting issues --- lib/api.js | 2 +- lib/now-and-timers.cjs | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/api.js b/lib/api.js index c61e70342..8c44be27a 100644 --- a/lib/api.js +++ b/lib/api.js @@ -16,10 +16,10 @@ import fork from './fork.js'; import * as globs from './globs.js'; import isCi from './is-ci.js'; import {getApplicableLineNumbers} from './line-numbers.js'; +import nowAndTimers from './now-and-timers.cjs'; import {observeWorkerProcess} from './plugin-support/shared-workers.js'; import RunStatus from './run-status.js'; import scheduler from './scheduler.js'; -import nowAndTimers from './now-and-timers.cjs'; import serializeError from './serialize-error.js'; function resolveModules(modules) { diff --git a/lib/now-and-timers.cjs b/lib/now-and-timers.cjs index 4e4cf1954..0d5b523b4 100644 --- a/lib/now-and-timers.cjs +++ b/lib/now-and-timers.cjs @@ -2,13 +2,15 @@ const timers = require('timers'); // Slightly higher than 24 days -const MAX_INT32 = Math.pow(2, 31) - 1; +const MAX_INT32 = (2 ** 31) - 1; const setSafeTimeout = (callback, ms) => { - const avaliableMs = ms < MAX_INT32 ? ms : MAX_INT32; + const allowedMs = Math.min(ms, MAX_INT32); - return timers.setTimeout(callback, avaliableMs) -} + return timers.setTimeout(callback, allowedMs); +}; + +Object.assign(exports, timers); -Object.assign(exports, timers, { setSafeTimeout }); exports.now = Date.now; +exports.setSafeTimeout = setSafeTimeout; From c835d56fc78311582e9375b4403d6f40ad4d1110 Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Mon, 5 Sep 2022 11:09:46 +0200 Subject: [PATCH 3/3] Move things around and rename for extra clarity --- lib/api.js | 4 ++-- lib/now-and-timers.cjs | 20 ++++++++++---------- lib/test.js | 2 +- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/api.js b/lib/api.js index 8c44be27a..eaf223650 100644 --- a/lib/api.js +++ b/lib/api.js @@ -16,7 +16,7 @@ import fork from './fork.js'; import * as globs from './globs.js'; import isCi from './is-ci.js'; import {getApplicableLineNumbers} from './line-numbers.js'; -import nowAndTimers from './now-and-timers.cjs'; +import {setCappedTimeout} from './now-and-timers.cjs'; import {observeWorkerProcess} from './plugin-support/shared-workers.js'; import RunStatus from './run-status.js'; import scheduler from './scheduler.js'; @@ -53,7 +53,7 @@ class TimeoutTrigger { debounce() { if (this.timer === undefined) { - this.timer = nowAndTimers.setSafeTimeout(() => this.trigger(), this.waitMs); + this.timer = setCappedTimeout(() => this.trigger(), this.waitMs); } else { this.timer.refresh(); } diff --git a/lib/now-and-timers.cjs b/lib/now-and-timers.cjs index 0d5b523b4..851bf6d4d 100644 --- a/lib/now-and-timers.cjs +++ b/lib/now-and-timers.cjs @@ -1,16 +1,16 @@ 'use strict'; const timers = require('timers'); -// Slightly higher than 24 days -const MAX_INT32 = (2 ** 31) - 1; +Object.assign(exports, timers); +exports.now = Date.now; -const setSafeTimeout = (callback, ms) => { - const allowedMs = Math.min(ms, MAX_INT32); +// Any delay larger than this value is ignored by Node.js, with a delay of `1` +// used instead. See . +const MAX_DELAY = (2 ** 31) - 1; - return timers.setTimeout(callback, allowedMs); -}; +function setCappedTimeout(callback, delay) { + const safeDelay = Math.min(delay, MAX_DELAY); + return timers.setTimeout(callback, safeDelay); +} -Object.assign(exports, timers); - -exports.now = Date.now; -exports.setSafeTimeout = setSafeTimeout; +exports.setCappedTimeout = setCappedTimeout; diff --git a/lib/test.js b/lib/test.js index 7020dda4a..d6a39444a 100644 --- a/lib/test.js +++ b/lib/test.js @@ -405,7 +405,7 @@ export default class Test { this.clearTimeout(); this.timeoutMs = ms; - this.timeoutTimer = nowAndTimers.setSafeTimeout(() => { + this.timeoutTimer = nowAndTimers.setCappedTimeout(() => { this.saveFirstError(new Error(message || 'Test timeout exceeded')); if (this.finishDueToTimeout) {