Skip to content
Merged
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
17 changes: 11 additions & 6 deletions packages/runtime/test/lib/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,17 @@ import {
import { TestRuntimeServices } from "./RuntimeServiceProvider";
import { TestNotificationItem } from "./TestNotificationItem";

const DEFAULT_SYNC_UNTIL_TIMEOUT = 10000;
const MAX_SYNC_UNTIL_SLEEP = 250;

export async function syncUntil(transportServices: TransportServices, until: (syncResult: SyncEverythingResponse) => boolean): Promise<SyncEverythingResponse> {
const finalSyncResult: SyncEverythingResponse = { messages: [], relationships: [], identityDeletionProcesses: [], files: [] };

let iterationNumber = 0;
let criteriaMet: boolean;
const startedAt = Date.now();
do {
await sleep(iterationNumber * 25);
await sleep(Math.min(iterationNumber * 25, MAX_SYNC_UNTIL_SLEEP));

const currentIterationSyncResult = (await transportServices.account.syncEverything()).value;

Expand All @@ -84,8 +88,8 @@ export async function syncUntil(transportServices: TransportServices, until: (sy

iterationNumber++;
criteriaMet = until(finalSyncResult);
} while (!criteriaMet && iterationNumber < 15);
if (!criteriaMet) throw new Error("syncUntil timed out.");
} while (!criteriaMet && Date.now() - startedAt < DEFAULT_SYNC_UNTIL_TIMEOUT);
if (!criteriaMet) throw new Error(`syncUntil timed out after ${DEFAULT_SYNC_UNTIL_TIMEOUT}ms.`);
return finalSyncResult;
}

Expand Down Expand Up @@ -151,8 +155,9 @@ export async function syncUntilHasEvent<TEvent extends Event>(
): Promise<Event> {
let iterationNumber = 0;
let event: Event | undefined;
const startedAt = Date.now();
do {
await sleep(iterationNumber * 25);
await sleep(Math.min(iterationNumber * 25, MAX_SYNC_UNTIL_SLEEP));

await runtimeServices.transport.account.syncEverything();
event = runtimeServices.eventBus.publishedEvents.find(
Expand All @@ -163,8 +168,8 @@ export async function syncUntilHasEvent<TEvent extends Event>(
);

iterationNumber++;
} while (!event && iterationNumber < 15);
if (!event) throw new Error("syncUntil timed out.");
} while (!event && Date.now() - startedAt < DEFAULT_SYNC_UNTIL_TIMEOUT);
if (!event) throw new Error(`syncUntil timed out after ${DEFAULT_SYNC_UNTIL_TIMEOUT}ms.`);
return event;
}

Expand Down
14 changes: 10 additions & 4 deletions packages/transport/test/testHelpers/TestUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ import {
} from "../../src";

export class TestUtil {
private static readonly DEFAULT_SYNC_UNTIL_TIMEOUT = 60000;
private static readonly MAX_SYNC_UNTIL_SLEEP = 1000;

public static loggerFactory = new NodeLoggerFactory({
appenders: {
consoleAppender: {
Expand Down Expand Up @@ -466,18 +469,21 @@ export class TestUtil {
const syncResult = new ChangedItems();

let iterationNumber = 0;
let criteriaMet = false;
const startedAt = Date.now();
do {
await sleep(150 * iterationNumber);
await sleep(Math.min(150 * iterationNumber, TestUtil.MAX_SYNC_UNTIL_SLEEP));
const newSyncResult = await accountController.syncEverything();
syncResult.messages.push(...newSyncResult.messages);
syncResult.relationships.push(...newSyncResult.relationships);
syncResult.identityDeletionProcesses.push(...newSyncResult.identityDeletionProcesses);
syncResult.files.push(...newSyncResult.files);
iterationNumber++;
} while (!until(syncResult) && iterationNumber < 20);
criteriaMet = until(syncResult);
} while (!criteriaMet && Date.now() - startedAt < TestUtil.DEFAULT_SYNC_UNTIL_TIMEOUT);

if (!until(syncResult)) {
throw new Error("syncUntil condition was not met");
if (!criteriaMet) {
throw new Error(`syncUntil condition was not met after ${TestUtil.DEFAULT_SYNC_UNTIL_TIMEOUT}ms`);
}
return syncResult;
}
Expand Down