Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make discv5 test deterministic #5968

Merged
merged 4 commits into from
Apr 24, 2024
Merged
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
25 changes: 20 additions & 5 deletions yarn-project/p2p/src/service/discv5_service.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import { sleep } from '@aztec/foundation/sleep';

import type { PeerId } from '@libp2p/interface';

import { BootstrapNode } from '../bootstrap/bootstrap.js';
import { DiscV5Service, PeerDiscoveryState } from './discV5_service.js';
import { createLibP2PPeerId } from './libp2p_service.js';

const waitForPeers = (node: DiscV5Service, expectedCount: number): Promise<void> => {
const timeout = 5_000;
return new Promise((resolve, reject) => {
const timeoutId = setTimeout(() => {
reject(new Error(`Timeout: Failed to connect to ${expectedCount} peers within ${timeout} ms`));
}, timeout);

node.on('peer:discovered', () => {
if (node.getAllPeers().length >= expectedCount) {
clearTimeout(timeoutId);
resolve();
}
});
});
};

describe('Discv5Service', () => {
let bootNode: BootstrapNode;
let bootNodePeerId: PeerId;
Expand Down Expand Up @@ -48,7 +62,8 @@ describe('Discv5Service', () => {
const node2 = await createNode(port);
await node1.start();
await node2.start();
await sleep(200);
await waitForPeers(node2, 2);

const node1Peers = await Promise.all(node1.getAllPeers().map(async peer => (await peer.peerId()).toString()));
const node2Peers = await Promise.all(node2.getAllPeers().map(async peer => (await peer.peerId()).toString()));

Expand All @@ -68,13 +83,13 @@ describe('Discv5Service', () => {
const node2 = await createNode(port);
await node1.start();
await node2.start();
await sleep(200);
await waitForPeers(node2, 2);

await node2.stop();
await bootNode.stop();

await node2.start();
await sleep(200);
await waitForPeers(node2, 1);

const node2Peers = await Promise.all(node2.getAllPeers().map(async peer => (await peer.peerId()).toString()));
expect(node2Peers).toHaveLength(1);
Expand Down
Loading