refactor store tests

This commit is contained in:
fbarbu15 2023-09-29 19:10:03 +03:00
parent f307e9b6c6
commit c83b976621
No known key found for this signature in database
GPG Key ID: D75221C8DEA22501
3 changed files with 62 additions and 227 deletions

View File

@ -1,15 +1,14 @@
import { createDecoder, waitForRemotePeer } from "@waku/core"; import { createDecoder } from "@waku/core";
import type { IMessage, LightNode } from "@waku/interfaces"; import type { LightNode } from "@waku/interfaces";
import { Protocols } from "@waku/interfaces";
import { createLightNode } from "@waku/sdk";
import { expect } from "chai"; import { expect } from "chai";
import { makeLogFileName, NimGoNode, tearDownNodes } from "../../src/index.js";
import { import {
makeLogFileName, processMessages,
NimGoNode, sendMessages,
NOISE_KEY_1, startAndConnectLightNode
tearDownNodes } from "./utils.js";
} from "../../src/index.js";
const customPubSubTopic = "/waku/2/custom-dapp/proto"; const customPubSubTopic = "/waku/2/custom-dapp/proto";
const TestContentTopic = "/test/1/waku-store/utf8"; const TestContentTopic = "/test/1/waku-store/utf8";
@ -17,8 +16,10 @@ const CustomPubSubTestDecoder = createDecoder(
TestContentTopic, TestContentTopic,
customPubSubTopic customPubSubTopic
); );
const totalMsgs = 20;
describe("Waku Store, custom pubsub topic", () => { describe("Waku Store, custom pubsub topic", function () {
this.timeout(15000);
let waku: LightNode; let waku: LightNode;
let nwaku: NimGoNode; let nwaku: NimGoNode;
@ -27,9 +28,10 @@ describe("Waku Store, custom pubsub topic", () => {
nwaku = new NimGoNode(makeLogFileName(this)); nwaku = new NimGoNode(makeLogFileName(this));
await nwaku.startWithRetries({ await nwaku.startWithRetries({
store: true, store: true,
topic: customPubSubTopic, relay: true,
relay: true topic: customPubSubTopic
}); });
await nwaku.ensureSubscriptions([customPubSubTopic]);
}); });
afterEach(async function () { afterEach(async function () {
@ -38,45 +40,13 @@ describe("Waku Store, custom pubsub topic", () => {
}); });
it("Generator, custom pubsub topic", async function () { it("Generator, custom pubsub topic", async function () {
this.timeout(15000); await sendMessages(nwaku, totalMsgs, TestContentTopic, customPubSubTopic);
waku = await startAndConnectLightNode(nwaku, [customPubSubTopic]);
const totalMsgs = 20; const messages = await processMessages(
for (let i = 0; i < totalMsgs; i++) { waku,
expect( [CustomPubSubTestDecoder],
await nwaku.sendMessage(
NimGoNode.toMessageRpcQuery({
payload: new Uint8Array([i]),
contentTopic: TestContentTopic
}),
customPubSubTopic 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<void>[] = [];
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);
expect(messages?.length).eq(totalMsgs); expect(messages?.length).eq(totalMsgs);
const result = messages?.findIndex((msg) => { const result = messages?.findIndex((msg) => {

View File

@ -20,35 +20,27 @@ import {
createEncoder as createSymEncoder, createEncoder as createSymEncoder,
generateSymmetricKey generateSymmetricKey
} from "@waku/message-encryption/symmetric"; } from "@waku/message-encryption/symmetric";
import { createLightNode } from "@waku/sdk";
import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes"; import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes";
import { expect } from "chai"; import { expect } from "chai";
import debug from "debug";
import { makeLogFileName, NimGoNode, tearDownNodes } from "../../src/index.js";
import {
delay,
makeLogFileName,
NimGoNode,
NOISE_KEY_1,
NOISE_KEY_2,
tearDownNodes
} from "../../src/index.js";
import { import {
log,
processMessages, processMessages,
sendMessages, sendMessages,
startAndConnectLightNode startAndConnectLightNode
} from "./utils.js"; } from "./utils.js";
const log = debug("waku:test:store");
const TestContentTopic = "/test/1/waku-store/utf8"; const TestContentTopic = "/test/1/waku-store/utf8";
const TestEncoder = createEncoder({ contentTopic: TestContentTopic }); const TestEncoder = createEncoder({ contentTopic: TestContentTopic });
const TestDecoder = createDecoder(TestContentTopic); const TestDecoder = createDecoder(TestContentTopic);
const totalMsgs = 20;
describe.only("Waku Store", function () { describe("Waku Store", function () {
this.timeout(15000); this.timeout(15000);
let waku: LightNode; let waku: LightNode;
let waku2: LightNode;
let nwaku: NimGoNode; let nwaku: NimGoNode;
beforeEach(async function () { beforeEach(async function () {
@ -59,12 +51,10 @@ describe.only("Waku Store", function () {
afterEach(async function () { afterEach(async function () {
this.timeout(15000); this.timeout(15000);
await tearDownNodes([nwaku], [waku]); await tearDownNodes([nwaku], [waku, waku2]);
}); });
it("Generator", async function () { it("Generator", async function () {
const totalMsgs = 20;
await sendMessages(nwaku, totalMsgs, TestContentTopic, DefaultPubSubTopic); await sendMessages(nwaku, totalMsgs, TestContentTopic, DefaultPubSubTopic);
waku = await startAndConnectLightNode(nwaku); waku = await startAndConnectLightNode(nwaku);
const messages = await processMessages( const messages = await processMessages(
@ -80,7 +70,7 @@ describe.only("Waku Store", function () {
expect(result).to.not.eq(-1); 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); waku = await startAndConnectLightNode(nwaku);
const messages = await processMessages( const messages = await processMessages(
waku, waku,
@ -92,26 +82,8 @@ describe.only("Waku Store", function () {
}); });
it("Passing a cursor", async function () { it("Passing a cursor", async function () {
this.timeout(4_000); await sendMessages(nwaku, totalMsgs, TestContentTopic, DefaultPubSubTopic);
const totalMsgs = 20; waku = await startAndConnectLightNode(nwaku);
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]);
const query = waku.store.queryGenerator([TestDecoder]); const query = waku.store.queryGenerator([TestDecoder]);
@ -148,27 +120,8 @@ describe.only("Waku Store", function () {
}); });
it("Callback on promise", async function () { it("Callback on promise", async function () {
this.timeout(15_000); await sendMessages(nwaku, totalMsgs, TestContentTopic, DefaultPubSubTopic);
waku = await startAndConnectLightNode(nwaku);
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]);
const messages: IMessage[] = []; const messages: IMessage[] = [];
await waku.store.queryWithPromiseCallback( await waku.store.queryWithPromiseCallback(
@ -189,27 +142,8 @@ describe.only("Waku Store", function () {
}); });
it("Callback on promise, aborts when callback returns true", async function () { it("Callback on promise, aborts when callback returns true", async function () {
this.timeout(15_000); await sendMessages(nwaku, totalMsgs, TestContentTopic, DefaultPubSubTopic);
waku = await startAndConnectLightNode(nwaku);
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]);
const desiredMsgs = 14; const desiredMsgs = 14;
const messages: IMessage[] = []; const messages: IMessage[] = [];
@ -229,27 +163,8 @@ describe.only("Waku Store", function () {
}); });
it("Ordered Callback - Forward", async function () { it("Ordered Callback - Forward", async function () {
this.timeout(15_000); await sendMessages(nwaku, totalMsgs, TestContentTopic, DefaultPubSubTopic);
waku = await startAndConnectLightNode(nwaku);
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]);
const messages: IMessage[] = []; const messages: IMessage[] = [];
await waku.store.queryWithOrderedCallback( await waku.store.queryWithOrderedCallback(
@ -268,27 +183,8 @@ describe.only("Waku Store", function () {
}); });
it("Ordered Callback - Backward", async function () { it("Ordered Callback - Backward", async function () {
this.timeout(15_000); await sendMessages(nwaku, totalMsgs, TestContentTopic, DefaultPubSubTopic);
waku = await startAndConnectLightNode(nwaku);
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]);
let messages: IMessage[] = []; let messages: IMessage[] = [];
await waku.store.queryWithOrderedCallback( await waku.store.queryWithOrderedCallback(
@ -309,8 +205,6 @@ describe.only("Waku Store", function () {
}); });
it("Generator, with asymmetric & symmetric encrypted messages", async 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 asymText = "This message is encrypted for me using asymmetric";
const asymTopic = "/test/1/asymmetric/proto"; const asymTopic = "/test/1/asymmetric/proto";
const symText = const symText =
@ -357,33 +251,25 @@ describe.only("Waku Store", function () {
const eciesDecoder = createEciesDecoder(asymTopic, privateKey); const eciesDecoder = createEciesDecoder(asymTopic, privateKey);
const symDecoder = createSymDecoder(symTopic, symKey); const symDecoder = createSymDecoder(symTopic, symKey);
const [waku1, waku2, nimWakuMultiaddr] = await Promise.all([ waku = await startAndConnectLightNode(nwaku);
createLightNode({ waku2 = await startAndConnectLightNode(nwaku);
staticNoiseKey: NOISE_KEY_1 const nimWakuMultiaddr = await nwaku.getMultiaddrWithId();
}).then((waku) => waku.start().then(() => waku)),
createLightNode({
staticNoiseKey: NOISE_KEY_2
}).then((waku) => waku.start().then(() => waku)),
nwaku.getMultiaddrWithId()
]);
log("Waku nodes created");
await Promise.all([ await Promise.all([
waku1.dial(nimWakuMultiaddr), waku.dial(nimWakuMultiaddr),
waku2.dial(nimWakuMultiaddr) waku2.dial(nimWakuMultiaddr)
]); ]);
log("Waku nodes connected to nwaku"); log("Waku nodes connected to nwaku");
await waitForRemotePeer(waku1, [Protocols.LightPush]); await waitForRemotePeer(waku, [Protocols.LightPush]);
log("Sending messages using light push"); log("Sending messages using light push");
await Promise.all([ await Promise.all([
waku1.lightPush.send(eciesEncoder, asymMsg), waku.lightPush.send(eciesEncoder, asymMsg),
waku1.lightPush.send(symEncoder, symMsg), waku.lightPush.send(symEncoder, symMsg),
waku1.lightPush.send(otherEncoder, otherMsg), waku.lightPush.send(otherEncoder, otherMsg),
waku1.lightPush.send(TestEncoder, clearMsg) waku.lightPush.send(TestEncoder, clearMsg)
]); ]);
await waitForRemotePeer(waku2, [Protocols.Store]); 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[1].payload!)).to.eq(symText);
expect(bytesToUtf8(messages[2].payload!)).to.eq(clearText); expect(bytesToUtf8(messages[2].payload!)).to.eq(clearText);
expect(messages?.length).eq(3); 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 () { it("Ordered callback, using start and end time", async function () {
this.timeout(20000);
const now = new Date(); const now = new Date();
const startTime = new Date(); const startTime = new Date();
@ -436,6 +317,9 @@ describe.only("Waku Store", function () {
// Set end time 1 second in the past // Set end time 1 second in the past
endTime.setTime(now.getTime() - 1000); endTime.setTime(now.getTime() - 1000);
await sendMessages(nwaku, 2, TestContentTopic, DefaultPubSubTopic);
waku = await startAndConnectLightNode(nwaku);
for (let i = 0; i < 2; i++) { for (let i = 0; i < 2; i++) {
expect( expect(
await nwaku.sendMessage( await nwaku.sendMessage(
@ -448,12 +332,7 @@ describe.only("Waku Store", function () {
).to.be.true; ).to.be.true;
} }
waku = await createLightNode({ waku = await startAndConnectLightNode(nwaku);
staticNoiseKey: NOISE_KEY_1
});
await waku.start();
await waku.dial(await nwaku.getMultiaddrWithId());
await waitForRemotePeer(waku, [Protocols.Store]);
const firstMessages: IMessage[] = []; const firstMessages: IMessage[] = [];
await waku.store.queryWithOrderedCallback( await waku.store.queryWithOrderedCallback(
@ -490,28 +369,8 @@ describe.only("Waku Store", function () {
}); });
it("Ordered callback, aborts when callback returns true", async function () { it("Ordered callback, aborts when callback returns true", async function () {
this.timeout(15_000); await sendMessages(nwaku, totalMsgs, TestContentTopic, DefaultPubSubTopic);
waku = await startAndConnectLightNode(nwaku);
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]);
const desiredMsgs = 14; const desiredMsgs = 14;
const messages: IMessage[] = []; const messages: IMessage[] = [];

View File

@ -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 { IMessage, LightNode, Protocols } from "@waku/interfaces";
import { createLightNode } from "@waku/sdk"; import { createLightNode } from "@waku/sdk";
import { expect } from "chai"; import { expect } from "chai";
import debug from "debug";
import { delay, NimGoNode, NOISE_KEY_1 } from "../../src"; import { delay, NimGoNode, NOISE_KEY_1 } from "../../src";
export const log = debug("waku:test:store");
export async function sendMessages( export async function sendMessages(
instance: NimGoNode, instance: NimGoNode,
numMessages: number, numMessages: number,
@ -48,13 +51,16 @@ export async function processMessages(
} }
export async function startAndConnectLightNode( export async function startAndConnectLightNode(
instance: NimGoNode instance: NimGoNode,
pubSubTopics: string[] = [DefaultPubSubTopic]
): Promise<LightNode> { ): Promise<LightNode> {
const waku = await createLightNode({ const waku = await createLightNode({
pubSubTopics: pubSubTopics,
staticNoiseKey: NOISE_KEY_1 staticNoiseKey: NOISE_KEY_1
}); });
await waku.start(); await waku.start();
await waku.dial(await instance.getMultiaddrWithId()); await waku.dial(await instance.getMultiaddrWithId());
await waitForRemotePeer(waku, [Protocols.Store]); await waitForRemotePeer(waku, [Protocols.Store]);
log("Waku node created");
return waku; return waku;
} }