Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/libraries/core/tests/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
18 changes: 8 additions & 10 deletions packages/libraries/core/tests/usage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
fastFetchError,
normalizeLogMessage,
waitFor,
waitUntil,
} from './test-utils';

const headers = {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
Loading