Merge pull request #1139 from waku-org/chore/test-improvements

This commit is contained in:
fryorcraken.eth 2023-01-30 21:37:40 +11:00 committed by GitHub
commit 7f5e1f61e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 19 deletions

View File

@ -52,7 +52,7 @@ jobs:
with:
node-version: ${{ env.NODE_JS }}
- uses: bahmutov/npm-install@v1
- run: npm run build
- run: npm run build:esm
- run: npm run test:browser
node:
@ -76,7 +76,7 @@ jobs:
with:
node-version: ${{ env.NODE_JS }}
- uses: bahmutov/npm-install@v1
- run: npm run build
- run: npm run build:esm
- run: npm run test:node
env:
DEBUG: ""
@ -130,7 +130,7 @@ jobs:
node-version: ${{ env.NODE_JS }}
- uses: bahmutov/npm-install@v1
- run: npm run build
- run: npm run build:esm
- run: npm run test:node
env:
DEBUG: "waku:nwaku*,waku:test*"
@ -184,7 +184,7 @@ jobs:
cd nwaku
./build/wakunode2 --help
- run: npm run build
- run: npm run build:esm
- run: npm run test:node
env:
DEBUG: "waku:nwaku*,waku:test*"

View File

@ -18,6 +18,7 @@
"scripts": {
"prepare": "husky install",
"build": "npm run build --workspaces --if-present",
"build:esm": "npm run build:esm --workspaces --if-present",
"size": "npm run build && size-limit",
"fix": "npm run fix --workspaces --if-present",
"check": "npm run check --workspaces --if-present",

View File

@ -32,7 +32,8 @@
"privacy"
],
"scripts": {
"build": "tsc",
"build": "run-s build:**",
"build:esm": "tsc",
"fix": "run-s fix:*",
"fix:prettier": "prettier . --write",
"fix:lint": "eslint src --ext .ts --ext .cjs --fix",

View File

@ -40,7 +40,7 @@
"pretest": "run-s pretest:*",
"pretest:1-init-git-submodules": "[ -f '../../nwaku/build/wakunode2' ] || git submodule update --init --recursive",
"pretest:2-build-nwaku": "[ -f '../../nwaku/build/wakunode2' ] || run-s nwaku:build",
"nwaku:build": "(PROC=$(nproc --all 2>/dev/null || echo 2); cd ../../nwaku; make -j$PROC update; NIMFLAGS=\"-d:chronicles_colors=off -d:chronicles_sinks=textlines\" make -j$PROC wakunode2)",
"nwaku:build": "(PROC=$(nproc --all 2>/dev/null || echo 2); cd ../../nwaku; make -j$PROC update; NIMFLAGS=\"-d:chronicles_colors=off\" make -j$PROC wakunode2)",
"nwaku:force-build": "(cd ../../nwaku && rm -rf ./build/ ./vendor) && run-s nwaku:build",
"check": "run-s check:*",
"check:prettier": "prettier . --list-different",

View File

@ -99,6 +99,7 @@ export class Nwaku {
private pid?: number;
private peerId?: PeerId;
private multiaddrWithId?: Multiaddr;
private websocketPort?: number;
private readonly logPath: string;
private rpcPort?: number;
@ -147,8 +148,12 @@ export class Nwaku {
const mergedArgs = defaultArgs();
// nwaku takes some time to bind port so to decrease chances of conflict
// we also randomize the first port that portfinder will try
const startPort = Math.floor(Math.random() * (65535 - 1025) + 1025);
const ports: number[] = await new Promise((resolve, reject) => {
portfinder.getPorts(3, {}, (err, ports) => {
portfinder.getPorts(3, { port: startPort }, (err, ports) => {
if (err) reject(err);
resolve(ports);
});
@ -167,6 +172,8 @@ export class Nwaku {
args
);
this.websocketPort = mergedArgs.websocketPort;
process.env.WAKUNODE2_STORE_MESSAGE_DB_URL = "";
const argsArray = argsToArray(mergedArgs);
@ -369,29 +376,40 @@ export class Nwaku {
}
async getPeerId(): Promise<PeerId> {
return await this._getPeerId().then((res) => res.peerId);
if (this.peerId) return this.peerId;
this.peerId = await this._getPeerId();
return this.peerId;
}
async getMultiaddrWithId(): Promise<Multiaddr> {
return await this._getPeerId().then((res) => res.multiaddrWithId);
if (this.multiaddrWithId) return this.multiaddrWithId;
if (!this.websocketPort) {
return Promise.reject("No websocket port defined.");
}
const peerId = await this.getPeerId();
this.multiaddrWithId = multiaddr(
`/ip4/127.0.0.1/tcp/${this.websocketPort}/ws/p2p/${peerId.toString()}`
);
return this.multiaddrWithId;
}
private async _getPeerId(): Promise<{
peerId: PeerId;
multiaddrWithId: Multiaddr;
}> {
if (this.peerId && this.multiaddrWithId) {
return { peerId: this.peerId, multiaddrWithId: this.multiaddrWithId };
private async _getPeerId(): Promise<PeerId> {
if (this.peerId) {
return this.peerId;
}
const res = await this.info();
this.multiaddrWithId = res.listenAddresses
const multiaddrWithId = res.listenAddresses
.map((ma) => multiaddr(ma))
.find((ma) => ma.protoNames().includes("ws"));
if (!this.multiaddrWithId) throw "Nwaku did not return a ws multiaddr";
const peerIdStr = this.multiaddrWithId.getPeerId();
if (!multiaddrWithId) throw "Nwaku did not return a ws multiaddr";
const peerIdStr = multiaddrWithId.getPeerId();
if (!peerIdStr) throw "Nwaku multiaddr does not contain peerId";
this.peerId = peerIdFromString(peerIdStr);
return { peerId: this.peerId, multiaddrWithId: this.multiaddrWithId };
return this.peerId;
}
get rpcUrl(): string {