From 38f260cc519386f67c1c2e4259942d8f74eddbc2 Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Mon, 23 Jan 2023 17:57:37 -0800 Subject: [PATCH] test: unflake "should respect interval" (#20298) --- packages/playwright-core/src/utils/timeoutRunner.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/playwright-core/src/utils/timeoutRunner.ts b/packages/playwright-core/src/utils/timeoutRunner.ts index 72a1c4f1ab..32ba38faa4 100644 --- a/packages/playwright-core/src/utils/timeoutRunner.ts +++ b/packages/playwright-core/src/utils/timeoutRunner.ts @@ -117,7 +117,7 @@ export async function pollAgainstTimeout(callback: () => Promise<{ continuePo const wrappedCallback = () => Promise.resolve().then(callback); while (true) { const elapsed = monotonicTime() - startTime; - if (timeout !== 0 && elapsed > timeout) + if (timeout !== 0 && elapsed >= timeout) break; const received = timeout !== 0 ? await raceAgainstTimeout(wrappedCallback, timeout - elapsed) : await wrappedCallback().then(value => ({ result: value, timedOut: false })); @@ -126,11 +126,9 @@ export async function pollAgainstTimeout(callback: () => Promise<{ continuePo lastResult = received.result.result; if (!received.result.continuePolling) return { result: received.result.result, timedOut: false }; - let interval = pollIntervals!.shift() ?? lastPollInterval; - if (timeout !== 0) { - const elapsed = monotonicTime() - startTime; - interval = Math.min(interval, Math.max(timeout - elapsed, 1)); - } + const interval = pollIntervals!.shift() ?? lastPollInterval; + if (timeout !== 0 && startTime + timeout <= monotonicTime() + interval) + break; await new Promise(x => setTimeout(x, interval)); } return { timedOut: true, result: lastResult };