diff --git a/index.js b/index.js index aeb34aa..3736196 100644 --- a/index.js +++ b/index.js @@ -24,8 +24,10 @@ export default function runAll(nodes) { const ma = new Multiaddr(a); return ma.getPeerId(); }); - - expect(nodes.length).to.eq(3); + const hostnames = nodes.map((a) => { + const ma = new Multiaddr(a); + return ma.nodeAddress().address; + }); const promises = nodes.map(async (node, i) => { wakus[i] = await Waku.create({ @@ -38,7 +40,10 @@ export default function runAll(nodes) { }); }).then((peerId) => { console.log("connected", peerId.toB58String()); - expect(peerId.toB58String()).to.eq(peerIds[i]); + expect(peerId.toB58String()).to.eq( + peerIds[i], + `Could not connect to ${hostnames[i]}` + ); }); }); @@ -52,8 +57,6 @@ export default function runAll(nodes) { const id = uuidv4(); - expect(nodes.length).to.eq(3); - const promises = nodes.map(async (node, i) => { wakus[i] = await Waku.create({ bootstrap: { peers: [node] }, @@ -69,11 +72,16 @@ export default function runAll(nodes) { const contentTopic = `/waku-tests/1/relay-test-${id}/utf8`; const messages = []; + const hostnames = nodes.map((a) => { + const ma = new Multiaddr(a); + return ma.nodeAddress().address; + }); - wakus.forEach((waku) => { + wakus.forEach((waku, i) => { + messages[i] = []; waku.relay.addObserver( (message) => { - messages.push({ + messages[i].push({ msg: message.payloadAsUtf8, timestamp: message.timestamp, rcvd: new Date(), @@ -85,7 +93,7 @@ export default function runAll(nodes) { const relayPromises = wakus.map(async (waku, i) => { const msg = await WakuMessage.fromUtf8String( - `sent via ${nodes[i]} - ${id}`, + `sent via ${hostnames[i]} - ${id}`, contentTopic ); return waku.relay.send(msg); @@ -96,15 +104,31 @@ export default function runAll(nodes) { console.log(messages); - messages.forEach((msg) => { - const diff = msg.rcvd.getTime() - msg.timestamp.getTime(); - console.log(msg.timestamp, msg.rcvd, diff + "ms"); + messages.forEach((msgs) => { + msgs.forEach((msg) => { + const diff = msg.rcvd.getTime() - msg.timestamp.getTime(); + console.log(msg.timestamp, msg.rcvd, diff + "ms"); + }); }); - expect(messages.length).to.gte(nodes.length); + messages.forEach((msgs, i) => { + expect(msgs.length).to.eq( + nodes.length - 1, + `Unexpected number of messages received by ${hostnames[i]}` + ); + }); + // Checking that message sent by waku[i] for (let i = 0; i < wakus.length; i++) { - expect(messages.map((m) => m.msg)).to.contain(`sent via ${nodes[i]} - ${id}`); + // is received by waku[j] + for (let j = 0; j < wakus.length; j++) { + if (i === j) continue; + + expect(messages[j].map((m) => m.msg)).to.contain( + `sent via ${hostnames[i]} - ${id}`, + `Node connected to ${hostnames[j]} did not receive message sent via ${hostnames[i]}` + ); + } } }); @@ -166,7 +190,9 @@ export default function runAll(nodes) { expect(messages.length).to.gte(nodes.length); for (let i = 0; i < wakus.length; i++) { - expect(messages.map((m) => m.msg)).to.contain(`sent via ${nodes[i]} - ${id}`); + expect(messages.map((m) => m.msg)).to.contain( + `sent via ${nodes[i]} - ${id}` + ); } }); diff --git a/package.json b/package.json index e677815..fe473fb 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ }, "dependencies": { "js-waku": "^0.20.0", + "multiaddr": "^10.0.1", "uuid": "^8.3.2" } } diff --git a/yarn.lock b/yarn.lock index 85ffa50..0c8e14c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4634,6 +4634,7 @@ __metadata: eslint-config-prettier: ^8.5.0 js-waku: ^0.20.0 mocha: ^9.2.2 + multiaddr: ^10.0.1 npm-run-all: ^4.1.5 prettier: ^2.6.2 uuid: ^8.3.2