*refactor test to add delays on creating the peers

This commit is contained in:
Derrick Hammer 2022-11-27 16:32:34 -05:00
parent 4f8918f8e9
commit a83c66c630
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 33 additions and 31 deletions

16
test.js
View File

@ -1,4 +1,5 @@
const test = require("tape"); const test = require("tape");
const sleep = require("sleep-promise");
const Hyperswarm = require("hyperswarm"); const Hyperswarm = require("hyperswarm");
const sodium = require("sodium-universal"); const sodium = require("sodium-universal");
const b4a = require("b4a"); const b4a = require("b4a");
@ -8,16 +9,13 @@ const crypto = require("crypto");
const topicName = crypto.randomBytes(10); const topicName = crypto.randomBytes(10);
test("Broadcast through several peers", (t) => { test("Broadcast through several peers", (t) => {
const peer1 = createPeer();
const peer2 = createPeer();
const peer3 = createPeer();
t.plan(2); t.plan(2);
Promise.all([peer1, peer2, peer3]).then((peers) => { Promise.all([createPeer(100), createPeer(300), createPeer(500)]).then(
(peers) => {
const peer1 = peers.shift(); const peer1 = peers.shift();
const peer2 = peers.shift(); const peer2 = peers.shift();
const peer3 = peers.shift(); const peer3 = peers.shift();
const flood1 = new DHTFlood({ swarm: peer1 }); const flood1 = new DHTFlood({ swarm: peer1 });
const flood2 = new DHTFlood({ swarm: peer2 }); const flood2 = new DHTFlood({ swarm: peer2 });
const flood3 = new DHTFlood({ swarm: peer3 }); const flood3 = new DHTFlood({ swarm: peer3 });
@ -44,10 +42,12 @@ test("Broadcast through several peers", (t) => {
t.teardown(() => { t.teardown(() => {
[peer1, peer2, peer3].forEach((item) => item.destroy()); [peer1, peer2, peer3].forEach((item) => item.destroy());
}); });
}); }
);
}); });
async function createPeer() { async function createPeer(ms) {
await sleep(ms / 2);
const swarm = new Hyperswarm(); const swarm = new Hyperswarm();
await swarm.dht.ready(); await swarm.dht.ready();
await swarm.listen(); await swarm.listen();
@ -57,5 +57,7 @@ async function createPeer() {
swarm.join(topic); swarm.join(topic);
await sleep(ms / 2);
return swarm; return swarm;
} }