From 3acc4fc86f586e8cf3861549a46225ebe74a95d2 Mon Sep 17 00:00:00 2001 From: "fryorcraken.eth" Date: Thu, 21 Sep 2023 11:51:00 +1000 Subject: [PATCH 1/4] refactor: group import --- packages/tests/tests/waku.node.spec.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/tests/tests/waku.node.spec.ts b/packages/tests/tests/waku.node.spec.ts index ff5ba0479..ea3323167 100644 --- a/packages/tests/tests/waku.node.spec.ts +++ b/packages/tests/tests/waku.node.spec.ts @@ -16,8 +16,12 @@ import { createLightNode, createRelayNode } from "@waku/sdk"; import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes"; import { expect } from "chai"; -import { makeLogFileName, NOISE_KEY_1, NOISE_KEY_2 } from "../src/index.js"; -import { NimGoNode } from "../src/node/node.js"; +import { + makeLogFileName, + NimGoNode, + NOISE_KEY_1, + NOISE_KEY_2 +} from "../src/index.js"; const TestContentTopic = "/test/1/waku/utf8"; From 4a9360d4e3b8cbf580e5fb7199f7e6c001fa1ce1 Mon Sep 17 00:00:00 2001 From: "fryorcraken.eth" Date: Wed, 20 Sep 2023 15:12:32 +1000 Subject: [PATCH 2/4] test: fail on unhandled rejections and uncaught exceptions --- packages/tests/tests/waku.node.spec.ts | 41 +++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/packages/tests/tests/waku.node.spec.ts b/packages/tests/tests/waku.node.spec.ts index ea3323167..72bb3d888 100644 --- a/packages/tests/tests/waku.node.spec.ts +++ b/packages/tests/tests/waku.node.spec.ts @@ -12,7 +12,11 @@ import { createEncoder, generateSymmetricKey } from "@waku/message-encryption/symmetric"; -import { createLightNode, createRelayNode } from "@waku/sdk"; +import { + createLightNode, + createEncoder as createPlainEncoder, + createRelayNode +} from "@waku/sdk"; import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes"; import { expect } from "chai"; @@ -25,6 +29,8 @@ import { const TestContentTopic = "/test/1/waku/utf8"; +const TestEncoder = createPlainEncoder({ contentTopic: TestContentTopic }); + describe("Waku Dial [node only]", function () { describe("Interop: NimGoNode", function () { let waku: Waku; @@ -60,6 +66,39 @@ describe("Waku Dial [node only]", function () { const nimPeerId = await nwaku.getPeerId(); expect(await waku.libp2p.peerStore.has(nimPeerId)).to.be.true; }); + + it("Does not throw an exception when node disconnects", async function () { + this.timeout(20_000); + + process.on("unhandledRejection", (e) => + expect.fail("unhandledRejection", e) + ); + process.on("uncaughtException", (e) => + expect.fail("uncaughtException", e) + ); + + nwaku = new NimGoNode(makeLogFileName(this)); + await nwaku.start({ + filter: true, + store: true, + lightpush: true + }); + const multiAddrWithId = await nwaku.getMultiaddrWithId(); + + waku = await createLightNode({ + staticNoiseKey: NOISE_KEY_1 + }); + await waku.start(); + await waku.dial(multiAddrWithId); + await nwaku.stop(); + try { + await waku.lightPush?.send(TestEncoder, { + payload: utf8ToBytes("hello world") + }); + } catch (e) { + // We are not checking this exception + } + }); }); describe("Bootstrap", function () { From fb37c89e40a9d7c98bef17a085876478486fca8b Mon Sep 17 00:00:00 2001 From: "fryorcraken.eth" Date: Wed, 20 Sep 2023 15:33:13 +1000 Subject: [PATCH 3/4] fix: catch top level exception when preemptively creating streams --- packages/core/src/lib/stream_manager.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/core/src/lib/stream_manager.ts b/packages/core/src/lib/stream_manager.ts index 54d1956a2..fa1c78238 100644 --- a/packages/core/src/lib/stream_manager.ts +++ b/packages/core/src/lib/stream_manager.ts @@ -6,8 +6,8 @@ import { selectConnection } from "@waku/utils/libp2p"; import debug from "debug"; export class StreamManager { - private streamPool: Map>; - private log: debug.Debugger; + private streamPool: Map>; + private readonly log: debug.Debugger; constructor( public multicodec: string, @@ -38,7 +38,7 @@ export class StreamManager { const stream = await streamPromise; - if (stream.status === "closed") { + if (!stream || stream.status === "closed") { return this.newStream(peer); // fallback by creating a new stream on the spot } @@ -55,7 +55,10 @@ export class StreamManager { } private prepareNewStream(peer: Peer): void { - const streamPromise = this.newStream(peer); + const streamPromise = this.newStream(peer).catch(() => { + // No error thrown as this call is not triggered by the user + this.log(`Failed to prepare a new stream for ${peer.id.toString()}`); + }); this.streamPool.set(peer.id.toString(), streamPromise); } From 8cbd4c1d00e708b2b059f310eef2ca21d57119e8 Mon Sep 17 00:00:00 2001 From: "fryorcraken.eth" Date: Thu, 21 Sep 2023 11:54:17 +1000 Subject: [PATCH 4/4] test: light push should not throw an exception --- packages/tests/tests/waku.node.spec.ts | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/packages/tests/tests/waku.node.spec.ts b/packages/tests/tests/waku.node.spec.ts index 72bb3d888..608b432bc 100644 --- a/packages/tests/tests/waku.node.spec.ts +++ b/packages/tests/tests/waku.node.spec.ts @@ -91,13 +91,9 @@ describe("Waku Dial [node only]", function () { await waku.start(); await waku.dial(multiAddrWithId); await nwaku.stop(); - try { - await waku.lightPush?.send(TestEncoder, { - payload: utf8ToBytes("hello world") - }); - } catch (e) { - // We are not checking this exception - } + await waku.lightPush?.send(TestEncoder, { + payload: utf8ToBytes("hello world") + }); }); });