waku-tests/index.js

271 lines
7.5 KiB
JavaScript
Raw Normal View History

2022-04-20 00:06:48 +00:00
import { expect } from "chai";
import { describe } from "mocha";
import { Multiaddr } from "multiaddr";
2022-09-05 06:29:55 +00:00
import { Protocols, WakuMessage } from "js-waku";
import { createWaku } from "js-waku/lib/create_waku";
2022-04-20 02:07:40 +00:00
import { v4 as uuidv4 } from "uuid";
import delay from "./delay.js";
2022-09-05 06:29:55 +00:00
import { PeerDiscoveryStaticPeers } from "js-waku/lib/peer_discovery_static_list";
import { waitForRemotePeer } from "js-waku/lib/wait_for_remote_peer";
2022-04-20 02:07:40 +00:00
2022-04-20 02:14:08 +00:00
export default function runAll(nodes) {
describe("Run Waku Test Suite", () => {
2022-04-20 01:15:11 +00:00
const wakus = [];
2022-04-20 00:06:48 +00:00
afterEach(function () {
wakus.forEach((waku) => {
waku.stop().catch((e) => console.log("Waku failed to stop", e));
});
});
it("Connect", async function () {
2022-04-20 01:15:11 +00:00
this.timeout(20000);
2022-04-20 00:06:48 +00:00
const peerIds = nodes.map((a) => {
const ma = new Multiaddr(a);
2022-04-20 01:15:11 +00:00
return ma.getPeerId();
2022-04-20 00:06:48 +00:00
});
2022-04-20 04:14:41 +00:00
const hostnames = nodes.map((a) => {
const ma = new Multiaddr(a);
return ma.nodeAddress().address;
});
2022-04-20 00:06:48 +00:00
const promises = nodes.map(async (node, i) => {
2022-09-05 06:29:55 +00:00
wakus[i] = await createWaku({
libp2p: {
peerDiscovery: [new PeerDiscoveryStaticPeers([node])],
},
2022-04-20 00:06:48 +00:00
});
2022-09-05 06:29:55 +00:00
await wakus[i].start();
2022-04-20 00:06:48 +00:00
return new Promise((resolve) => {
2022-09-05 06:29:55 +00:00
wakus[i].libp2p.connectionManager.addEventListener(
"peer:connect",
(evt) => {
resolve(evt.detail.remotePeer);
}
);
2022-04-20 02:14:08 +00:00
}).then((peerId) => {
2022-09-05 06:29:55 +00:00
console.log("connected", peerId.toString());
expect(peerId.toString()).to.eq(
2022-04-20 04:14:41 +00:00
peerIds[i],
`Could not connect to ${hostnames[i]}`
);
2022-04-20 00:06:48 +00:00
});
});
await Promise.all(promises);
});
it("Relay", async function () {
this.timeout(60000);
2022-04-20 02:07:40 +00:00
const id = uuidv4();
2022-04-20 00:06:48 +00:00
const promises = nodes.map(async (node, i) => {
2022-09-05 06:29:55 +00:00
wakus[i] = await createWaku({
libp2p: {
peerDiscovery: [new PeerDiscoveryStaticPeers([node])],
},
2022-04-20 00:06:48 +00:00
});
2022-09-05 06:29:55 +00:00
await wakus[i].start();
await waitForRemotePeer(wakus[i], [Protocols.Relay]);
2022-04-20 00:06:48 +00:00
console.log(node + ": ready");
});
await Promise.all(promises);
// All connected and relay ready
2022-04-20 03:47:28 +00:00
const contentTopic = `/waku-tests/1/relay-test-${id}/utf8`;
2022-04-20 00:06:48 +00:00
2022-04-20 01:15:11 +00:00
const messages = [];
2022-04-20 04:14:41 +00:00
const hostnames = nodes.map((a) => {
const ma = new Multiaddr(a);
return ma.nodeAddress().address;
});
2022-04-20 00:06:48 +00:00
2022-04-20 04:14:41 +00:00
wakus.forEach((waku, i) => {
messages[i] = [];
2022-04-20 00:06:48 +00:00
waku.relay.addObserver(
(message) => {
2022-04-20 04:14:41 +00:00
messages[i].push({
2022-04-20 00:06:48 +00:00
msg: message.payloadAsUtf8,
2022-04-20 01:15:11 +00:00
timestamp: message.timestamp,
2022-04-20 00:06:48 +00:00
rcvd: new Date(),
});
},
[contentTopic]
);
});
const relayPromises = wakus.map(async (waku, i) => {
const msg = await WakuMessage.fromUtf8String(
2022-04-20 04:14:41 +00:00
`sent via ${hostnames[i]} - ${id}`,
2022-04-20 00:06:48 +00:00
contentTopic
);
return waku.relay.send(msg);
2022-04-20 00:06:48 +00:00
});
await Promise.all(relayPromises);
await delay(30000);
console.log(messages);
2022-04-20 04:14:41 +00:00
messages.forEach((msgs) => {
msgs.forEach((msg) => {
const diff = msg.rcvd.getTime() - msg.timestamp.getTime();
console.log(msg.timestamp, msg.rcvd, diff + "ms");
});
2022-04-20 00:06:48 +00:00
});
2022-04-20 04:14:41 +00:00
// Checking that message sent by waku[i]
2022-04-20 00:06:48 +00:00
for (let i = 0; i < wakus.length; i++) {
2022-04-20 04:14:41 +00:00
// 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]}`
);
}
2022-04-20 00:06:48 +00:00
}
});
it("Send via Light Push, receive via Relay", async function () {
2022-04-20 00:06:48 +00:00
this.timeout(60000);
2022-04-20 02:07:40 +00:00
const id = uuidv4();
2022-04-20 00:06:48 +00:00
const promises = nodes.map(async (node, i) => {
2022-09-05 06:29:55 +00:00
wakus[i] = await createWaku({
libp2p: {
peerDiscovery: [new PeerDiscoveryStaticPeers([node])],
},
2022-04-20 00:06:48 +00:00
});
2022-09-05 06:29:55 +00:00
await wakus[i].start();
await waitForRemotePeer(wakus[i], [
Protocols.LightPush,
Protocols.Relay,
]);
2022-04-20 00:06:48 +00:00
console.log(node + ": ready");
});
await Promise.all(promises);
// All connected and relay ready
2022-04-20 03:47:28 +00:00
const contentTopic = `/waku-tests/1/light-push-${id}/utf8`;
2022-04-20 00:06:48 +00:00
2022-04-20 01:15:11 +00:00
const messages = [];
const hostnames = nodes.map((a) => {
const ma = new Multiaddr(a);
return ma.nodeAddress().address;
});
2022-04-20 00:06:48 +00:00
wakus.forEach((waku, i) => {
messages[i] = [];
2022-04-20 00:06:48 +00:00
waku.relay.addObserver(
(message) => {
messages[i].push({
2022-04-20 00:06:48 +00:00
msg: message.payloadAsUtf8,
2022-04-20 01:15:11 +00:00
timestamp: message.timestamp,
2022-04-20 00:06:48 +00:00
rcvd: new Date(),
});
},
[contentTopic]
);
});
const relayPromises = wakus.map(async (waku, i) => {
const msg = await WakuMessage.fromUtf8String(
`sent via ${hostnames[i]} - ${id}`,
2022-04-20 00:06:48 +00:00
contentTopic
);
return waku.lightPush.push(msg);
2022-04-20 00:06:48 +00:00
});
await Promise.all(relayPromises);
await delay(30000);
2022-04-20 00:06:48 +00:00
console.log(messages);
messages.forEach((msgs) => {
msgs.forEach((msg) => {
const diff = msg.rcvd.getTime() - msg.timestamp.getTime();
console.log(msg.timestamp, msg.rcvd, diff + "ms");
});
2022-04-20 00:06:48 +00:00
});
// Checking that message sent by waku[i]
2022-04-20 00:06:48 +00:00
for (let i = 0; i < wakus.length; i++) {
// 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]}`
);
}
2022-04-20 00:06:48 +00:00
}
});
it("Retrieve from Store, sent via Light push", async function () {
2022-04-20 00:06:48 +00:00
this.timeout(30000);
2022-04-20 02:07:40 +00:00
const id = uuidv4();
2022-04-20 00:06:48 +00:00
const promises = nodes.map(async (node, i) => {
2022-09-05 06:29:55 +00:00
wakus[i] = await createWaku({
libp2p: {
peerDiscovery: [new PeerDiscoveryStaticPeers([node])],
},
2022-04-20 00:06:48 +00:00
});
2022-09-05 06:29:55 +00:00
await wakus[i].start();
await waitForRemotePeer(wakus[i], [Protocols.Relay, Protocols.Store]);
2022-04-20 00:06:48 +00:00
console.log(node + ": ready");
});
await Promise.all(promises);
// All connected and relay ready
const hostnames = nodes.map((a) => {
const ma = new Multiaddr(a);
return ma.nodeAddress().address;
});
2022-04-20 03:47:28 +00:00
const contentTopic = `/waku-tests/1/store-test-${id}/utf8`;
2022-04-20 00:06:48 +00:00
const pushPromises = wakus.map(async (waku, i) => {
2022-04-20 00:06:48 +00:00
const msg = await WakuMessage.fromUtf8String(
2022-04-20 03:47:28 +00:00
`sent via ${nodes[i]} - ${id}`,
2022-04-20 00:06:48 +00:00
contentTopic
);
return waku.lightPush.push(msg);
2022-04-20 00:06:48 +00:00
});
await Promise.all(pushPromises);
2022-04-20 00:06:48 +00:00
await delay(5000);
const storePromises = wakus.map(async (waku, index) => {
const messages = await waku.store.queryHistory([contentTopic]);
const payloads = messages.map((msg) => msg.payloadAsUtf8);
console.log(index, payloads);
for (let i = 0; i < wakus.length; i++) {
expect(payloads).to.contain(
`sent via ${nodes[i]} - ${id}`,
`Store ${hostnames[index]} did not contain message sent via ${hostnames[i]}`
);
2022-04-20 00:06:48 +00:00
}
});
await Promise.all(storePromises);
});
});
2022-04-20 02:14:08 +00:00
}