Skip to content

Commit

Permalink
chore: new relay tests (#1649)
Browse files Browse the repository at this point in the history
* make relay folder

* make relay folder

* adjust message collector for relay

* small fix

* small fix

* small fix

* split tests more

* small fixes

* small fix

* new test

* fix pubsubtopic name

* new subscribe tests

* new subscribe tests

* new tests

* small fix after ci run

* small fix after ci run2

* fix skipped test

* added issue for skipped test
  • Loading branch information
fbarbu15 authored Oct 23, 2023
1 parent 80a33b9 commit 1ec0c20
Show file tree
Hide file tree
Showing 16 changed files with 1,222 additions and 829 deletions.
3 changes: 2 additions & 1 deletion packages/tests/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export const TEST_STRING = [
{ description: "JSON", value: '{"user":"admin","password":"123456"}' },
{ description: "shell command", value: "`rm -rf /`" },
{ description: "escaped characters", value: "\\n\\t\\0" },
{ description: "unicode special characters", value: "\u202Ereverse" }
{ description: "unicode special characters", value: "\u202Ereverse" },
{ description: "emoji", value: "🤫 🤥 😶 😶‍🌫️ 😐 😑 😬 🫨 🫠 🙄 😯 😦 😧 😮" }
];

export const TEST_TIMESTAMPS = [
Expand Down
1 change: 1 addition & 0 deletions packages/tests/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export * from "./log_file.js";
export * from "./node/node.js";
export * from "./teardown.js";
export * from "./message_collector.js";
export * from "./utils.js";
77 changes: 33 additions & 44 deletions packages/tests/src/message_collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class MessageCollector {
expectedVersion?: number;
expectedMeta?: Uint8Array;
expectedEphemeral?: boolean;
expectedTimestamp?: bigint;
expectedTimestamp?: bigint | number;
checkTimestamp?: boolean; // Used to determine if we need to check the timestamp
}
): void {
Expand Down Expand Up @@ -148,6 +148,38 @@ export class MessageCollector {
);
}

const shouldCheckTimestamp =
options.checkTimestamp !== undefined ? options.checkTimestamp : true;
if (shouldCheckTimestamp && message.timestamp) {
// In we send timestamp in the request we assert that it matches the timestamp in the response +- 1 sec
// We take the 1s deviation because there are some ms diffs in timestamps, probably because of conversions
let timestampAsNumber: number;

if (message.timestamp instanceof Date) {
timestampAsNumber = message.timestamp.getTime();
} else {
timestampAsNumber = Number(message.timestamp) / 1_000_000;
}

let lowerBound: number;
let upperBound: number;

// Define the bounds based on the expectedTimestamp
if (options.expectedTimestamp !== undefined) {
lowerBound = Number(options.expectedTimestamp) - 1000;
upperBound = Number(options.expectedTimestamp) + 1000;
} else {
upperBound = Date.now();
lowerBound = upperBound - 10000;
}

if (timestampAsNumber < lowerBound || timestampAsNumber > upperBound) {
throw new AssertionError(
`Message timestamp not within the expected range. Expected between: ${lowerBound} and ${upperBound}. Got: ${timestampAsNumber}`
);
}
}

if (this.isMessageRpcResponse(message)) {
// nwaku message specific assertions
const receivedMessageText = message.payload
Expand All @@ -158,37 +190,6 @@ export class MessageCollector {
options.expectedMessageText,
`Message text mismatch. Expected: ${options.expectedMessageText}. Got: ${receivedMessageText}`
);

if (message.timestamp) {
// In we send timestamp in the request we assert that it matches the timestamp in the response +- 1 sec
// We take the 1s deviation because there are some ms diffs in timestamps, probably because of conversions
if (options.expectedTimestamp !== undefined) {
const lowerBound =
BigInt(options.expectedTimestamp) - BigInt(1000000000);
const upperBound =
BigInt(options.expectedTimestamp) + BigInt(1000000000);

if (
message.timestamp < lowerBound ||
message.timestamp > upperBound
) {
throw new AssertionError(
`Message timestamp not within the expected range. Expected between: ${lowerBound} and ${upperBound}. Got: ${message.timestamp}`
);
}
}
// In we don't send timestamp in the request we assert that the timestamp in the response is between now and (now-10s)
else {
const now = BigInt(Date.now()) * BigInt(1_000_000);
const tenSecondsAgo = now - BigInt(10_000_000_000);

if (message.timestamp < tenSecondsAgo || message.timestamp > now) {
throw new AssertionError(
`Message timestamp not within the expected range. Expected between: ${tenSecondsAgo} and ${now}. Got: ${message.timestamp}`
);
}
}
}
} else {
// js-waku message specific assertions
expect(message.pubsubTopic).to.eq(
Expand All @@ -205,18 +206,6 @@ export class MessageCollector {
}. Got: ${bytesToUtf8(message.payload)}`
);

const shouldCheckTimestamp =
options.checkTimestamp !== undefined ? options.checkTimestamp : true;
if (shouldCheckTimestamp && message.timestamp) {
const now = Date.now();
const tenSecondsAgo = now - 10000;
expect(message.timestamp.getTime()).to.be.within(
tenSecondsAgo,
now,
`Message timestamp not within the expected range. Expected between: ${tenSecondsAgo} and ${now}. Got: ${message.timestamp.getTime()}`
);
}

expect([
options.expectedMeta,
undefined,
Expand Down
22 changes: 22 additions & 0 deletions packages/tests/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { createDecoder, createEncoder, Decoder, Encoder } from "@waku/core";

// Utility to generate test data for multiple topics tests.
export function generateTestData(topicCount: number): {
contentTopics: string[];
encoders: Encoder[];
decoders: Decoder[];
} {
const contentTopics = Array.from(
{ length: topicCount },
(_, i) => `/test/${i + 1}/waku-multi`
);
const encoders = contentTopics.map((topic) =>
createEncoder({ contentTopic: topic })
);
const decoders = contentTopics.map((topic) => createDecoder(topic));
return {
contentTopics,
encoders,
decoders
};
}
18 changes: 9 additions & 9 deletions packages/tests/tests/filter/push.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe("Waku Filter V2: FilterPush", function () {
});
});

it("Check received message with invalid timestamp is not received", async function () {
it("Check message with invalid timestamp is not received", async function () {
await subscription.subscribe([TestDecoder], messageCollector.callback);
await delay(400);

Expand All @@ -105,7 +105,7 @@ describe("Waku Filter V2: FilterPush", function () {
expect(await messageCollector.waitForMessages(1)).to.eq(false);
});

it("Check received message on other pubsub topic is not received", async function () {
it("Check message on other pubsub topic is not received", async function () {
await subscription.subscribe([TestDecoder], messageCollector.callback);
await delay(400);

Expand All @@ -121,7 +121,7 @@ describe("Waku Filter V2: FilterPush", function () {
expect(await messageCollector.waitForMessages(1)).to.eq(false);
});

it("Check received message with no pubsub topic is not received", async function () {
it("Check message with no pubsub topic is not received", async function () {
await subscription.subscribe([TestDecoder], messageCollector.callback);
await delay(400);

Expand All @@ -136,7 +136,7 @@ describe("Waku Filter V2: FilterPush", function () {
expect(await messageCollector.waitForMessages(1)).to.eq(false);
});

it("Check received message with no content topic is not received", async function () {
it("Check message with no content topic is not received", async function () {
await subscription.subscribe([TestDecoder], messageCollector.callback);
await delay(400);

Expand All @@ -151,7 +151,7 @@ describe("Waku Filter V2: FilterPush", function () {
expect(await messageCollector.waitForMessages(1)).to.eq(false);
});

it("Check received message with no payload is not received", async function () {
it("Check message with no payload is not received", async function () {
await subscription.subscribe([TestDecoder], messageCollector.callback);
await delay(400);

Expand All @@ -171,7 +171,7 @@ describe("Waku Filter V2: FilterPush", function () {
}
});

it("Check received message with non string payload is not received", async function () {
it("Check message with non string payload is not received", async function () {
await subscription.subscribe([TestDecoder], messageCollector.callback);
await delay(400);

Expand All @@ -187,7 +187,7 @@ describe("Waku Filter V2: FilterPush", function () {
expect(await messageCollector.waitForMessages(1)).to.eq(false);
});

it("Check received message with extra parameter is not received", async function () {
it("Check message with extra parameter is not received", async function () {
await subscription.subscribe([TestDecoder], messageCollector.callback);
await delay(400);

Expand Down Expand Up @@ -226,7 +226,7 @@ describe("Waku Filter V2: FilterPush", function () {
});

// Will be skipped until https://github.com/waku-org/js-waku/issues/1464 si done
it.skip("Check received message received after jswaku node is restarted", async function () {
it.skip("Check message received after jswaku node is restarted", async function () {
// Subscribe and send message
await subscription.subscribe([TestDecoder], messageCollector.callback);
await waku.lightPush.send(TestEncoder, { payload: utf8ToBytes("M1") });
Expand Down Expand Up @@ -259,7 +259,7 @@ describe("Waku Filter V2: FilterPush", function () {
});

// Will be skipped until https://github.com/waku-org/js-waku/issues/1464 si done
it.skip("Check received message received after nwaku node is restarted", async function () {
it.skip("Check message received after nwaku node is restarted", async function () {
await subscription.subscribe([TestDecoder], messageCollector.callback);
await waku.lightPush.send(TestEncoder, { payload: utf8ToBytes("M1") });
expect(await messageCollector.waitForMessages(1)).to.eq(true);
Expand Down
10 changes: 7 additions & 3 deletions packages/tests/tests/filter/subscribe.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { expect } from "chai";

import {
delay,
generateTestData,
makeLogFileName,
MessageCollector,
NimGoNode,
Expand All @@ -20,7 +21,6 @@ import {
} from "../../src/index.js";

import {
generateTestData,
messagePayload,
messageText,
runNodes,
Expand Down Expand Up @@ -295,7 +295,9 @@ describe("Waku Filter V2: Subscribe", function () {

// Check if all messages were received.
// Since there are overlapping topics, there should be 6 messages in total (2 from the first set + 4 from the second set).
expect(await messageCollector.waitForMessages(6)).to.eq(true);
expect(await messageCollector.waitForMessages(6, { exact: true })).to.eq(
true
);
});

it("Refresh subscription", async function () {
Expand All @@ -307,7 +309,9 @@ describe("Waku Filter V2: Subscribe", function () {
await waku.lightPush.send(TestEncoder, { payload: utf8ToBytes("M2") });

// Confirm both messages were received.
expect(await messageCollector.waitForMessages(2)).to.eq(true);
expect(await messageCollector.waitForMessages(2, { exact: true })).to.eq(
true
);
messageCollector.verifyReceivedMessage(0, {
expectedMessageText: "M1",
expectedContentTopic: TestContentTopic
Expand Down
8 changes: 6 additions & 2 deletions packages/tests/tests/filter/unsubscribe.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import type { IFilterSubscription, LightNode } from "@waku/interfaces";
import { utf8ToBytes } from "@waku/utils/bytes";
import { expect } from "chai";

import { MessageCollector, NimGoNode, tearDownNodes } from "../../src/index.js";

import {
generateTestData,
MessageCollector,
NimGoNode,
tearDownNodes
} from "../../src/index.js";

import {
messagePayload,
messageText,
runNodes,
Expand Down
29 changes: 1 addition & 28 deletions packages/tests/tests/filter/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
createDecoder,
createEncoder,
Decoder,
Encoder,
waitForRemotePeer
} from "@waku/core";
import { createDecoder, createEncoder, waitForRemotePeer } from "@waku/core";
import { IFilterSubscription, LightNode, Protocols } from "@waku/interfaces";
import { createLightNode } from "@waku/sdk";
import { Logger } from "@waku/utils";
Expand All @@ -21,27 +15,6 @@ export const TestDecoder = createDecoder(TestContentTopic);
export const messageText = "Filtering works!";
export const messagePayload = { payload: utf8ToBytes(messageText) };

// Utility to generate test data for multiple topics tests.
export function generateTestData(topicCount: number): {
contentTopics: string[];
encoders: Encoder[];
decoders: Decoder[];
} {
const contentTopics = Array.from(
{ length: topicCount },
(_, i) => `/test/${i + 1}/waku-multi`
);
const encoders = contentTopics.map((topic) =>
createEncoder({ contentTopic: topic })
);
const decoders = contentTopics.map((topic) => createDecoder(topic));
return {
contentTopics,
encoders,
decoders
};
}

// Utility to validate errors related to pings in the subscription.
export async function validatePingError(
subscription: IFilterSubscription
Expand Down
3 changes: 1 addition & 2 deletions packages/tests/tests/light-push/index.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ describe("Waku Light Push", function () {
Date.now() + 3600000
].forEach((testItem) => {
it(`Push message with custom timestamp: ${testItem}`, async function () {
const customTimeNanos = BigInt(testItem) * BigInt(1000000);
const pushResponse = await waku.lightPush.send(TestEncoder, {
payload: utf8ToBytes(messageText),
timestamp: new Date(testItem)
Expand All @@ -198,7 +197,7 @@ describe("Waku Light Push", function () {
expect(await messageCollector.waitForMessages(1)).to.eq(true);
messageCollector.verifyReceivedMessage(0, {
expectedMessageText: messageText,
expectedTimestamp: customTimeNanos,
expectedTimestamp: testItem,
expectedContentTopic: TestContentTopic
});
});
Expand Down
Loading

0 comments on commit 1ec0c20

Please sign in to comment.