Merge pull request #901 from status-im/parameterized-tests

This commit is contained in:
fryorcraken.eth 2022-08-24 10:05:01 +10:00 committed by GitHub
commit ebb0e786cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 70 deletions

View File

@ -82,6 +82,7 @@ describe("Waku Light Push [node only]", () => {
dbg("Send message via lightpush"); dbg("Send message via lightpush");
const pushResponse = await waku.lightPush.push(message, { const pushResponse = await waku.lightPush.push(message, {
peerId: nimPeerId, peerId: nimPeerId,
pubSubTopic: customPubSubTopic,
}); });
dbg("Ack received", pushResponse); dbg("Ack received", pushResponse);
expect(pushResponse?.isSuccess).to.be.true; expect(pushResponse?.isSuccess).to.be.true;

View File

@ -36,7 +36,7 @@ describe("Waku Store", () => {
this.timeout(15_000); this.timeout(15_000);
nwaku = new Nwaku(makeLogFileName(this)); nwaku = new Nwaku(makeLogFileName(this));
await nwaku.start({ persistMessages: true }); await nwaku.start({ persistMessages: true, store: true });
for (let i = 0; i < 2; i++) { for (let i = 0; i < 2; i++) {
expect( expect(
@ -67,7 +67,7 @@ describe("Waku Store", () => {
this.timeout(15_000); this.timeout(15_000);
nwaku = new Nwaku(makeLogFileName(this)); nwaku = new Nwaku(makeLogFileName(this));
await nwaku.start({ persistMessages: true }); await nwaku.start({ persistMessages: true, store: true });
const totalMsgs = 20; const totalMsgs = 20;
@ -107,7 +107,7 @@ describe("Waku Store", () => {
this.timeout(15_000); this.timeout(15_000);
nwaku = new Nwaku(makeLogFileName(this)); nwaku = new Nwaku(makeLogFileName(this));
await nwaku.start({ persistMessages: true }); await nwaku.start({ persistMessages: true, store: true });
const availMsgs = 20; const availMsgs = 20;
@ -146,7 +146,7 @@ describe("Waku Store", () => {
this.timeout(15_000); this.timeout(15_000);
nwaku = new Nwaku(makeLogFileName(this)); nwaku = new Nwaku(makeLogFileName(this));
await nwaku.start({ persistMessages: true }); await nwaku.start({ persistMessages: true, store: true });
for (let i = 0; i < 15; i++) { for (let i = 0; i < 15; i++) {
expect( expect(
@ -184,7 +184,11 @@ describe("Waku Store", () => {
const customPubSubTopic = "/waku/2/custom-dapp/proto"; const customPubSubTopic = "/waku/2/custom-dapp/proto";
nwaku = new Nwaku(makeLogFileName(this)); nwaku = new Nwaku(makeLogFileName(this));
await nwaku.start({ persistMessages: true, topics: customPubSubTopic }); await nwaku.start({
persistMessages: true,
store: true,
topics: customPubSubTopic,
});
for (let i = 0; i < 2; i++) { for (let i = 0; i < 2; i++) {
expect( expect(
@ -222,7 +226,7 @@ describe("Waku Store", () => {
this.timeout(15_000); this.timeout(15_000);
nwaku = new Nwaku(makeLogFileName(this)); nwaku = new Nwaku(makeLogFileName(this));
await nwaku.start({ persistMessages: true, lightpush: true }); await nwaku.start({ persistMessages: true, store: true, lightpush: true });
const encryptedAsymmetricMessageText = const encryptedAsymmetricMessageText =
"This message is encrypted for me using asymmetric"; "This message is encrypted for me using asymmetric";
@ -317,7 +321,7 @@ describe("Waku Store", () => {
this.timeout(15_000); this.timeout(15_000);
nwaku = new Nwaku(makeLogFileName(this)); nwaku = new Nwaku(makeLogFileName(this));
await nwaku.start({ persistMessages: true, lightpush: true }); await nwaku.start({ persistMessages: true, store: true, lightpush: true });
const encryptedAsymmetricMessageText = const encryptedAsymmetricMessageText =
"This message is encrypted for me using asymmetric"; "This message is encrypted for me using asymmetric";
@ -424,7 +428,7 @@ describe("Waku Store", () => {
this.timeout(15_000); this.timeout(15_000);
nwaku = new Nwaku(makeLogFileName(this)); nwaku = new Nwaku(makeLogFileName(this));
await nwaku.start({ persistMessages: true }); await nwaku.start({ persistMessages: true, store: true });
const now = new Date(); const now = new Date();

View File

@ -1,6 +1,6 @@
import { expect } from "chai"; import { expect } from "chai";
import { argsToArray, bytesToHex, defaultArgs, strToHex } from "./nwaku"; import { argsToArray, defaultArgs } from "./nwaku";
describe("nwaku", () => { describe("nwaku", () => {
it("Correctly serialized arguments", function () { it("Correctly serialized arguments", function () {
@ -10,34 +10,16 @@ describe("nwaku", () => {
const actual = argsToArray(args); const actual = argsToArray(args);
const expected = [ const expected = [
"--nat=none",
"--listen-address=127.0.0.1", "--listen-address=127.0.0.1",
"--nat=none",
"--relay=true", "--relay=true",
"--rpc=true", "--rpc=true",
"--rpc-admin=true", "--rpc-admin=true",
"--websocket-support=true", "--websocket-support=true",
"--log-level=DEBUG",
"--ports-shift=42", "--ports-shift=42",
]; ];
expect(actual).to.deep.equal(expected); expect(actual).to.deep.equal(expected);
}); });
it("Convert utf-8 string to hex", function () {
const str = "This is an utf-8 string.";
const expected = "5468697320697320616e207574662d3820737472696e672e";
const actual = strToHex(str);
expect(actual).deep.equal(expected);
});
it("Convert buffer to hex", function () {
const buf = Uint8Array.from([
0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x6e, 0x20, 0x75,
0x74, 0x66, 0x2d, 0x38, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x2e,
]);
const expected = "5468697320697320616e207574662d3820737472696e672e";
const actual = bytesToHex(buf);
expect(actual).to.deep.equal(expected);
});
}); });

View File

@ -13,7 +13,7 @@ import debug from "debug";
import portfinder from "portfinder"; import portfinder from "portfinder";
import { DefaultPubSubTopic } from "../lib/constants"; import { DefaultPubSubTopic } from "../lib/constants";
import { hexToBytes } from "../lib/utils"; import { bytesToHex, hexToBytes } from "../lib/utils";
import { WakuMessage } from "../lib/waku_message"; import { WakuMessage } from "../lib/waku_message";
import * as proto from "../proto/message"; import * as proto from "../proto/message";
@ -22,8 +22,14 @@ import waitForLine from "./log_file";
const dbg = debug("waku:nwaku"); const dbg = debug("waku:nwaku");
const NIM_WAKU_DIR = appRoot + "/nwaku"; const WAKU_SERVICE_NODE_DIR =
const NIM_WAKU_BIN = NIM_WAKU_DIR + "/build/wakunode2"; process.env.WAKU_SERVICE_NODE_DIR ?? appRoot + "/nwaku";
const WAKU_SERVICE_NODE_BIN =
process.env.WAKU_SERVICE_NODE_BIN ??
WAKU_SERVICE_NODE_DIR + "/build/wakunode2";
const WAKU_SERVICE_NODE_PARAMS =
process.env.WAKU_SERVICE_NODE_PARAMS ?? undefined;
const NODE_READY_LOG_LINE = "Node setup complete";
const LOG_DIR = "./log"; const LOG_DIR = "./log";
@ -50,13 +56,13 @@ export interface Args {
} }
export enum LogLevel { export enum LogLevel {
Error = "error", Error = "ERROR",
Info = "info", Info = "INFO",
Warn = "warn", Warn = "WARN",
Debug = "debug", Debug = "DEBUG",
Trace = "trace", Trace = "TRACE",
Notice = "notice", Notice = "NOTICE",
Fatal = "fatal", Fatal = "FATAL",
} }
export interface KeyPair { export interface KeyPair {
@ -135,15 +141,17 @@ export class Nwaku {
tcpPort: ports[1], tcpPort: ports[1],
rpcPort: this.rpcPort, rpcPort: this.rpcPort,
websocketPort: ports[2], websocketPort: ports[2],
logLevel: LogLevel.Trace,
}, },
args args
); );
const argsArray = argsToArray(mergedArgs); const argsArray = argsToArray(mergedArgs);
if (WAKU_SERVICE_NODE_PARAMS) {
argsArray.push(WAKU_SERVICE_NODE_PARAMS);
}
dbg(`nwaku args: ${argsArray.join(" ")}`); dbg(`nwaku args: ${argsArray.join(" ")}`);
this.process = spawn(NIM_WAKU_BIN, argsArray, { this.process = spawn(WAKU_SERVICE_NODE_BIN, argsArray, {
cwd: NIM_WAKU_DIR, cwd: WAKU_SERVICE_NODE_DIR,
stdio: [ stdio: [
"ignore", // stdin "ignore", // stdin
logFile, // stdout logFile, // stdout
@ -171,8 +179,8 @@ export class Nwaku {
); );
}); });
dbg("Waiting to see 'Node setup complete' in nwaku logs"); dbg(`Waiting to see '${NODE_READY_LOG_LINE}' in nwaku logs`);
await this.waitForLog("Node setup complete", 15000); await this.waitForLog(NODE_READY_LOG_LINE, 15000);
dbg("nwaku node has been started"); dbg("nwaku node has been started");
} }
@ -217,7 +225,9 @@ export class Nwaku {
]); ]);
} }
async messages(pubsubTopic?: string): Promise<WakuMessage[]> { async messages(
pubsubTopic: string = DefaultPubSubTopic
): Promise<WakuMessage[]> {
this.checkProcess(); this.checkProcess();
const isDefined = (msg: WakuMessage | undefined): msg is WakuMessage => { const isDefined = (msg: WakuMessage | undefined): msg is WakuMessage => {
@ -226,7 +236,7 @@ export class Nwaku {
const protoMsgs = await this.rpcCall<proto.WakuMessage[]>( const protoMsgs = await this.rpcCall<proto.WakuMessage[]>(
"get_waku_v2_relay_v1_messages", "get_waku_v2_relay_v1_messages",
[pubsubTopic ?? DefaultPubSubTopic] [pubsubTopic]
); );
const msgs = await Promise.all( const msgs = await Promise.all(
@ -356,6 +366,7 @@ export class Nwaku {
method: string, method: string,
params: Array<string | number | unknown> params: Array<string | number | unknown>
): Promise<T> { ): Promise<T> {
dbg("RPC Query: ", method, params);
const res = await fetch(this.rpcUrl, { const res = await fetch(this.rpcUrl, {
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
@ -366,8 +377,8 @@ export class Nwaku {
}), }),
headers: new Headers({ "Content-Type": "application/json" }), headers: new Headers({ "Content-Type": "application/json" }),
}); });
const json = await res.json(); const json = await res.json();
dbg(`RPC Response: `, res, json);
return json.result; return json.result;
} }
@ -396,37 +407,16 @@ export function argsToArray(args: Args): Array<string> {
export function defaultArgs(): Args { export function defaultArgs(): Args {
return { return {
nat: "none",
listenAddress: "127.0.0.1", listenAddress: "127.0.0.1",
nat: "none",
relay: true, relay: true,
rpc: true, rpc: true,
rpcAdmin: true, rpcAdmin: true,
websocketSupport: true, websocketSupport: true,
logLevel: LogLevel.Debug,
}; };
} }
export function strToHex(str: string): string {
let hex: string;
try {
hex = unescape(encodeURIComponent(str))
.split("")
.map(function (v) {
return v.charCodeAt(0).toString(16);
})
.join("");
} catch (e) {
hex = str;
console.log("invalid text input: " + str);
}
return hex;
}
export function bytesToHex(buffer: Uint8Array): string {
return Array.prototype.map
.call(buffer, (x) => ("00" + x.toString(16)).slice(-2))
.join("");
}
interface RpcInfoResponse { interface RpcInfoResponse {
// multiaddrs including peer id. // multiaddrs including peer id.
listenAddresses: string[]; listenAddresses: string[];