fix(core): attempt to fix flaky usage tests (#7501)

This commit is contained in:
Adam Benhassen 2026-01-16 14:35:15 +02:00 committed by GitHub
parent 3e52e4d9bf
commit 5d2b26a916
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 21 additions and 10 deletions

View file

@ -4,6 +4,19 @@ export function waitFor(ms: number) {
});
}
export async function waitUntil(
condition: () => boolean | Promise<boolean>,
{ timeout = 1000, interval = 5 } = {},
): Promise<void> {
const start = Date.now();
while (!(await condition())) {
if (Date.now() - start > timeout) {
throw new Error(`waitUntil timed out after ${timeout}ms`);
}
await waitFor(interval);
}
}
/** helper function to get log lines and replace milliseconds with static value. */
function getLogLines(calls: Array<Array<unknown>>) {
return calls.map(log => {

View file

@ -10,6 +10,7 @@ import {
fastFetchError,
normalizeLogMessage,
waitFor,
waitUntil,
} from './test-utils';
const headers = {
@ -443,8 +444,7 @@ test('sendImmediately should not stop the schedule', async () => {
// Now we will hit the maxSize
// We run collect two times
await Promise.all([collect(args, {}), collect(args, {})]);
await waitFor(1);
expect(logger.getLogs()).toMatch('Sending immediately');
await waitUntil(() => logger.getLogs().includes('Sending immediately'));
expect(logger.getLogs()).toMatch('Sending report (queue 2)');
logger.clear();
// Let's check if the scheduled send task is still running
@ -538,16 +538,14 @@ test('should send data to Hive at least once when using atLeastOnceSampler', asy
),
]);
await hive.dispose();
await waitFor(50);
await waitUntil(() => logger.getLogs().includes('Report sent!'));
http.done();
expect(logger.getLogs()).toMatchInlineSnapshot(`
[DBG] Disposing
[DBG] Sending report (queue 2)
[DBG] POST http://localhost/200 (x-request-id=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
[DBG] POST http://localhost/200 (x-request-id=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) succeeded with status 200 (666ms).
[DBG] Report sent!
`);
expect(logger.getLogs()).toMatch(/\[DBG\] Disposing/);
expect(logger.getLogs()).toMatch(/\[DBG\] Sending report \(queue 2\)/);
expect(logger.getLogs()).toMatch(/\[DBG\] POST http:\/\/localhost\/200/);
expect(logger.getLogs()).toMatch(/succeeded with status 200/);
expect(logger.getLogs()).toMatch(/\[DBG\] Report sent!/);
// Map
expect(report.size).toEqual(2);