Skip to content

Commit 217ad4a

Browse files
authored
test(node): Fix flaky ANR test (#20592)
Closes #20268
1 parent 91ffb3f commit 217ad4a

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

  • dev-packages/node-core-integration-tests/suites/anr

dev-packages/node-core-integration-tests/suites/anr/test.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ import type { Event } from '@sentry/core';
22
import { afterAll, describe, expect, test } from 'vitest';
33
import { cleanupChildProcesses, createRunner } from '../../utils/runner';
44

5+
/** Avoid flakes on slow CI: fixed sleeps can fire before the child process has finished exiting. */
6+
async function waitForChildExit(childHasExited: () => boolean, timeoutMs = 30_000): Promise<void> {
7+
const start = Date.now();
8+
while (!childHasExited()) {
9+
if (Date.now() - start > timeoutMs) {
10+
throw new Error('Timed out waiting for child process to exit');
11+
}
12+
await new Promise<void>(resolve => setTimeout(resolve, 100));
13+
}
14+
}
15+
516
const ANR_EVENT = {
617
// Ensure we have context
718
contexts: {
@@ -178,15 +189,15 @@ describe('should report ANR when event loop blocked', { timeout: 90_000 }, () =>
178189
test('should exit', async () => {
179190
const runner = createRunner(__dirname, 'should-exit.js').start();
180191

181-
await new Promise(resolve => setTimeout(resolve, 5_000));
192+
await waitForChildExit(() => runner.childHasExited());
182193

183194
expect(runner.childHasExited()).toBe(true);
184195
});
185196

186197
test('should exit forced', async () => {
187198
const runner = createRunner(__dirname, 'should-exit-forced.js').start();
188199

189-
await new Promise(resolve => setTimeout(resolve, 5_000));
200+
await waitForChildExit(() => runner.childHasExited());
190201

191202
expect(runner.childHasExited()).toBe(true);
192203
});

0 commit comments

Comments
 (0)