Skip to content

Commit

Permalink
test: sharding tests refactor (#1883)
Browse files Browse the repository at this point in the history
* sharding tests refactor

* small fixes

* adjust clusterID based on version

* fix typo

* fix dispatchEvent test

* sharding unit tests

* port adjustment

* update unit tests

* fix 1902

* adjust content topic tests

* adjust metdata tests

* small adjustments

* fix

* update resolveAutoshardingCluster version

* skip autosharding tests for nwaku < 0.27.0

* skip autosharding tests for nwaku < 0.27.0
  • Loading branch information
fbarbu15 authored Mar 18, 2024
1 parent 40c5e0e commit f4c7c02
Show file tree
Hide file tree
Showing 20 changed files with 1,236 additions and 242 deletions.
2 changes: 1 addition & 1 deletion packages/tests/src/lib/service_node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ export class ServiceNode {
}

return this.restCall<boolean>(
`/relay/v1/auto/message`,
`/relay/v1/auto/messages`,
"POST",
message,
async (response) => response.status === 200
Expand Down
1 change: 1 addition & 0 deletions packages/tests/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface Args {
discv5Discovery?: boolean;
storeMessageDbUrl?: string;
pubsubTopic?: Array<string>;
contentTopic?: Array<string>;
websocketSupport?: boolean;
tcpPort?: number;
restPort?: number;
Expand Down
18 changes: 0 additions & 18 deletions packages/tests/src/utils/generate_test_data.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { createDecoder, createEncoder, Decoder, Encoder } from "@waku/core";

import { DOCKER_IMAGE_NAME } from "../lib/service_node";

// Utility to generate test data for multiple topics tests.
export function generateTestData(topicCount: number): {
contentTopics: string[];
Expand All @@ -22,19 +20,3 @@ export function generateTestData(topicCount: number): {
decoders
};
}

// Utility to add test conditions based on nwaku/go-waku versions
export function isNwakuAtLeast(requiredVersion: string): boolean {
const versionRegex = /(?:v)?(\d+\.\d+(?:\.\d+)?)/;
const match = DOCKER_IMAGE_NAME.match(versionRegex);

if (match) {
const version = match[0].substring(1); // Remove the 'v' prefix
return (
version.localeCompare(requiredVersion, undefined, { numeric: true }) >= 0
);
} else {
// If there is no match we assume that it's a version close to master so we return True
return true;
}
}
1 change: 1 addition & 0 deletions packages/tests/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from "./delay.js";
export * from "./base64_utf8.js";
export * from "./waitForConnections.js";
export * from "./custom_mocha_hooks.js";
export * from "./waku_versions_utils.js";
34 changes: 34 additions & 0 deletions packages/tests/src/utils/waku_versions_utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Logger } from "@waku/utils";

import { DOCKER_IMAGE_NAME } from "../lib/service_node";

const log = new Logger("test:utils");

// Utility to add test conditions based on nwaku/go-waku versions
export function isNwakuAtLeast(requiredVersion: string): boolean {
const versionRegex = /(?:v)?(\d+\.\d+(?:\.\d+)?)/;
const match = DOCKER_IMAGE_NAME.match(versionRegex);

if (match) {
const version = match[0].substring(1); // Remove the 'v' prefix
return (
version.localeCompare(requiredVersion, undefined, { numeric: true }) >= 0
);
} else {
// If there is no match we assume that it's a version close to master so we return True
return true;
}
}

// Utility to resolve autosharding cluster ID
export function resolveAutoshardingCluster(clusterId: number): number {
if (isNwakuAtLeast("0.27.0")) {
log.info(`Using clusterID ${clusterId} for autosharding`);
return clusterId;
} else {
// for versions older than 0.27.0 the autosharding cluster was hardcoded to 1
// https://github.com/waku-org/nwaku/pull/2505
log.warn("Falling back to clusterID 1 for autosharding");
return 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import { expect } from "chai";
import {
afterEachCustom,
beforeEachCustom,
isNwakuAtLeast,
makeLogFileName,
MessageCollector,
resolveAutoshardingCluster,
ServiceNode,
tearDownNodes
} from "../../../src/index.js";
Expand Down Expand Up @@ -186,6 +188,7 @@ describe("Waku Filter V2: Multiple PubsubTopics", function () {
describe("Waku Filter V2 (Autosharding): Multiple PubsubTopics", function () {
// Set the timeout for all tests in this suite. Can be overwritten at test level
this.timeout(30000);
const clusterId = resolveAutoshardingCluster(3);
let waku: LightNode;
let nwaku: ServiceNode;
let nwaku2: ServiceNode;
Expand All @@ -196,39 +199,45 @@ describe("Waku Filter V2 (Autosharding): Multiple PubsubTopics", function () {
const customContentTopic2 = "/myapp/1/latest/proto";
const autoshardingPubsubTopic1 = contentTopicToPubsubTopic(
customContentTopic1,
3
clusterId
);
const autoshardingPubsubTopic2 = contentTopicToPubsubTopic(
customContentTopic2,
3
clusterId
);
const contentTopicInfo: ContentTopicInfo = {
clusterId: 3,
clusterId: clusterId,
contentTopics: [customContentTopic1, customContentTopic2]
};
const customEncoder1 = createEncoder({
contentTopic: customContentTopic1,
pubsubTopicShardInfo: {
clusterId: 3,
clusterId: clusterId,
shard: contentTopicToShardIndex(customContentTopic1)
}
});
const customDecoder1 = createDecoder(customContentTopic1, {
clusterId: 3,
clusterId: clusterId,
shard: contentTopicToShardIndex(customContentTopic1)
});
const customEncoder2 = createEncoder({
contentTopic: customContentTopic2,
pubsubTopicShardInfo: {
clusterId: 3,
clusterId: clusterId,
shard: contentTopicToShardIndex(customContentTopic2)
}
});
const customDecoder2 = createDecoder(customContentTopic2, {
clusterId: 3,
clusterId: clusterId,
shard: contentTopicToShardIndex(customContentTopic2)
});

before(async () => {
if (!isNwakuAtLeast("0.27.0")) {
this.ctx.skip();
}
});

beforeEachCustom(this, async () => {
[nwaku, waku] = await runNodes(
this.ctx,
Expand Down Expand Up @@ -309,7 +318,8 @@ describe("Waku Filter V2 (Autosharding): Multiple PubsubTopics", function () {
lightpush: true,
relay: true,
pubsubTopic: [autoshardingPubsubTopic2],
clusterId: 3
clusterId: clusterId,
contentTopic: [customContentTopic2]
});
await waku.dial(await nwaku2.getMultiaddrWithId());
await waitForRemotePeer(waku, [Protocols.Filter, Protocols.LightPush]);
Expand Down
13 changes: 11 additions & 2 deletions packages/tests/tests/filter/single_node/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { waitForRemotePeer } from "@waku/core";
import {
ContentTopicInfo,
DefaultPubsubTopic,
LightNode,
ProtocolCreateOptions,
Expand All @@ -26,17 +27,25 @@ export async function runNodes(
): Promise<[ServiceNode, LightNode]> {
const nwaku = new ServiceNode(makeLogFileName(context));

function isContentTopicInfo(info: ShardingParams): info is ContentTopicInfo {
return (info as ContentTopicInfo).contentTopics !== undefined;
}

await nwaku.start(
{
filter: true,
lightpush: true,
relay: true,
pubsubTopic: pubsubTopics,
...(shardInfo && { clusterId: shardInfo.clusterId })
// Conditionally include clusterId if shardInfo exists
...(shardInfo && { clusterId: shardInfo.clusterId }),
// Conditionally include contentTopic if shardInfo exists and clusterId is 1
...(shardInfo &&
isContentTopicInfo(shardInfo) &&
shardInfo.clusterId === 1 && { contentTopic: shardInfo.contentTopics })
},
{ retries: 3 }
);

const waku_options: ProtocolCreateOptions = {
staticNoiseKey: NOISE_KEY_1,
libp2p: { addresses: { listen: ["/ip4/0.0.0.0/tcp/0/ws"] } },
Expand Down
28 changes: 21 additions & 7 deletions packages/tests/tests/getPeers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import Sinon from "sinon";
import {
afterEachCustom,
beforeEachCustom,
isNwakuAtLeast,
makeLogFileName,
resolveAutoshardingCluster,
ServiceNode,
tearDownNodes
} from "../src/index.js";
Expand All @@ -30,6 +32,13 @@ describe("getConnectedPeersForProtocolAndShard", function () {
let serviceNode1: ServiceNode;
let serviceNode2: ServiceNode;
const contentTopic = "/test/2/waku-light-push/utf8";
const autoshardingClusterId = resolveAutoshardingCluster(6);

before(async () => {
if (!isNwakuAtLeast("0.27.0")) {
this.ctx.skip();
}
});

beforeEachCustom(this, async () => {
serviceNode1 = new ServiceNode(makeLogFileName(this.ctx) + "1");
Expand Down Expand Up @@ -231,7 +240,7 @@ describe("getConnectedPeersForProtocolAndShard", function () {
this.timeout(15000);

const shardInfo: ContentTopicInfo = {
clusterId: 2,
clusterId: autoshardingClusterId,
contentTopics: [contentTopic]
};

Expand All @@ -240,6 +249,7 @@ describe("getConnectedPeersForProtocolAndShard", function () {
peerExchange: true,
clusterId: shardInfo.clusterId,
pubsubTopic: shardInfoToPubsubTopics(shardInfo),
contentTopic: [contentTopic],
lightpush: true,
relay: true
});
Expand All @@ -263,12 +273,12 @@ describe("getConnectedPeersForProtocolAndShard", function () {
this.timeout(15000);

const shardInfo1: ContentTopicInfo = {
clusterId: 2,
clusterId: autoshardingClusterId,
contentTopics: [contentTopic]
};

const shardInfo2: ContentTopicInfo = {
clusterId: 2,
clusterId: autoshardingClusterId,
contentTopics: ["/test/5/waku-light-push/utf8"]
};

Expand All @@ -278,6 +288,7 @@ describe("getConnectedPeersForProtocolAndShard", function () {
peerExchange: true,
clusterId: shardInfo1.clusterId,
pubsubTopic: shardInfoToPubsubTopics(shardInfo1),
contentTopic: [contentTopic],
lightpush: true,
relay: true
});
Expand All @@ -288,6 +299,7 @@ describe("getConnectedPeersForProtocolAndShard", function () {
peerExchange: true,
clusterId: shardInfo2.clusterId,
pubsubTopic: shardInfoToPubsubTopics(shardInfo2),
contentTopic: [contentTopic],
lightpush: true,
relay: true
});
Expand Down Expand Up @@ -315,12 +327,12 @@ describe("getConnectedPeersForProtocolAndShard", function () {
this.timeout(15000);

const shardInfo1: ContentTopicInfo = {
clusterId: 2,
clusterId: autoshardingClusterId,
contentTopics: [contentTopic]
};

const shardInfo2: ContentTopicInfo = {
clusterId: 3,
clusterId: 2,
contentTopics: [contentTopic]
};

Expand All @@ -330,6 +342,7 @@ describe("getConnectedPeersForProtocolAndShard", function () {
peerExchange: true,
clusterId: shardInfo1.clusterId,
pubsubTopic: shardInfoToPubsubTopics(shardInfo1),
contentTopic: [contentTopic],
lightpush: true,
relay: true
});
Expand Down Expand Up @@ -367,12 +380,12 @@ describe("getConnectedPeersForProtocolAndShard", function () {
this.timeout(15000);

const shardInfo1: ContentTopicInfo = {
clusterId: 2,
clusterId: autoshardingClusterId,
contentTopics: [contentTopic]
};

const shardInfo2: ContentTopicInfo = {
clusterId: 3,
clusterId: 2,
contentTopics: ["/test/5/waku-light-push/utf8"]
};

Expand All @@ -382,6 +395,7 @@ describe("getConnectedPeersForProtocolAndShard", function () {
peerExchange: true,
clusterId: shardInfo1.clusterId,
pubsubTopic: shardInfoToPubsubTopics(shardInfo1),
contentTopic: [contentTopic],
lightpush: true,
relay: true
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import { expect } from "chai";
import {
afterEachCustom,
beforeEachCustom,
isNwakuAtLeast,
makeLogFileName,
MessageCollector,
resolveAutoshardingCluster,
ServiceNode,
tearDownNodes
} from "../../../src/index.js";
Expand Down Expand Up @@ -177,7 +179,7 @@ describe("Waku Light Push (Autosharding): Multiple PubsubTopics", function () {
let nwaku2: ServiceNode;
let messageCollector: MessageCollector;

const clusterId = 2;
const clusterId = resolveAutoshardingCluster(4);
const customContentTopic1 = "/waku/2/content/test.js";
const customContentTopic2 = "/myapp/1/latest/proto";
const autoshardingPubsubTopic1 = contentTopicToPubsubTopic(
Expand All @@ -203,6 +205,12 @@ describe("Waku Light Push (Autosharding): Multiple PubsubTopics", function () {

let nimPeerId: PeerId;

before(async () => {
if (!isNwakuAtLeast("0.27.0")) {
this.ctx.skip();
}
});

beforeEachCustom(this, async () => {
[nwaku, waku] = await runNodes(
this.ctx,
Expand Down
13 changes: 12 additions & 1 deletion packages/tests/tests/light-push/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createEncoder, waitForRemotePeer } from "@waku/core";
import {
ContentTopicInfo,
DefaultPubsubTopic,
LightNode,
Protocols,
Expand All @@ -23,13 +24,23 @@ export async function runNodes(
shardInfo?: ShardingParams
): Promise<[ServiceNode, LightNode]> {
const nwaku = new ServiceNode(makeLogFileName(context));

function isContentTopicInfo(info: ShardingParams): info is ContentTopicInfo {
return (info as ContentTopicInfo).contentTopics !== undefined;
}

await nwaku.start(
{
lightpush: true,
filter: true,
relay: true,
pubsubTopic: pubsubTopics,
...(shardInfo && { clusterId: shardInfo.clusterId })
// Conditionally include clusterId if shardInfo exists
...(shardInfo && { clusterId: shardInfo.clusterId }),
// Conditionally include contentTopic if shardInfo exists and clusterId is 1
...(shardInfo &&
isContentTopicInfo(shardInfo) &&
shardInfo.clusterId === 1 && { contentTopic: shardInfo.contentTopics })
},
{ retries: 3 }
);
Expand Down
Loading

0 comments on commit f4c7c02

Please sign in to comment.