@@ -157,9 +157,16 @@ function makeBot() {
|
157 | 157 | |
158 | 158 | function installPollingStallWatchdogHarness(dateNowSequence: readonly number[] = [0, 0]) { |
159 | 159 | let watchdog: (() => void) | undefined; |
| 160 | +let resolveWatchdog: ((fn: () => void) => void) | undefined; |
| 161 | +const watchdogReady = new Promise<() => void>((resolve) => { |
| 162 | +resolveWatchdog = resolve; |
| 163 | +}); |
| 164 | +const realSetTimeout = globalThis.setTimeout; |
| 165 | +const realClearTimeout = globalThis.clearTimeout; |
160 | 166 | const setIntervalSpy = vi.spyOn(globalThis, "setInterval").mockImplementation((fn, delay) => { |
161 | 167 | if (delay === POLLING_TEST_WATCHDOG_INTERVAL_MS) { |
162 | 168 | watchdog = fn as () => void; |
| 169 | +resolveWatchdog?.(watchdog); |
163 | 170 | } |
164 | 171 | return 1 as unknown as ReturnType<typeof setInterval>; |
165 | 172 | }); |
@@ -177,15 +184,24 @@ function installPollingStallWatchdogHarness(dateNowSequence: readonly number[] =
|
177 | 184 | |
178 | 185 | return { |
179 | 186 | async waitForWatchdog() { |
180 | | -for (let attempt = 0; attempt < 200; attempt += 1) { |
181 | | -if (watchdog) { |
182 | | -break; |
183 | | -} |
184 | | -await Promise.resolve(); |
185 | | -await new Promise<void>((resolve) => setImmediate(resolve)); |
| 187 | +if (watchdog) { |
| 188 | +return watchdog; |
186 | 189 | } |
187 | | -expect(watchdog).toBeTypeOf("function"); |
188 | | -return watchdog; |
| 190 | +return await new Promise<() => void>((resolve, reject) => { |
| 191 | +const timeout = realSetTimeout(() => { |
| 192 | +reject(new Error("Timed out waiting for polling watchdog interval registration")); |
| 193 | +}, 5_000); |
| 194 | +watchdogReady.then( |
| 195 | +(fn) => { |
| 196 | +realClearTimeout(timeout); |
| 197 | +resolve(fn); |
| 198 | +}, |
| 199 | +(error: unknown) => { |
| 200 | +realClearTimeout(timeout); |
| 201 | +reject(error); |
| 202 | +}, |
| 203 | +); |
| 204 | +}); |
189 | 205 | }, |
190 | 206 | setNow(now: number) { |
191 | 207 | dateNowSpy.mockReset(); |
|