Skip to content

Commit

Permalink
chore: use random ports in p2p_client tests (#8624)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 authored Sep 18, 2024
1 parent a098d41 commit 650a241
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions yarn-project/foundation/src/testing/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './test_data.js';
export * from './snapshot_serializer.js';
export * from './port_allocator.js';
31 changes: 31 additions & 0 deletions yarn-project/foundation/src/testing/port_allocator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import net from 'net';

/**
* Get a random port that is free to use.
* Returns undefined if it fails to get a port.
*
* @attribution: adapted from https://stackoverflow.com/a/71178451
*
* @returns a random port that is free to use.
*/
export function getRandomPort(): Promise<number | undefined> {
return new Promise(resolve => {
const server = net.createServer();
server.listen(0, () => {
const address = server.address();
if (address && typeof address === 'object' && 'port' in address) {
const port = address.port;
server.close(() => {
resolve(port);
});
} else {
server.close(() => {
resolve(undefined);
});
}
});
server.on('error', () => {
resolve(undefined);
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { type ClientProtocolCircuitVerifier, type WorldStateSynchronizer, mockTx
import { EthAddress } from '@aztec/circuits.js';
import { createDebugLogger } from '@aztec/foundation/log';
import { sleep } from '@aztec/foundation/sleep';
import { getRandomPort } from '@aztec/foundation/testing';
import { type AztecKVStore } from '@aztec/kv-store';
import { type DataStoreConfig, openTmpStore } from '@aztec/kv-store/utils';

Expand All @@ -27,7 +28,7 @@ type Mockify<T> = {

const TEST_TIMEOUT = 80000;

const BOOT_NODE_UDP_PORT = 40400;
const DEFAULT_BOOT_NODE_UDP_PORT = 40400;
async function createBootstrapNode(port: number) {
const peerId = await createLibP2PPeerId();
const bootstrapNode = new BootstrapNode();
Expand Down Expand Up @@ -61,10 +62,12 @@ describe('Req Resp p2p client integration', () => {
let kvStore: AztecKVStore;
let worldStateSynchronizer: WorldStateSynchronizer;
let proofVerifier: ClientProtocolCircuitVerifier;
let bootNodePort: number;
const logger = createDebugLogger('p2p-client-integration-test');

const makeBootstrapNode = async (): Promise<[BootstrapNode, string]> => {
const bootstrapNode = await createBootstrapNode(BOOT_NODE_UDP_PORT);
bootNodePort = (await getRandomPort()) || DEFAULT_BOOT_NODE_UDP_PORT;
const bootstrapNode = await createBootstrapNode(bootNodePort);
const enr = bootstrapNode.getENR().encodeTxt();
return [bootstrapNode, enr];
};
Expand All @@ -74,8 +77,9 @@ describe('Req Resp p2p client integration', () => {
const peerIdPrivateKeys = generatePeerIdPrivateKeys(numberOfPeers);
for (let i = 0; i < numberOfPeers; i++) {
// Note these bindings are important
const addr = `127.0.0.1:${i + 1 + BOOT_NODE_UDP_PORT}`;
const listenAddr = `0.0.0.0:${i + 1 + BOOT_NODE_UDP_PORT}`;
const port = (await getRandomPort()) || bootNodePort + i + 1;
const addr = `127.0.0.1:${port}`;
const listenAddr = `0.0.0.0:${port}`;
const config: P2PConfig & DataStoreConfig = {
...getP2PDefaultConfig(),
p2pEnabled: true,
Expand Down

0 comments on commit 650a241

Please sign in to comment.