From c83b976621c3d549aad1f8d960978b892592cd4b Mon Sep 17 00:00:00 2001 From: fbarbu15 Date: Fri, 29 Sep 2023 19:10:03 +0300 Subject: [PATCH] refactor store tests --- .../tests/tests/store/multiple_pubsub.spec.ts | 72 ++---- packages/tests/tests/store/store.node.spec.ts | 207 +++--------------- packages/tests/tests/store/utils.ts | 10 +- 3 files changed, 62 insertions(+), 227 deletions(-) diff --git a/packages/tests/tests/store/multiple_pubsub.spec.ts b/packages/tests/tests/store/multiple_pubsub.spec.ts index 05367aa46a..34e4dd5618 100644 --- a/packages/tests/tests/store/multiple_pubsub.spec.ts +++ b/packages/tests/tests/store/multiple_pubsub.spec.ts @@ -1,15 +1,14 @@ -import { createDecoder, waitForRemotePeer } from "@waku/core"; -import type { IMessage, LightNode } from "@waku/interfaces"; -import { Protocols } from "@waku/interfaces"; -import { createLightNode } from "@waku/sdk"; +import { createDecoder } from "@waku/core"; +import type { LightNode } from "@waku/interfaces"; import { expect } from "chai"; +import { makeLogFileName, NimGoNode, tearDownNodes } from "../../src/index.js"; + import { - makeLogFileName, - NimGoNode, - NOISE_KEY_1, - tearDownNodes -} from "../../src/index.js"; + processMessages, + sendMessages, + startAndConnectLightNode +} from "./utils.js"; const customPubSubTopic = "/waku/2/custom-dapp/proto"; const TestContentTopic = "/test/1/waku-store/utf8"; @@ -17,8 +16,10 @@ const CustomPubSubTestDecoder = createDecoder( TestContentTopic, customPubSubTopic ); +const totalMsgs = 20; -describe("Waku Store, custom pubsub topic", () => { +describe("Waku Store, custom pubsub topic", function () { + this.timeout(15000); let waku: LightNode; let nwaku: NimGoNode; @@ -27,9 +28,10 @@ describe("Waku Store, custom pubsub topic", () => { nwaku = new NimGoNode(makeLogFileName(this)); await nwaku.startWithRetries({ store: true, - topic: customPubSubTopic, - relay: true + relay: true, + topic: customPubSubTopic }); + await nwaku.ensureSubscriptions([customPubSubTopic]); }); afterEach(async function () { @@ -38,45 +40,13 @@ describe("Waku Store, custom pubsub topic", () => { }); it("Generator, custom pubsub topic", async function () { - this.timeout(15000); - - const totalMsgs = 20; - for (let i = 0; i < totalMsgs; i++) { - expect( - await nwaku.sendMessage( - NimGoNode.toMessageRpcQuery({ - payload: new Uint8Array([i]), - contentTopic: TestContentTopic - }), - customPubSubTopic - ) - ).to.be.true; - } - - waku = await createLightNode({ - staticNoiseKey: NOISE_KEY_1, - pubSubTopics: [customPubSubTopic] - }); - await waku.start(); - await waku.dial(await nwaku.getMultiaddrWithId()); - await waitForRemotePeer(waku, [Protocols.Store]); - - const messages: IMessage[] = []; - let promises: Promise[] = []; - for await (const msgPromises of waku.store.queryGenerator([ - CustomPubSubTestDecoder - ])) { - const _promises = msgPromises.map(async (promise) => { - const msg = await promise; - if (msg) { - messages.push(msg); - expect(msg.pubSubTopic).to.eq(customPubSubTopic); - } - }); - - promises = promises.concat(_promises); - } - await Promise.all(promises); + await sendMessages(nwaku, totalMsgs, TestContentTopic, customPubSubTopic); + waku = await startAndConnectLightNode(nwaku, [customPubSubTopic]); + const messages = await processMessages( + waku, + [CustomPubSubTestDecoder], + customPubSubTopic + ); expect(messages?.length).eq(totalMsgs); const result = messages?.findIndex((msg) => { diff --git a/packages/tests/tests/store/store.node.spec.ts b/packages/tests/tests/store/store.node.spec.ts index 4efd1389b9..26616b8b32 100644 --- a/packages/tests/tests/store/store.node.spec.ts +++ b/packages/tests/tests/store/store.node.spec.ts @@ -20,35 +20,27 @@ import { createEncoder as createSymEncoder, generateSymmetricKey } from "@waku/message-encryption/symmetric"; -import { createLightNode } from "@waku/sdk"; import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes"; import { expect } from "chai"; -import debug from "debug"; - -import { - delay, - makeLogFileName, - NimGoNode, - NOISE_KEY_1, - NOISE_KEY_2, - tearDownNodes -} from "../../src/index.js"; + +import { makeLogFileName, NimGoNode, tearDownNodes } from "../../src/index.js"; import { + log, processMessages, sendMessages, startAndConnectLightNode } from "./utils.js"; -const log = debug("waku:test:store"); - const TestContentTopic = "/test/1/waku-store/utf8"; const TestEncoder = createEncoder({ contentTopic: TestContentTopic }); const TestDecoder = createDecoder(TestContentTopic); +const totalMsgs = 20; -describe.only("Waku Store", function () { +describe("Waku Store", function () { this.timeout(15000); let waku: LightNode; + let waku2: LightNode; let nwaku: NimGoNode; beforeEach(async function () { @@ -59,12 +51,10 @@ describe.only("Waku Store", function () { afterEach(async function () { this.timeout(15000); - await tearDownNodes([nwaku], [waku]); + await tearDownNodes([nwaku], [waku, waku2]); }); it("Generator", async function () { - const totalMsgs = 20; - await sendMessages(nwaku, totalMsgs, TestContentTopic, DefaultPubSubTopic); waku = await startAndConnectLightNode(nwaku); const messages = await processMessages( @@ -80,7 +70,7 @@ describe.only("Waku Store", function () { expect(result).to.not.eq(-1); }); - it.only("Generator, no message returned", async function () { + it("Generator, no message returned", async function () { waku = await startAndConnectLightNode(nwaku); const messages = await processMessages( waku, @@ -92,26 +82,8 @@ describe.only("Waku Store", function () { }); it("Passing a cursor", async function () { - this.timeout(4_000); - const totalMsgs = 20; - - for (let i = 0; i < totalMsgs; i++) { - expect( - await nwaku.sendMessage( - NimGoNode.toMessageRpcQuery({ - payload: utf8ToBytes(`Message ${i}`), - contentTopic: TestContentTopic - }) - ) - ).to.be.true; - } - - waku = await createLightNode({ - staticNoiseKey: NOISE_KEY_1 - }); - await waku.start(); - await waku.dial(await nwaku.getMultiaddrWithId()); - await waitForRemotePeer(waku, [Protocols.Store]); + await sendMessages(nwaku, totalMsgs, TestContentTopic, DefaultPubSubTopic); + waku = await startAndConnectLightNode(nwaku); const query = waku.store.queryGenerator([TestDecoder]); @@ -148,27 +120,8 @@ describe.only("Waku Store", function () { }); it("Callback on promise", async function () { - this.timeout(15_000); - - const totalMsgs = 15; - - for (let i = 0; i < totalMsgs; i++) { - expect( - await nwaku.sendMessage( - NimGoNode.toMessageRpcQuery({ - payload: new Uint8Array([i]), - contentTopic: TestContentTopic - }) - ) - ).to.be.true; - } - - waku = await createLightNode({ - staticNoiseKey: NOISE_KEY_1 - }); - await waku.start(); - await waku.dial(await nwaku.getMultiaddrWithId()); - await waitForRemotePeer(waku, [Protocols.Store]); + await sendMessages(nwaku, totalMsgs, TestContentTopic, DefaultPubSubTopic); + waku = await startAndConnectLightNode(nwaku); const messages: IMessage[] = []; await waku.store.queryWithPromiseCallback( @@ -189,27 +142,8 @@ describe.only("Waku Store", function () { }); it("Callback on promise, aborts when callback returns true", async function () { - this.timeout(15_000); - - const totalMsgs = 20; - - for (let i = 0; i < totalMsgs; i++) { - expect( - await nwaku.sendMessage( - NimGoNode.toMessageRpcQuery({ - payload: new Uint8Array([i]), - contentTopic: TestContentTopic - }) - ) - ).to.be.true; - } - - waku = await createLightNode({ - staticNoiseKey: NOISE_KEY_1 - }); - await waku.start(); - await waku.dial(await nwaku.getMultiaddrWithId()); - await waitForRemotePeer(waku, [Protocols.Store]); + await sendMessages(nwaku, totalMsgs, TestContentTopic, DefaultPubSubTopic); + waku = await startAndConnectLightNode(nwaku); const desiredMsgs = 14; const messages: IMessage[] = []; @@ -229,27 +163,8 @@ describe.only("Waku Store", function () { }); it("Ordered Callback - Forward", async function () { - this.timeout(15_000); - - const totalMsgs = 18; - for (let i = 0; i < totalMsgs; i++) { - expect( - await nwaku.sendMessage( - NimGoNode.toMessageRpcQuery({ - payload: new Uint8Array([i]), - contentTopic: TestContentTopic - }) - ) - ).to.be.true; - await delay(1); // to ensure each timestamp is unique. - } - - waku = await createLightNode({ - staticNoiseKey: NOISE_KEY_1 - }); - await waku.start(); - await waku.dial(await nwaku.getMultiaddrWithId()); - await waitForRemotePeer(waku, [Protocols.Store]); + await sendMessages(nwaku, totalMsgs, TestContentTopic, DefaultPubSubTopic); + waku = await startAndConnectLightNode(nwaku); const messages: IMessage[] = []; await waku.store.queryWithOrderedCallback( @@ -268,27 +183,8 @@ describe.only("Waku Store", function () { }); it("Ordered Callback - Backward", async function () { - this.timeout(15_000); - - const totalMsgs = 18; - for (let i = 0; i < totalMsgs; i++) { - expect( - await nwaku.sendMessage( - NimGoNode.toMessageRpcQuery({ - payload: new Uint8Array([i]), - contentTopic: TestContentTopic - }) - ) - ).to.be.true; - await delay(1); // to ensure each timestamp is unique. - } - - waku = await createLightNode({ - staticNoiseKey: NOISE_KEY_1 - }); - await waku.start(); - await waku.dial(await nwaku.getMultiaddrWithId()); - await waitForRemotePeer(waku, [Protocols.Store]); + await sendMessages(nwaku, totalMsgs, TestContentTopic, DefaultPubSubTopic); + waku = await startAndConnectLightNode(nwaku); let messages: IMessage[] = []; await waku.store.queryWithOrderedCallback( @@ -309,8 +205,6 @@ describe.only("Waku Store", function () { }); it("Generator, with asymmetric & symmetric encrypted messages", async function () { - this.timeout(15_000); - const asymText = "This message is encrypted for me using asymmetric"; const asymTopic = "/test/1/asymmetric/proto"; const symText = @@ -357,33 +251,25 @@ describe.only("Waku Store", function () { const eciesDecoder = createEciesDecoder(asymTopic, privateKey); const symDecoder = createSymDecoder(symTopic, symKey); - const [waku1, waku2, nimWakuMultiaddr] = await Promise.all([ - createLightNode({ - staticNoiseKey: NOISE_KEY_1 - }).then((waku) => waku.start().then(() => waku)), - createLightNode({ - staticNoiseKey: NOISE_KEY_2 - }).then((waku) => waku.start().then(() => waku)), - nwaku.getMultiaddrWithId() - ]); - - log("Waku nodes created"); + waku = await startAndConnectLightNode(nwaku); + waku2 = await startAndConnectLightNode(nwaku); + const nimWakuMultiaddr = await nwaku.getMultiaddrWithId(); await Promise.all([ - waku1.dial(nimWakuMultiaddr), + waku.dial(nimWakuMultiaddr), waku2.dial(nimWakuMultiaddr) ]); log("Waku nodes connected to nwaku"); - await waitForRemotePeer(waku1, [Protocols.LightPush]); + await waitForRemotePeer(waku, [Protocols.LightPush]); log("Sending messages using light push"); await Promise.all([ - waku1.lightPush.send(eciesEncoder, asymMsg), - waku1.lightPush.send(symEncoder, symMsg), - waku1.lightPush.send(otherEncoder, otherMsg), - waku1.lightPush.send(TestEncoder, clearMsg) + waku.lightPush.send(eciesEncoder, asymMsg), + waku.lightPush.send(symEncoder, symMsg), + waku.lightPush.send(otherEncoder, otherMsg), + waku.lightPush.send(TestEncoder, clearMsg) ]); await waitForRemotePeer(waku2, [Protocols.Store]); @@ -409,14 +295,9 @@ describe.only("Waku Store", function () { expect(bytesToUtf8(messages[1].payload!)).to.eq(symText); expect(bytesToUtf8(messages[2].payload!)).to.eq(clearText); expect(messages?.length).eq(3); - - !!waku1 && waku1.stop().catch((e) => console.log("Waku failed to stop", e)); - !!waku2 && waku2.stop().catch((e) => console.log("Waku failed to stop", e)); }); it("Ordered callback, using start and end time", async function () { - this.timeout(20000); - const now = new Date(); const startTime = new Date(); @@ -436,6 +317,9 @@ describe.only("Waku Store", function () { // Set end time 1 second in the past endTime.setTime(now.getTime() - 1000); + await sendMessages(nwaku, 2, TestContentTopic, DefaultPubSubTopic); + waku = await startAndConnectLightNode(nwaku); + for (let i = 0; i < 2; i++) { expect( await nwaku.sendMessage( @@ -448,12 +332,7 @@ describe.only("Waku Store", function () { ).to.be.true; } - waku = await createLightNode({ - staticNoiseKey: NOISE_KEY_1 - }); - await waku.start(); - await waku.dial(await nwaku.getMultiaddrWithId()); - await waitForRemotePeer(waku, [Protocols.Store]); + waku = await startAndConnectLightNode(nwaku); const firstMessages: IMessage[] = []; await waku.store.queryWithOrderedCallback( @@ -490,28 +369,8 @@ describe.only("Waku Store", function () { }); it("Ordered callback, aborts when callback returns true", async function () { - this.timeout(15_000); - - const totalMsgs = 20; - - for (let i = 0; i < totalMsgs; i++) { - expect( - await nwaku.sendMessage( - NimGoNode.toMessageRpcQuery({ - payload: new Uint8Array([i]), - contentTopic: TestContentTopic - }) - ) - ).to.be.true; - await delay(1); // to ensure each timestamp is unique. - } - - waku = await createLightNode({ - staticNoiseKey: NOISE_KEY_1 - }); - await waku.start(); - await waku.dial(await nwaku.getMultiaddrWithId()); - await waitForRemotePeer(waku, [Protocols.Store]); + await sendMessages(nwaku, totalMsgs, TestContentTopic, DefaultPubSubTopic); + waku = await startAndConnectLightNode(nwaku); const desiredMsgs = 14; const messages: IMessage[] = []; diff --git a/packages/tests/tests/store/utils.ts b/packages/tests/tests/store/utils.ts index 455d5a6821..f875906da3 100644 --- a/packages/tests/tests/store/utils.ts +++ b/packages/tests/tests/store/utils.ts @@ -1,10 +1,13 @@ -import { Decoder, waitForRemotePeer } from "@waku/core"; +import { Decoder, DefaultPubSubTopic, waitForRemotePeer } from "@waku/core"; import { IMessage, LightNode, Protocols } from "@waku/interfaces"; import { createLightNode } from "@waku/sdk"; import { expect } from "chai"; +import debug from "debug"; import { delay, NimGoNode, NOISE_KEY_1 } from "../../src"; +export const log = debug("waku:test:store"); + export async function sendMessages( instance: NimGoNode, numMessages: number, @@ -48,13 +51,16 @@ export async function processMessages( } export async function startAndConnectLightNode( - instance: NimGoNode + instance: NimGoNode, + pubSubTopics: string[] = [DefaultPubSubTopic] ): Promise { const waku = await createLightNode({ + pubSubTopics: pubSubTopics, staticNoiseKey: NOISE_KEY_1 }); await waku.start(); await waku.dial(await instance.getMultiaddrWithId()); await waitForRemotePeer(waku, [Protocols.Store]); + log("Waku node created"); return waku; }