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 2 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
24 changes: 19 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,24 @@
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, timeout = 5000): Promise<void> => {
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 +61,8 @@ describe('Discv5Service', () => {
const node2 = await createNode(port);
await node1.start();
await node2.start();
await sleep(200);
await waitForPeers(node2, 2, 4000);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like it's probably simpler to just removed the 3rd argument here and use the default. It means fewer hardcoded number littered around

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 +82,13 @@ describe('Discv5Service', () => {
const node2 = await createNode(port);
await node1.start();
await node2.start();
await sleep(200);
await waitForPeers(node2, 2, 4000);

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

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

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