Skip to content

Commit

Permalink
tests: abstract away node creation and teardown utils
Browse files Browse the repository at this point in the history
  • Loading branch information
danisharora099 committed Jul 25, 2024
1 parent 5283f2e commit 5329a2f
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 155 deletions.
6 changes: 4 additions & 2 deletions packages/tests/src/utils/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export async function runMultipleNodes(
shardInfo?: ShardingParams,
customArgs?: Args,
strictChecking: boolean = false,
numServiceNodes = 3
numServiceNodes = 3,
withoutFilter = false
): Promise<[ServiceNodesFleet, LightNode]> {
const pubsubTopics = shardInfo
? shardInfoToPubsubTopics(shardInfo)
Expand All @@ -35,7 +36,8 @@ export async function runMultipleNodes(
numServiceNodes,
strictChecking,
shardInfo,
customArgs
customArgs,
withoutFilter
);

const wakuOptions: ProtocolCreateOptions = {
Expand Down
10 changes: 5 additions & 5 deletions packages/tests/tests/filter/peer_management.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ import { describe } from "mocha";
import {
afterEachCustom,
beforeEachCustom,
ServiceNode,
ServiceNodesFleet
} from "../../src/index.js";
import {
runMultipleNodes,
ServiceNode,
ServiceNodesFleet,
teardownNodesWithRedundancy
} from "../filter/utils.js";
} from "../../src/index.js";

describe("Waku Filter: Peer Management: E2E", function () {
this.timeout(15000);
Expand All @@ -46,6 +44,7 @@ describe("Waku Filter: Peer Management: E2E", function () {
this.ctx,
undefined,
undefined,
undefined,
5
);
const { error, subscription: sub } =
Expand Down Expand Up @@ -185,6 +184,7 @@ describe("Waku Filter: Peer Management: E2E", function () {
this.ctx,
undefined,
undefined,
undefined,
2
);
const serviceNodesPeerIdStr = await Promise.all(
Expand Down
6 changes: 3 additions & 3 deletions packages/tests/tests/filter/ping.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { expect } from "chai";
import {
afterEachCustom,
beforeEachCustom,
ServiceNodesFleet
runMultipleNodes,
ServiceNodesFleet,
teardownNodesWithRedundancy
} from "../../src/index.js";

import {
runMultipleNodes,
teardownNodesWithRedundancy,
TestContentTopic,
TestDecoder,
TestEncoder,
Expand Down
4 changes: 2 additions & 2 deletions packages/tests/tests/filter/push.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import {
afterEachCustom,
beforeEachCustom,
delay,
runMultipleNodes,
ServiceNodesFleet,
teardownNodesWithRedundancy,
TEST_STRING,
TEST_TIMESTAMPS
} from "../../src/index.js";

import {
messageText,
runMultipleNodes,
teardownNodesWithRedundancy,
TestContentTopic,
TestDecoder,
TestEncoder,
Expand Down
5 changes: 3 additions & 2 deletions packages/tests/tests/filter/subscribe.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import {
beforeEachCustom,
delay,
generateTestData,
runMultipleNodes,
ServiceNodesFleet,
teardownNodesWithRedundancy,
TEST_STRING
} from "../../src/index.js";

import {
messagePayload,
messageText,
runMultipleNodes,
teardownNodesWithRedundancy,
TestContentTopic,
TestDecoder,
TestEncoder,
Expand All @@ -42,6 +42,7 @@ const runTests = (strictCheckNodes: boolean): void => {
[serviceNodes, waku] = await runMultipleNodes(
this.ctx,
TestShardInfo,
undefined,
strictCheckNodes
);
const { error, subscription: _subscription } =
Expand Down
6 changes: 3 additions & 3 deletions packages/tests/tests/filter/unsubscribe.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import {
afterEachCustom,
beforeEachCustom,
generateTestData,
ServiceNodesFleet
runMultipleNodes,
ServiceNodesFleet,
teardownNodesWithRedundancy
} from "../../src/index.js";

import {
ClusterId,
messagePayload,
messageText,
runMultipleNodes,
teardownNodesWithRedundancy,
TestContentTopic,
TestDecoder,
TestEncoder,
Expand Down
133 changes: 3 additions & 130 deletions packages/tests/tests/filter/utils.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,7 @@
import { createDecoder, createEncoder, waitForRemotePeer } from "@waku/core";
import {
DefaultPubsubTopic,
ISubscriptionSDK,
LightNode,
ProtocolCreateOptions,
Protocols,
ShardingParams,
Waku
} from "@waku/interfaces";
import { createLightNode } from "@waku/sdk";
import {
contentTopicToPubsubTopic,
Logger,
shardInfoToPubsubTopics
} from "@waku/utils";
import { createDecoder, createEncoder } from "@waku/core";
import { ISubscriptionSDK } from "@waku/interfaces";
import { contentTopicToPubsubTopic, Logger } from "@waku/utils";
import { utf8ToBytes } from "@waku/utils/bytes";
import { Context } from "mocha";
import pRetry from "p-retry";

import {
NOISE_KEY_1,
ServiceNodesFleet,
waitForConnections
} from "../../src/index.js";

// Constants for test configuration.
export const log = new Logger("test:filter");
Expand Down Expand Up @@ -66,109 +45,3 @@ export async function validatePingError(
}
}
}

export async function runMultipleNodes(
context: Context,
shardInfo?: ShardingParams,
strictChecking: boolean = false,
numServiceNodes = 3,
withoutFilter = false
): Promise<[ServiceNodesFleet, LightNode]> {
const pubsubTopics = shardInfo
? shardInfoToPubsubTopics(shardInfo)
: [DefaultPubsubTopic];
// create numServiceNodes nodes
const serviceNodes = await ServiceNodesFleet.createAndRun(
context,
pubsubTopics,
numServiceNodes,
strictChecking,
shardInfo,
undefined,
withoutFilter
);

const wakuOptions: ProtocolCreateOptions = {
staticNoiseKey: NOISE_KEY_1,
libp2p: {
addresses: { listen: ["/ip4/0.0.0.0/tcp/0/ws"] }
}
};

if (shardInfo) {
wakuOptions.shardInfo = shardInfo;
} else {
wakuOptions.pubsubTopics = pubsubTopics;
}

log.info("Starting js waku node with :", JSON.stringify(wakuOptions));
let waku: LightNode | undefined;
try {
waku = await createLightNode(wakuOptions);
await waku.start();
} catch (error) {
log.error("jswaku node failed to start:", error);
}

if (!waku) {
throw new Error("Failed to initialize waku");
}

for (const node of serviceNodes.nodes) {
await waku.dial(await node.getMultiaddrWithId());
await waitForRemotePeer(waku, [Protocols.Filter, Protocols.LightPush]);
await node.ensureSubscriptions(pubsubTopics);

const wakuConnections = waku.libp2p.getConnections();
const nodePeers = await node.peers();

if (wakuConnections.length < 1 || nodePeers.length < 1) {
throw new Error(
`Expected at least 1 peer in each node. Got waku connections: ${wakuConnections.length} and service nodes: ${nodePeers.length}`
);
}
}

await waitForConnections(numServiceNodes, waku);

return [serviceNodes, waku];
}

export async function teardownNodesWithRedundancy(
serviceNodes: ServiceNodesFleet,
wakuNodes: Waku | Waku[]
): Promise<void> {
const wNodes = Array.isArray(wakuNodes) ? wakuNodes : [wakuNodes];

const stopNwakuNodes = serviceNodes.nodes.map(async (node) => {
await pRetry(
async () => {
try {
await node.stop();
} catch (error) {
log.error("Service Node failed to stop:", error);
throw error;
}
},
{ retries: 3 }
);
});

const stopWakuNodes = wNodes.map(async (waku) => {
if (waku) {
await pRetry(
async () => {
try {
await waku.stop();
} catch (error) {
log.error("Waku failed to stop:", error);
throw error;
}
},
{ retries: 3 }
);
}
});

await Promise.all([...stopNwakuNodes, ...stopWakuNodes]);
}
7 changes: 3 additions & 4 deletions packages/tests/tests/light-push/index.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ import {
afterEachCustom,
beforeEachCustom,
generateRandomUint8Array,
runMultipleNodes,
ServiceNodesFleet,
teardownNodesWithRedundancy,
TEST_STRING
} from "../../src";
import {
runMultipleNodes,
teardownNodesWithRedundancy
} from "../filter/utils.js";

import {
messagePayload,
Expand All @@ -36,6 +34,7 @@ const runTests = (strictNodeCheck: boolean): void => {
[serviceNodes, waku] = await runMultipleNodes(
this.ctx,
TestShardInfo,
undefined,
strictNodeCheck,
numServiceNodes,
true
Expand Down
7 changes: 3 additions & 4 deletions packages/tests/tests/light-push/peer_management.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import { describe } from "mocha";
import {
afterEachCustom,
beforeEachCustom,
ServiceNodesFleet
} from "../../src/index.js";
import {
runMultipleNodes,
ServiceNodesFleet,
teardownNodesWithRedundancy
} from "../filter/utils.js";
} from "../../src/index.js";

describe("Waku Light Push: Peer Management: E2E", function () {
this.timeout(15000);
Expand All @@ -23,6 +21,7 @@ describe("Waku Light Push: Peer Management: E2E", function () {
this.ctx,
undefined,
undefined,
undefined,
5
);
});
Expand Down

0 comments on commit 5329a2f

Please sign in to comment.