From 249bf36b49d2ea22d48f88f424e74388b5382e8c Mon Sep 17 00:00:00 2001 From: Tuyen Nguyen Date: Thu, 5 May 2022 16:24:03 +0700 Subject: [PATCH 1/2] Fix connectSome test util --- test/utils/create-gossipsub.ts | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/test/utils/create-gossipsub.ts b/test/utils/create-gossipsub.ts index 94b846f0..362fce54 100644 --- a/test/utils/create-gossipsub.ts +++ b/test/utils/create-gossipsub.ts @@ -89,29 +89,38 @@ export async function tearDownGossipsubs(gss: PubsubBaseMinimal[]) { } /** - * Connect some gossipsub nodes to others + * Connect some gossipsub nodes to others, ensure each has num peers * @param {Gossipsub[]} gss * @param {number} num number of peers to connect */ export async function connectSome(gss: PubsubBaseMinimal[], num: number) { for (let i = 0; i < gss.length; i++) { - for (let j = 0; j < num; j++) { - const n = Math.floor(Math.random() * gss.length) - if (n === i) { - j-- - continue + let count = 0; + // merely do a Math.random() and check for duplicate may take a lot of time to run a test + // so we make an array of candidate peers + // initially, don't populate i as a candidate to connect: candidatePeers[i] = i + 1 + const candidatePeers = Array.from({length: gss.length - 1}, (_, j) => j >= i ? j + 1 : j) + while (count < num) { + const n = Math.floor(Math.random() * (candidatePeers.length)) + const peer = candidatePeers[n] + await connectGossipsub(gss[i], gss[peer]) + // after connecting to a peer, update indexToPeers so that we don't connect to it again + for (let j = n; j < candidatePeers.length - 1; j++) { + candidatePeers[j] = candidatePeers[j + 1] } - await connectGossipsub(gss[i], gss[n]) + // remove the last item + candidatePeers.splice(candidatePeers.length - 1, 1) + count++ } } } export async function sparseConnect(gss: PubsubBaseMinimal[]) { - await connectSome(gss, 3) + await connectSome(gss, Math.min(3, gss.length - 1)) } export async function denseConnect(gss: PubsubBaseMinimal[]) { - await connectSome(gss, 10) + await connectSome(gss, Math.min(10, gss.length - 1)) } /** From da7d51ad14628093bcdc97104a40776f90f6a368 Mon Sep 17 00:00:00 2001 From: Tuyen Nguyen Date: Thu, 5 May 2022 16:28:21 +0700 Subject: [PATCH 2/2] chore: fix comment --- test/utils/create-gossipsub.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/utils/create-gossipsub.ts b/test/utils/create-gossipsub.ts index 362fce54..e2bd0c3d 100644 --- a/test/utils/create-gossipsub.ts +++ b/test/utils/create-gossipsub.ts @@ -104,7 +104,7 @@ export async function connectSome(gss: PubsubBaseMinimal[], num: number) { const n = Math.floor(Math.random() * (candidatePeers.length)) const peer = candidatePeers[n] await connectGossipsub(gss[i], gss[peer]) - // after connecting to a peer, update indexToPeers so that we don't connect to it again + // after connecting to a peer, update candidatePeers so that we don't connect to it again for (let j = n; j < candidatePeers.length - 1; j++) { candidatePeers[j] = candidatePeers[j + 1] }