diff --git a/dev-packages/node-core-integration-tests/suites/anr/test.ts b/dev-packages/node-core-integration-tests/suites/anr/test.ts index c9a81ccb5db0..406830c9b299 100644 --- a/dev-packages/node-core-integration-tests/suites/anr/test.ts +++ b/dev-packages/node-core-integration-tests/suites/anr/test.ts @@ -2,6 +2,17 @@ import type { Event } from '@sentry/core'; import { afterAll, describe, expect, test } from 'vitest'; import { cleanupChildProcesses, createRunner } from '../../utils/runner'; +/** Avoid flakes on slow CI: fixed sleeps can fire before the child process has finished exiting. */ +async function waitForChildExit(childHasExited: () => boolean, timeoutMs = 30_000): Promise { + const start = Date.now(); + while (!childHasExited()) { + if (Date.now() - start > timeoutMs) { + throw new Error('Timed out waiting for child process to exit'); + } + await new Promise(resolve => setTimeout(resolve, 100)); + } +} + const ANR_EVENT = { // Ensure we have context contexts: { @@ -178,7 +189,7 @@ describe('should report ANR when event loop blocked', { timeout: 90_000 }, () => test('should exit', async () => { const runner = createRunner(__dirname, 'should-exit.js').start(); - await new Promise(resolve => setTimeout(resolve, 5_000)); + await waitForChildExit(() => runner.childHasExited()); expect(runner.childHasExited()).toBe(true); }); @@ -186,7 +197,7 @@ describe('should report ANR when event loop blocked', { timeout: 90_000 }, () => test('should exit forced', async () => { const runner = createRunner(__dirname, 'should-exit-forced.js').start(); - await new Promise(resolve => setTimeout(resolve, 5_000)); + await waitForChildExit(() => runner.childHasExited()); expect(runner.childHasExited()).toBe(true); });