Skip to content

Commit

Permalink
fix: Better handle async timings in test (#9178)
Browse files Browse the repository at this point in the history
This PR makes an improvement to the way a test handles waiting for syncs
  • Loading branch information
PhilWindle authored Oct 11, 2024
1 parent 7872d09 commit fb35151
Showing 1 changed file with 35 additions and 11 deletions.
46 changes: 35 additions & 11 deletions yarn-project/world-state/src/test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('world-state integration', () => {
log.info(`Generating ${MAX_BLOCK_COUNT} mock blocks`);
({ blocks, messages } = await mockBlocks(1, MAX_BLOCK_COUNT, 1, db));
log.info(`Generated ${blocks.length} mock blocks`);
});
}, 30_000);

beforeEach(async () => {
config = {
Expand All @@ -56,7 +56,30 @@ describe('world-state integration', () => {
await db.close();
});

const awaitSync = () => sleep(200);
const awaitSync = async (blockToSyncTo: number, finalized?: number, maxTimeoutMS = 2000) => {
const startTime = Date.now();
let sleepTime = 0;
let tips = await synchronizer.getL2Tips();

const waitForFinalised = (tipFinalised?: number) => {
if (finalized == undefined || tipFinalised == undefined) {
return false;
}
return finalized > tipFinalised;
};

while (tips.latest < blockToSyncTo && sleepTime < maxTimeoutMS) {
await sleep(100);
sleepTime = Date.now() - startTime;
tips = await synchronizer.getL2Tips();
}

while (waitForFinalised(tips.finalized) && sleepTime < maxTimeoutMS) {
await sleep(100);
sleepTime = Date.now() - startTime;
tips = await synchronizer.getL2Tips();
}
};

const expectSynchedBlockHashMatches = async (number: number) => {
const syncedBlockHash = await db.getCommitted().getLeafValue(MerkleTreeId.ARCHIVE, BigInt(number));
Expand Down Expand Up @@ -85,7 +108,7 @@ describe('world-state integration', () => {
it('syncs new blocks from the archiver from genesis', async () => {
await synchronizer.start();
archiver.createBlocks(5);
await awaitSync();
await awaitSync(5);
await expectSynchedToBlock(5);
});

Expand All @@ -94,7 +117,7 @@ describe('world-state integration', () => {
await synchronizer.start();

archiver.createBlocks(3);
await awaitSync();
await awaitSync(8);
await expectSynchedToBlock(8);
});

Expand All @@ -104,7 +127,7 @@ describe('world-state integration', () => {
await expectSynchedToBlock(10);

archiver.createBlocks(10);
await awaitSync();
await awaitSync(20);
await expectSynchedToBlock(20);
});

Expand All @@ -113,7 +136,7 @@ describe('world-state integration', () => {

await synchronizer.start();
archiver.createBlocks(5);
await awaitSync();
await awaitSync(5);
await expectSynchedToBlock(5);
await synchronizer.stopBlockStream();

Expand All @@ -124,7 +147,7 @@ describe('world-state integration', () => {
await expectSynchedToBlock(8);

archiver.createBlocks(4);
await awaitSync();
await awaitSync(12);
await expectSynchedToBlock(12);

expect(getBlocksSpy).toHaveBeenCalledTimes(3);
Expand All @@ -142,7 +165,7 @@ describe('world-state integration', () => {
await expectSynchedToBlock(3);

archiver.setProvenBlockNumber(4);
await awaitSync();
await awaitSync(4);
await expectSynchedToBlock(4);
});
});
Expand All @@ -159,7 +182,8 @@ describe('world-state integration', () => {

archiver.removeBlocks(3);
archiver.createBlocks(2);
await awaitSync();
await sleep(2000);
await awaitSync(4);
await expectSynchedToBlock(4);
});
});
Expand Down Expand Up @@ -220,11 +244,11 @@ describe('world-state integration', () => {
archiver.setProvenBlockNumber(3);

await synchronizer.start();
await awaitSync();
await awaitSync(5, 3);
await expectSynchedToBlock(5, 3);

archiver.setProvenBlockNumber(4);
await awaitSync();
await awaitSync(5, 4);
await expectSynchedToBlock(5, 4);
});
});
Expand Down

0 comments on commit fb35151

Please sign in to comment.