diff --git a/packages/tests/.mocharc.json b/packages/tests/.mocharc.json index e277cf89b1..72164c409b 100644 --- a/packages/tests/.mocharc.json +++ b/packages/tests/.mocharc.json @@ -7,5 +7,7 @@ "loader=ts-node/esm" ], "exit": true, - "retries": 3 + "retries": 4, + "parallel": true, + "jobs": 6 } diff --git a/packages/tests/README.md b/packages/tests/README.md index 90977c5c88..9a42689812 100644 --- a/packages/tests/README.md +++ b/packages/tests/README.md @@ -39,3 +39,10 @@ Therefore, you need to have `docker` installed on your machine to run the tests. ```bash WAKUNODE_IMAGE=image-name npm run test:node ``` + + +# Running tests in the CI + +- Tests are being run on standard Ubuntu GitHub Actions instances. +- To speed up execution, we run tests in parallel. After numerous attempts, we determined that using 6 threads strikes the best balance between execution speed and test reliability. Using more than this doesn't significantly decrease execution time and might even slow it down. +- To address occasional test flakiness, primarily due to Docker containers starting and stopping for each test and the concurrent execution of tests, we utilize the Mocha retry mechanism. diff --git a/packages/tests/src/message_collector.ts b/packages/tests/src/message_collector.ts index 5b26e56615..7aa7a54615 100644 --- a/packages/tests/src/message_collector.ts +++ b/packages/tests/src/message_collector.ts @@ -44,6 +44,7 @@ export class MessageCollector { if (typeof message.payload === "string") { return message.payload === text; } else if (message.payload instanceof Uint8Array) { + log(`Checking payload: ${bytesToUtf8(message.payload)}`); return isEqual(message.payload, utf8ToBytes(text)); } return false; diff --git a/packages/tests/src/node/dockerode.ts b/packages/tests/src/node/dockerode.ts index d5f7160b66..894010f326 100644 --- a/packages/tests/src/node/dockerode.ts +++ b/packages/tests/src/node/dockerode.ts @@ -18,7 +18,6 @@ export default class Dockerode { public containerId?: string; private static network: Docker.Network; - private static lastUsedIp = "172.18.0.1"; private containerIp: string; private constructor(imageName: string, containerIp: string) { @@ -70,15 +69,14 @@ export default class Dockerode { } private static getNextIp(): string { - const ipFragments = Dockerode.lastUsedIp.split("."); - let lastFragment = Number(ipFragments[3]); - lastFragment++; - if (lastFragment > 254) { - throw new Error("IP Address Range Exhausted"); - } - ipFragments[3] = lastFragment.toString(); - Dockerode.lastUsedIp = ipFragments.join("."); - return Dockerode.lastUsedIp; + const baseIpFragments = "172.18".split("."); + // Generate a random number between 0 and 255 for the last two fragments. + const secondLastFragment = Math.floor(Math.random() * 256); // For the .0 fragment + const lastFragment = Math.floor(Math.random() * 256); // For the last fragment + const newIp = [...baseIpFragments, secondLastFragment, lastFragment].join( + "." + ); + return newIp; } get container(): Docker.Container | undefined { @@ -159,18 +157,19 @@ export default class Dockerode { } async stop(): Promise { - if (!this.container) throw "containerId not set"; + if (!this.container) { + log("ContainerId not set"); + } else { + log( + `Shutting down container ID ${ + this.containerId + } at ${new Date().toLocaleTimeString()}` + ); - log( - `Shutting down container ID ${ - this.containerId - } at ${new Date().toLocaleTimeString()}` - ); + await this.container.stop(); - await this.container.stop(); - await this.container.remove(); - - delete this.containerId; + delete this.containerId; + } } private async confirmImageExistsOrPull(): Promise { diff --git a/packages/tests/src/node/node.ts b/packages/tests/src/node/node.ts index 68e9a68aeb..d9603df053 100644 --- a/packages/tests/src/node/node.ts +++ b/packages/tests/src/node/node.ts @@ -86,86 +86,8 @@ export class NimGoNode { return isGoWaku ? "go-waku" : "nwaku"; } - async start(args: Args = {}): Promise { - this.docker = await Dockerode.createInstance(DOCKER_IMAGE_NAME); - try { - await existsAsync(LOG_DIR); - } catch (e) { - try { - await mkdirAsync(LOG_DIR); - } catch (e) { - // Looks like 2 tests tried to create the director at the same time, - // it can be ignored - } - } - - await openAsync(this.logPath, "w"); - - const mergedArgs = defaultArgs(); - - // waku nodes takes some time to bind port so to decrease chances of conflict - // we also randomize the first port that portfinder will try - const startPort = Math.floor(Math.random() * (65535 - 1025) + 1025); - - const ports: number[] = await new Promise((resolve, reject) => { - portfinder.getPorts(4, { port: startPort }, (err, ports) => { - if (err) reject(err); - resolve(ports); - }); - }); - - if (isGoWaku && !args.logLevel) { - args.logLevel = LogLevel.Debug; - } - - const [rpcPort, tcpPort, websocketPort, discv5UdpPort] = ports; - this.rpcPort = rpcPort; - this.websocketPort = websocketPort; - - // `legacyFilter` is required to enable filter v1 with go-waku - const { legacyFilter = false, ..._args } = args; - - // Object.assign overrides the properties with the source (if there are conflicts) - Object.assign( - mergedArgs, - { - rpcPort, - tcpPort, - websocketPort, - ...(args?.peerExchange && { discv5UdpPort }), - ...(isGoWaku && { minRelayPeersToPublish: 0, legacyFilter }) - }, - { rpcAddress: "0.0.0.0" }, - _args - ); - - process.env.WAKUNODE2_STORE_MESSAGE_DB_URL = ""; - - if (this.docker.container) { - await this.docker.stop(); - } - - await this.docker.startContainer( - ports, - mergedArgs, - this.logPath, - WAKU_SERVICE_NODE_PARAMS - ); - - try { - log(`Waiting to see '${NODE_READY_LOG_LINE}' in ${this.type} logs`); - await this.waitForLog(NODE_READY_LOG_LINE, 15000); - if (process.env.CI) await delay(100); - log(`${this.type} node has been started`); - } catch (error) { - log(`Error starting ${this.type}: ${error}`); - if (this.docker.container) await this.docker.stop(); - throw error; - } - } - - async startWithRetries( - args: Args, + async start( + args: Args = {}, options: { retries?: number; } = { retries: 3 } @@ -173,9 +95,83 @@ export class NimGoNode { await pRetry( async () => { try { - await this.start(args); + this.docker = await Dockerode.createInstance(DOCKER_IMAGE_NAME); + try { + await existsAsync(LOG_DIR); + } catch (e) { + try { + await mkdirAsync(LOG_DIR); + } catch (e) { + // Looks like 2 tests tried to create the director at the same time, + // it can be ignored + } + } + + await openAsync(this.logPath, "w"); + + const mergedArgs = defaultArgs(); + + // waku nodes takes some time to bind port so to decrease chances of conflict + // we also randomize the first port that portfinder will try + const startPort = Math.floor(Math.random() * (65535 - 1025) + 1025); + + const ports: number[] = await new Promise((resolve, reject) => { + portfinder.getPorts(4, { port: startPort }, (err, ports) => { + if (err) reject(err); + resolve(ports); + }); + }); + + if (isGoWaku && !args.logLevel) { + args.logLevel = LogLevel.Debug; + } + + const [rpcPort, tcpPort, websocketPort, discv5UdpPort] = ports; + this.rpcPort = rpcPort; + this.websocketPort = websocketPort; + + // `legacyFilter` is required to enable filter v1 with go-waku + const { legacyFilter = false, ..._args } = args; + + // Object.assign overrides the properties with the source (if there are conflicts) + Object.assign( + mergedArgs, + { + rpcPort, + tcpPort, + websocketPort, + ...(args?.peerExchange && { discv5UdpPort }), + ...(isGoWaku && { minRelayPeersToPublish: 0, legacyFilter }) + }, + { rpcAddress: "0.0.0.0" }, + _args + ); + + process.env.WAKUNODE2_STORE_MESSAGE_DB_URL = ""; + + if (this.docker.container) { + await this.docker.stop(); + } + + await this.docker?.startContainer( + ports, + mergedArgs, + this.logPath, + WAKU_SERVICE_NODE_PARAMS + ); } catch (error) { log("Nwaku node failed to start:", error); + await this.stop(); + throw error; + } + try { + log(`Waiting to see '${NODE_READY_LOG_LINE}' in ${this.type} logs`); + await this.waitForLog(NODE_READY_LOG_LINE, 15000); + if (process.env.CI) await delay(100); + log(`${this.type} node has been started`); + } catch (error) { + log(`Error starting ${this.type}: ${error}`); + if (this.docker.container) await this.docker.stop(); throw error; } }, @@ -184,7 +180,7 @@ export class NimGoNode { } public async stop(): Promise { - await this.docker?.container?.stop(); + await this.docker?.stop(); delete this.docker; } @@ -381,20 +377,31 @@ export class NimGoNode { method: string, params: Array ): Promise { - log("RPC Query: ", method, params); - const res = await fetch(this.rpcUrl, { - method: "POST", - body: JSON.stringify({ - jsonrpc: "2.0", - id: 1, - method, - params - }), - headers: new Headers({ "Content-Type": "application/json" }) - }); - const json = await res.json(); - log(`RPC Response: `, JSON.stringify(json)); - return json.result; + return await pRetry( + async () => { + try { + log("RPC Query: ", method, params); + const res = await fetch(this.rpcUrl, { + method: "POST", + body: JSON.stringify({ + jsonrpc: "2.0", + id: 1, + method, + params + }), + headers: new Headers({ "Content-Type": "application/json" }) + }); + const json = await res.json(); + log(`RPC Response: `, JSON.stringify(json)); + return json.result; + } catch (error) { + log(`${this.rpcUrl} failed with error:`, error); + await delay(10); + throw error; + } + }, + { retries: 5 } + ); } private checkProcess(): void { diff --git a/packages/tests/src/teardown.ts b/packages/tests/src/teardown.ts index e68782999e..9c297d014b 100644 --- a/packages/tests/src/teardown.ts +++ b/packages/tests/src/teardown.ts @@ -1,4 +1,4 @@ -import { LightNode } from "@waku/interfaces"; +import { Waku } from "@waku/interfaces"; import debug from "debug"; import pRetry from "p-retry"; @@ -8,7 +8,7 @@ const log = debug("waku:test"); export async function tearDownNodes( nwakuNodes: NimGoNode | NimGoNode[], - wakuNodes: LightNode | LightNode[] + wakuNodes: Waku | Waku[] ): Promise { const nNodes = Array.isArray(nwakuNodes) ? nwakuNodes : [nwakuNodes]; const wNodes = Array.isArray(wakuNodes) ? wakuNodes : [wakuNodes]; diff --git a/packages/tests/tests/connection_manager.spec.ts b/packages/tests/tests/connection_manager.spec.ts index 341af18444..f3e479b28c 100644 --- a/packages/tests/tests/connection_manager.spec.ts +++ b/packages/tests/tests/connection_manager.spec.ts @@ -6,6 +6,7 @@ import { expect } from "chai"; import sinon, { SinonSpy, SinonStub } from "sinon"; import { delay } from "../dist/delay.js"; +import { tearDownNodes } from "../src/index.js"; const TEST_TIMEOUT = 10_000; const DELAY_MS = 1_000; @@ -18,7 +19,8 @@ describe("ConnectionManager", function () { }); afterEach(async () => { - await waku.stop(); + this.timeout(15000); + await tearDownNodes([], waku); }); describe("Events", () => { @@ -161,7 +163,7 @@ describe("ConnectionManager", function () { afterEach(async () => { this.timeout(15000); - await waku.stop(); + await tearDownNodes([], waku); isPeerTopicConfigured.restore(); sinon.restore(); }); diff --git a/packages/tests/tests/enr.node.spec.ts b/packages/tests/tests/enr.node.spec.ts index 4f4df2d62e..f6b7872b65 100644 --- a/packages/tests/tests/enr.node.spec.ts +++ b/packages/tests/tests/enr.node.spec.ts @@ -5,7 +5,7 @@ import { Protocols } from "@waku/interfaces"; import { createRelayNode } from "@waku/sdk"; import { expect } from "chai"; -import { makeLogFileName, NOISE_KEY_1 } from "../src/index.js"; +import { makeLogFileName, NOISE_KEY_1, tearDownNodes } from "../src/index.js"; import { NimGoNode } from "../src/node/node.js"; describe("ENR Interop: NimGoNode", function () { @@ -13,9 +13,8 @@ describe("ENR Interop: NimGoNode", function () { let nwaku: NimGoNode; afterEach(async function () { - !!nwaku && - nwaku.stop().catch((e) => console.log("Nwaku failed to stop", e)); - !!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e)); + this.timeout(15000); + await tearDownNodes(nwaku, waku); }); it("Relay", async function () { diff --git a/packages/tests/tests/ephemeral.node.spec.ts b/packages/tests/tests/ephemeral.node.spec.ts index 5a4b983eb9..5ba287e6fe 100644 --- a/packages/tests/tests/ephemeral.node.spec.ts +++ b/packages/tests/tests/ephemeral.node.spec.ts @@ -26,7 +26,8 @@ import { delay, makeLogFileName, NOISE_KEY_1, - NOISE_KEY_2 + NOISE_KEY_2, + tearDownNodes } from "../src/index.js"; import { NimGoNode } from "../src/node/node.js"; @@ -45,9 +46,8 @@ describe("Waku Message Ephemeral field", () => { let subscription: IFilterSubscription; afterEach(async function () { - !!nwaku && - nwaku.stop().catch((e) => console.log("Nwaku failed to stop", e)); - !!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e)); + this.timeout(15000); + await tearDownNodes(nwaku, waku); }); beforeEach(async function () { @@ -165,8 +165,7 @@ describe("Waku Message Ephemeral field", () => { expect(messages?.length).eq(0); - !!waku1 && waku1.stop().catch((e) => console.log("Waku failed to stop", e)); - !!waku2 && waku2.stop().catch((e) => console.log("Waku failed to stop", e)); + await tearDownNodes([], [waku1, waku2]); }); it("Ephemeral field is preserved - encoder v0", async function () { diff --git a/packages/tests/tests/filter/push.node.spec.ts b/packages/tests/tests/filter/push.node.spec.ts index 69be141025..19f65bd5b2 100644 --- a/packages/tests/tests/filter/push.node.spec.ts +++ b/packages/tests/tests/filter/push.node.spec.ts @@ -265,7 +265,7 @@ describe("Waku Filter V2: FilterPush", function () { expect(await messageCollector.waitForMessages(1)).to.eq(true); // Restart nwaku node - await nwaku.stop(); + await tearDownNodes(nwaku, []); await nwaku.start(); await waitForRemotePeer(waku, [Protocols.Filter, Protocols.LightPush]); diff --git a/packages/tests/tests/filter/subscribe.node.spec.ts b/packages/tests/tests/filter/subscribe.node.spec.ts index ebb2f072a8..9bde81f753 100644 --- a/packages/tests/tests/filter/subscribe.node.spec.ts +++ b/packages/tests/tests/filter/subscribe.node.spec.ts @@ -321,7 +321,11 @@ describe("Waku Filter V2: Subscribe", function () { // Set up and start a new nwaku node nwaku2 = new NimGoNode(makeLogFileName(this) + "2"); - await nwaku2.start({ filter: true, lightpush: true, relay: true }); + await nwaku2.start({ + filter: true, + lightpush: true, + relay: true + }); await waku.dial(await nwaku2.getMultiaddrWithId()); await waitForRemotePeer(waku, [Protocols.Filter, Protocols.LightPush]); const subscription2 = await waku.filter.createSubscription( diff --git a/packages/tests/tests/filter/utils.ts b/packages/tests/tests/filter/utils.ts index b1a8c102e4..0eee4b12a1 100644 --- a/packages/tests/tests/filter/utils.ts +++ b/packages/tests/tests/filter/utils.ts @@ -69,7 +69,7 @@ export async function runNodes( ): Promise<[NimGoNode, LightNode]> { const nwaku = new NimGoNode(makeLogFileName(context)); - await nwaku.startWithRetries( + await nwaku.start( { filter: true, lightpush: true, diff --git a/packages/tests/tests/light-push/utils.ts b/packages/tests/tests/light-push/utils.ts index 548dd34e4f..5632f265ff 100644 --- a/packages/tests/tests/light-push/utils.ts +++ b/packages/tests/tests/light-push/utils.ts @@ -17,7 +17,7 @@ export async function runNodes( pubSubTopics: string[] ): Promise<[NimGoNode, LightNode]> { const nwaku = new NimGoNode(makeLogFileName(context)); - await nwaku.startWithRetries( + await nwaku.start( { lightpush: true, relay: true, topic: pubSubTopics }, { retries: 3 } ); diff --git a/packages/tests/tests/multiaddr.node.spec.ts b/packages/tests/tests/multiaddr.node.spec.ts index 352a9b1673..6b9dd34415 100644 --- a/packages/tests/tests/multiaddr.node.spec.ts +++ b/packages/tests/tests/multiaddr.node.spec.ts @@ -3,16 +3,15 @@ import type { Waku } from "@waku/interfaces"; import { createLightNode } from "@waku/sdk"; import { expect } from "chai"; -import { NimGoNode } from "../src/index.js"; +import { NimGoNode, tearDownNodes } from "../src/index.js"; describe("dials multiaddr", function () { let waku: Waku; let nwaku: NimGoNode; afterEach(async function () { - !!nwaku && - nwaku.stop().catch((e) => console.log("Nwaku failed to stop", e)); - !!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e)); + this.timeout(15000); + await tearDownNodes(nwaku, waku); }); it("TLS", async function () { diff --git a/packages/tests/tests/peer_exchange.node.spec.ts b/packages/tests/tests/peer_exchange.node.spec.ts index 716f93c3ce..657d2c1781 100644 --- a/packages/tests/tests/peer_exchange.node.spec.ts +++ b/packages/tests/tests/peer_exchange.node.spec.ts @@ -9,6 +9,7 @@ import { createLightNode, Libp2pComponents } from "@waku/sdk"; import { expect } from "chai"; import { delay } from "../src/delay.js"; +import { tearDownNodes } from "../src/index.js"; import { makeLogFileName } from "../src/log_file.js"; import { NimGoNode } from "../src/node/node.js"; @@ -24,10 +25,8 @@ describe("Peer Exchange", () => { }); afterEach(async function () { - this.timeout(10_000); - await nwaku1?.stop(); - await nwaku2?.stop(); - await waku?.stop(); + this.timeout(15000); + await tearDownNodes([nwaku1, nwaku2], waku); }); it("nwaku interop", async function () { @@ -126,9 +125,8 @@ describe("Peer Exchange", () => { return new PeerExchangeDiscovery(waku.libp2p.components); }, teardown: async () => { - await nwaku1?.stop(); - await nwaku2?.stop(); - await waku?.stop(); + this.timeout(15000); + await tearDownNodes([nwaku1, nwaku2], waku); } }); }); diff --git a/packages/tests/tests/peer_exchange.optional.spec.ts b/packages/tests/tests/peer_exchange.optional.spec.ts index a8f2544f8a..6c940c252f 100644 --- a/packages/tests/tests/peer_exchange.optional.spec.ts +++ b/packages/tests/tests/peer_exchange.optional.spec.ts @@ -8,12 +8,15 @@ import { wakuPeerExchangeDiscovery } from "@waku/peer-exchange"; import { createLightNode } from "@waku/sdk"; import { expect } from "chai"; +import { tearDownNodes } from "../src"; + describe("Peer Exchange", () => { describe("Auto Discovery", function () { let waku: LightNode; afterEach(async function () { - await waku?.stop(); + this.timeout(15000); + await tearDownNodes([], waku); }); const testCases: [Fleet, number][] = [ diff --git a/packages/tests/tests/relay.node.spec.ts b/packages/tests/tests/relay.node.spec.ts index 8d6fae2f50..bb3045d29f 100644 --- a/packages/tests/tests/relay.node.spec.ts +++ b/packages/tests/tests/relay.node.spec.ts @@ -30,7 +30,8 @@ import { MessageCollector, NOISE_KEY_1, NOISE_KEY_2, - NOISE_KEY_3 + NOISE_KEY_3, + tearDownNodes } from "../src/index.js"; import { MessageRpcResponse } from "../src/node/interfaces.js"; import { base64ToUtf8, NimGoNode } from "../src/node/node.js"; @@ -369,12 +370,24 @@ describe("Waku Relay [node only]", () => { true ); - expect(msgCollector1.hasMessage(testItem.pubsub, "M2")).to.be.true; - expect(msgCollector1.hasMessage(testItem.pubsub, "M3")).to.be.true; - expect(msgCollector2.hasMessage(testItem.pubsub, "M1")).to.be.true; - expect(msgCollector2.hasMessage(testItem.pubsub, "M3")).to.be.true; - expect(msgCollector3.hasMessage(testItem.pubsub, "M1")).to.be.true; - expect(msgCollector3.hasMessage(testItem.pubsub, "M2")).to.be.true; + expect( + msgCollector1.hasMessage(testItem.encoder.contentTopic, "M2") + ).to.eq(true); + expect( + msgCollector1.hasMessage(testItem.encoder.contentTopic, "M3") + ).to.eq(true); + expect( + msgCollector2.hasMessage(testItem.encoder.contentTopic, "M1") + ).to.eq(true); + expect( + msgCollector2.hasMessage(testItem.encoder.contentTopic, "M3") + ).to.eq(true); + expect( + msgCollector3.hasMessage(testItem.encoder.contentTopic, "M1") + ).to.eq(true); + expect( + msgCollector3.hasMessage(testItem.encoder.contentTopic, "M2") + ).to.eq(true); }); }); @@ -447,16 +460,14 @@ describe("Waku Relay [node only]", () => { expect(await msgCollector3.waitForMessages(2, { exact: true })).to.eq( true ); - - expect(msgCollector1.hasMessage(DefaultPubSubTopic, "M3")).to.be.true; - expect(msgCollector1.hasMessage(CustomPubSubTopic, "M4")).to.be.true; - expect(msgCollector1.hasMessage(DefaultPubSubTopic, "M5")).to.be.true; - expect(msgCollector1.hasMessage(DefaultPubSubTopic, "M1")).to.be.true; - expect(msgCollector1.hasMessage(CustomPubSubTopic, "M2")).to.be.true; - expect(msgCollector1.hasMessage(DefaultPubSubTopic, "M5")).to.be.true; - expect(msgCollector2.hasMessage(CustomPubSubTopic, "M1")).to.be.true; - expect(msgCollector2.hasMessage(DefaultPubSubTopic, "M3")).to.be.true; - expect(msgCollector3.hasMessage(DefaultPubSubTopic, "M1")).to.be.true; + expect(msgCollector1.hasMessage(TestContentTopic, "M3")).to.eq(true); + expect(msgCollector1.hasMessage(CustomContentTopic, "M4")).to.eq(true); + expect(msgCollector1.hasMessage(TestContentTopic, "M5")).to.eq(true); + expect(msgCollector2.hasMessage(TestContentTopic, "M1")).to.eq(true); + expect(msgCollector2.hasMessage(CustomContentTopic, "M2")).to.eq(true); + expect(msgCollector2.hasMessage(TestContentTopic, "M5")).to.eq(true); + expect(msgCollector3.hasMessage(TestContentTopic, "M1")).to.eq(true); + expect(msgCollector3.hasMessage(TestContentTopic, "M3")).to.eq(true); }); it("n1 and n2 uses a custom pubsub, n3 uses the default pubsub", async function () { @@ -602,9 +613,8 @@ describe("Waku Relay [node only]", () => { }); afterEach(async function () { - !!nwaku && - nwaku.stop().catch((e) => console.log("Nwaku failed to stop", e)); - !!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e)); + this.timeout(15000); + await tearDownNodes(nwaku, waku); }); it("nwaku subscribes", async function () { @@ -674,12 +684,7 @@ describe("Waku Relay [node only]", () => { let nwaku: NimGoNode; afterEach(async function () { - !!nwaku && - nwaku.stop().catch((e) => console.log("Nwaku failed to stop", e)); - !!waku1 && - waku1.stop().catch((e) => console.log("Waku failed to stop", e)); - !!waku2 && - waku2.stop().catch((e) => console.log("Waku failed to stop", e)); + await tearDownNodes(nwaku, [waku1, waku2]); }); it("Js publishes, other Js receives", async function () { diff --git a/packages/tests/tests/sharding/peer_management.spec.ts b/packages/tests/tests/sharding/peer_management.spec.ts index 38f4c3da9f..36152fe3c7 100644 --- a/packages/tests/tests/sharding/peer_management.spec.ts +++ b/packages/tests/tests/sharding/peer_management.spec.ts @@ -9,6 +9,7 @@ import Sinon, { SinonSpy } from "sinon"; import { delay } from "../../src/delay.js"; import { makeLogFileName } from "../../src/log_file.js"; import { NimGoNode } from "../../src/node/node.js"; +import { tearDownNodes } from "../../src/teardown.js"; chai.use(chaiAsPromised); @@ -22,18 +23,15 @@ describe("Static Sharding: Peer Management", function () { let attemptDialSpy: SinonSpy; beforeEach(async function () { + this.timeout(15000); nwaku1 = new NimGoNode(makeLogFileName(this) + "1"); nwaku2 = new NimGoNode(makeLogFileName(this) + "2"); nwaku3 = new NimGoNode(makeLogFileName(this) + "3"); }); afterEach(async function () { - this.timeout(5_000); - await nwaku1?.stop(); - await nwaku2?.stop(); - await nwaku3?.stop(); - !!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e)); - + this.timeout(15000); + await tearDownNodes([nwaku1, nwaku2, nwaku3], waku); attemptDialSpy && attemptDialSpy.restore(); }); diff --git a/packages/tests/tests/sharding/running_nodes.spec.ts b/packages/tests/tests/sharding/running_nodes.spec.ts index 09c474558c..26ff23805e 100644 --- a/packages/tests/tests/sharding/running_nodes.spec.ts +++ b/packages/tests/tests/sharding/running_nodes.spec.ts @@ -2,6 +2,7 @@ import { LightNode } from "@waku/interfaces"; import { createEncoder, createLightNode, utf8ToBytes } from "@waku/sdk"; import { expect } from "chai"; +import { tearDownNodes } from "../../src/index.js"; import { makeLogFileName } from "../../src/log_file.js"; import { NimGoNode } from "../../src/node/node.js"; @@ -21,9 +22,8 @@ describe("Static Sharding: Running Nodes", () => { }); afterEach(async function () { - !!nwaku && - nwaku.stop().catch((e) => console.log("Nwaku failed to stop", e)); - !!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e)); + this.timeout(15000); + await tearDownNodes(nwaku, waku); }); it("configure the node with multiple pubsub topics", async function () { @@ -42,16 +42,16 @@ describe("Static Sharding: Running Nodes", () => { pubSubTopic: PubSubTopic2 }); - const request1 = waku.lightPush.send(encoder1, { + const request1 = await waku.lightPush.send(encoder1, { payload: utf8ToBytes("Hello World") }); - const request2 = waku.lightPush.send(encoder2, { + const request2 = await waku.lightPush.send(encoder2, { payload: utf8ToBytes("Hello World") }); - await expect(request1).to.be.fulfilled; - await expect(request2).to.be.fulfilled; + expect(request1.recipients.length).to.eq(0); + expect(request2.recipients.length).to.eq(0); }); it("using a protocol with unconfigured pubsub topic should fail", async function () { @@ -66,11 +66,20 @@ describe("Static Sharding: Running Nodes", () => { pubSubTopic: PubSubTopic2 }); - // the following request should throw an error - const request = waku.lightPush.send(encoder, { - payload: utf8ToBytes("Hello World") - }); - - await expect(request).to.be.rejectedWith(Error); + try { + await waku.lightPush.send(encoder, { + payload: utf8ToBytes("Hello World") + }); + throw new Error("The request should've thrown an error"); + } catch (err) { + if ( + !(err instanceof Error) || + !err.message.includes( + `PubSub topic ${PubSubTopic2} has not been configured on this instance. Configured topics are: ${PubSubTopic1}` + ) + ) { + throw err; + } + } }); }); diff --git a/packages/tests/tests/store/cursor.node.spec.ts b/packages/tests/tests/store/cursor.node.spec.ts index 20b0f20ec9..007931b390 100644 --- a/packages/tests/tests/store/cursor.node.spec.ts +++ b/packages/tests/tests/store/cursor.node.spec.ts @@ -23,7 +23,7 @@ describe("Waku Store, cursor", function () { beforeEach(async function () { this.timeout(15000); nwaku = new NimGoNode(makeLogFileName(this)); - await nwaku.startWithRetries({ store: true, lightpush: true, relay: true }); + await nwaku.start({ store: true, lightpush: true, relay: true }); await nwaku.ensureSubscriptions(); }); diff --git a/packages/tests/tests/store/error_handling.node.spec.ts b/packages/tests/tests/store/error_handling.node.spec.ts index 37fa6d8971..c5eb3d5bc3 100644 --- a/packages/tests/tests/store/error_handling.node.spec.ts +++ b/packages/tests/tests/store/error_handling.node.spec.ts @@ -20,7 +20,7 @@ describe("Waku Store, error handling", function () { beforeEach(async function () { this.timeout(15000); nwaku = new NimGoNode(makeLogFileName(this)); - await nwaku.startWithRetries({ store: true, lightpush: true, relay: true }); + await nwaku.start({ store: true, lightpush: true, relay: true }); await nwaku.ensureSubscriptions(); waku = await startAndConnectLightNode(nwaku); }); diff --git a/packages/tests/tests/store/index.node.spec.ts b/packages/tests/tests/store/index.node.spec.ts index f519535a72..e9eb887384 100644 --- a/packages/tests/tests/store/index.node.spec.ts +++ b/packages/tests/tests/store/index.node.spec.ts @@ -54,7 +54,7 @@ describe("Waku Store, general", function () { beforeEach(async function () { this.timeout(15000); nwaku = new NimGoNode(makeLogFileName(this)); - await nwaku.startWithRetries({ store: true, lightpush: true, relay: true }); + await nwaku.start({ store: true, lightpush: true, relay: true }); await nwaku.ensureSubscriptions(); }); @@ -91,7 +91,7 @@ describe("Waku Store, general", function () { }), DefaultPubSubTopic ) - ).to.be.true; + ).to.eq(true); await delay(1); // to ensure each timestamp is unique. } @@ -148,7 +148,7 @@ describe("Waku Store, general", function () { }), DefaultPubSubTopic ) - ).to.be.true; + ).to.eq(true); await delay(1); // to ensure each timestamp is unique. } diff --git a/packages/tests/tests/store/order.node.spec.ts b/packages/tests/tests/store/order.node.spec.ts index 98f34b4bcb..21cc15cf28 100644 --- a/packages/tests/tests/store/order.node.spec.ts +++ b/packages/tests/tests/store/order.node.spec.ts @@ -21,7 +21,7 @@ describe("Waku Store, order", function () { beforeEach(async function () { this.timeout(15000); nwaku = new NimGoNode(makeLogFileName(this)); - await nwaku.startWithRetries({ store: true, lightpush: true, relay: true }); + await nwaku.start({ store: true, lightpush: true, relay: true }); await nwaku.ensureSubscriptions(); }); diff --git a/packages/tests/tests/store/page_size.node.spec.ts b/packages/tests/tests/store/page_size.node.spec.ts index 4f1b466a10..cfa924ce95 100644 --- a/packages/tests/tests/store/page_size.node.spec.ts +++ b/packages/tests/tests/store/page_size.node.spec.ts @@ -19,7 +19,7 @@ describe("Waku Store, page size", function () { beforeEach(async function () { this.timeout(15000); nwaku = new NimGoNode(makeLogFileName(this)); - await nwaku.startWithRetries({ store: true, lightpush: true, relay: true }); + await nwaku.start({ store: true, lightpush: true, relay: true }); await nwaku.ensureSubscriptions(); }); diff --git a/packages/tests/tests/store/sorting.node.spec.ts b/packages/tests/tests/store/sorting.node.spec.ts index b7d232cec6..2b737a5184 100644 --- a/packages/tests/tests/store/sorting.node.spec.ts +++ b/packages/tests/tests/store/sorting.node.spec.ts @@ -19,7 +19,7 @@ describe("Waku Store, sorting", function () { beforeEach(async function () { this.timeout(15000); nwaku = new NimGoNode(makeLogFileName(this)); - await nwaku.startWithRetries({ store: true, lightpush: true, relay: true }); + await nwaku.start({ store: true, lightpush: true, relay: true }); await nwaku.ensureSubscriptions(); }); diff --git a/packages/tests/tests/store/time_filter.node.spec.ts b/packages/tests/tests/store/time_filter.node.spec.ts index f53ff23b8f..44315e013f 100644 --- a/packages/tests/tests/store/time_filter.node.spec.ts +++ b/packages/tests/tests/store/time_filter.node.spec.ts @@ -18,7 +18,7 @@ describe("Waku Store, time filter", function () { beforeEach(async function () { this.timeout(15000); nwaku = new NimGoNode(makeLogFileName(this)); - await nwaku.startWithRetries({ store: true, lightpush: true, relay: true }); + await nwaku.start({ store: true, lightpush: true, relay: true }); await nwaku.ensureSubscriptions(); }); @@ -48,7 +48,7 @@ describe("Waku Store, time filter", function () { timestamp: msgTimestamp }) ) - ).to.be.true; + ).to.eq(true); waku = await startAndConnectLightNode(nwaku); @@ -93,7 +93,7 @@ describe("Waku Store, time filter", function () { timestamp: msgTimestamp }) ) - ).to.be.true; + ).to.eq(true); waku = await startAndConnectLightNode(nwaku); diff --git a/packages/tests/tests/store/utils.ts b/packages/tests/tests/store/utils.ts index d4ccac15ce..f7ee512fee 100644 --- a/packages/tests/tests/store/utils.ts +++ b/packages/tests/tests/store/utils.ts @@ -42,7 +42,7 @@ export async function sendMessages( }), pubSubTopic ) - ).to.be.true; + ).to.eq(true); await delay(1); // to ensure each timestamp is unique. } } diff --git a/packages/tests/tests/utils.spec.ts b/packages/tests/tests/utils.spec.ts index 89c77ebd8a..c0001adff2 100644 --- a/packages/tests/tests/utils.spec.ts +++ b/packages/tests/tests/utils.spec.ts @@ -38,7 +38,11 @@ describe("Util: toAsyncIterator: Filter", () => { beforeEach(async function () { this.timeout(15000); nwaku = new NimGoNode(makeLogFileName(this)); - await nwaku.start({ filter: true, lightpush: true, relay: true }); + await nwaku.start({ + filter: true, + lightpush: true, + relay: true + }); waku = await createLightNode({ staticNoiseKey: NOISE_KEY_1, libp2p: { addresses: { listen: ["/ip4/0.0.0.0/tcp/0/ws"] } } diff --git a/packages/tests/tests/wait_for_remote_peer.node.spec.ts b/packages/tests/tests/wait_for_remote_peer.node.spec.ts index 3034fdbcb1..6fedbdd0ee 100644 --- a/packages/tests/tests/wait_for_remote_peer.node.spec.ts +++ b/packages/tests/tests/wait_for_remote_peer.node.spec.ts @@ -4,21 +4,22 @@ import { Protocols } from "@waku/interfaces"; import { createLightNode, createRelayNode } from "@waku/sdk"; import { expect } from "chai"; -import { delay, makeLogFileName, NOISE_KEY_1 } from "../src/index.js"; +import { + delay, + makeLogFileName, + NOISE_KEY_1, + tearDownNodes +} from "../src/index.js"; import { NimGoNode } from "../src/node/node.js"; describe("Wait for remote peer", function () { let waku1: RelayNode; let waku2: LightNode; - let nwaku: NimGoNode | undefined; + let nwaku: NimGoNode; afterEach(async function () { - if (nwaku) { - nwaku.stop().catch((e) => console.log("Nwaku failed to stop", e)); - nwaku = undefined; - } - waku1?.stop().catch((e) => console.log("Waku failed to stop", e)); - waku2?.stop().catch((e) => console.log("Waku failed to stop", e)); + this.timeout(15000); + await tearDownNodes(nwaku, [waku1, waku2]); }); it("Relay - dialed first", async function () { diff --git a/packages/tests/tests/waku.node.optional.spec.ts b/packages/tests/tests/waku.node.optional.spec.ts index 7025cdbcfc..5a684e820c 100644 --- a/packages/tests/tests/waku.node.optional.spec.ts +++ b/packages/tests/tests/waku.node.optional.spec.ts @@ -4,15 +4,15 @@ import { LightNode } from "@waku/interfaces"; import { createLightNode } from "@waku/sdk"; import { expect } from "chai"; -import { makeLogFileName, NimGoNode } from "../src/index.js"; +import { makeLogFileName, NimGoNode, tearDownNodes } from "../src/index.js"; describe("Use static and several ENR trees for bootstrap", function () { let waku: LightNode; let nwaku: NimGoNode; afterEach(async function () { - !!nwaku && (await nwaku.stop()); - !!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e)); + this.timeout(15000); + await tearDownNodes(nwaku, waku); }); it("", async function () { diff --git a/packages/tests/tests/waku.node.spec.ts b/packages/tests/tests/waku.node.spec.ts index 3cc14ae49a..6b399718fc 100644 --- a/packages/tests/tests/waku.node.spec.ts +++ b/packages/tests/tests/waku.node.spec.ts @@ -90,7 +90,8 @@ describe("Waku Dial [node only]", function () { }); await waku.start(); await waku.dial(multiAddrWithId); - await nwaku.stop(); + + await tearDownNodes(nwaku, []); await waku.lightPush?.send(TestEncoder, { payload: utf8ToBytes("hello world") }); @@ -190,8 +191,8 @@ describe("Decryption Keys", () => { }); afterEach(async function () { - !!waku1 && waku1.stop().catch((e) => console.log("Waku failed to stop", e)); - !!waku2 && waku2.stop().catch((e) => console.log("Waku failed to stop", e)); + this.timeout(15000); + await tearDownNodes([], [waku1, waku2]); }); it("Used by Waku Relay", async function () { @@ -232,8 +233,8 @@ describe("User Agent", () => { let waku2: Waku; afterEach(async function () { - !!waku1 && waku1.stop().catch((e) => console.log("Waku failed to stop", e)); - !!waku2 && waku2.stop().catch((e) => console.log("Waku failed to stop", e)); + this.timeout(15000); + await tearDownNodes([], [waku1, waku2]); }); it("Sets default value correctly", async function () {