Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(clock): do not spend 4ms of real time per fired timer (#43470)

Yury Semikhatsky695e0ad85254
diff --git a/packages/injected/src/clock.ts b/packages/injected/src/clock.ts
index e7cf87b1de..c73fd2a763 100644
--- a/packages/injected/src/clock.ts
+++ b/packages/injected/src/clock.ts
@@ -45,6 +45,7 @@ interface Embedder {
performanceNow(): EmbedderTicks;
setTimeout(task: () => void, timeout?: number): () => void;
setInterval(task: () => void, delay: number): () => void;
+ postTask(task: () => void): void;
}
type Ticks = number & { readonly __brand: 'Ticks' };
@@ -374,7 +375,7 @@ export class ClockController {
} catch (e) {
error = e;
}
- await new Promise<void>(f => this._embedder.setTimeout(f));
+ await new Promise<void>(f => this._embedder.postTask(f));
return { timerFound: true, error };
}
@@ -390,7 +391,7 @@ export class ClockController {
} catch (e) {
error = e;
}
- await new Promise<void>(f => this._embedder.setTimeout(f));
+ await new Promise<void>(f => this._embedder.postTask(f));
return { timerFound: true, error };
} finally {
this._duringTick = false;
@@ -800,6 +801,10 @@ function fakeAbortSignal(clock: ClockController, abortSignal: Builtins['AbortSig
export function createClock(globalObject: WindowOrWorkerGlobalScope, config: InstallConfig = {}): { clock: ClockController, api: Builtins, originals: Builtins } {
const originals = platformOriginals(globalObject);
+ // Nested setTimeout(fn, 0) is clamped to 4ms, which adds up when many timers
+ // are due. A MessageChannel task is not clamped, so yield through one instead.
+ const tasks: (() => void)[] = [];
+ let channel: MessageChannel | undefined;
const embedder: Embedder = {
dateNow: () => originals.raw.Date.now(),
performanceNow: () => Math.ceil(originals.raw.performance!.now()) as EmbedderTicks,
@@ -811,6 +816,14 @@ export function createClock(globalObject: WindowOrWorkerGlobalScope, config: Ins
const intervalId = originals.bound.setInterval(task, delay);
return () => originals.bound.clearInterval(intervalId);
},
+ postTask: (task: () => void) => {
+ if (!channel) {
+ channel = new (globalObject as any).MessageChannel() as MessageChannel;
+ channel.port1.onmessage = () => tasks.shift()!();
+ }
+ tasks.push(task);
+ channel.port2.postMessage(undefined);
+ },
};
const clock = new ClockController(embedder);
diff --git a/tests/library/page-clock.spec.ts b/tests/library/page-clock.spec.ts
index 02d3444fe6..9e7401702b 100644
--- a/tests/library/page-clock.spec.ts
+++ b/tests/library/page-clock.spec.ts
@@ -144,13 +144,18 @@ it.describe('runFor', () => {
expect(calls.length).toBe(10);
});
- it('passes 2 hours, 34 minutes and 10 seconds', async ({ page, calls }) => {
+ it('passes 2 hours, 34 minutes and 10 seconds', {
+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43452' },
+ }, async ({ page, calls }) => {
await page.evaluate(async () => {
setInterval(window.stub, 10000);
});
+ const start = performance.now();
await page.clock.runFor('02:34:10');
expect(calls.length).toBe(925);
+ // Nested setTimeout(fn, 0) is clamped to 4ms, so firing 925 timers used to take over 4 seconds.
+ expect(performance.now() - start).toBeLessThan(2000);
});
it('throws for invalid format', async ({ page, calls }) => {