From 057f5f80ab2ecfb771ba63c49d23e2644d5d88e1 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 11 Mar 2021 11:11:37 +1100 Subject: [PATCH] test: Nim-waku connects to JS (success) --- package-lock.json | 43 ++++++++++++++++++++++++++----- package.json | 3 +++ src/test_utils/log_file.ts | 53 ++++++++++++++++++++++++++++++++++++++ src/test_utils/nim_waku.ts | 11 ++++---- 4 files changed, 99 insertions(+), 11 deletions(-) create mode 100644 src/test_utils/log_file.ts diff --git a/package-lock.json b/package-lock.json index bf5313a586..cd2be1b20f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -917,6 +917,12 @@ "resolved": "https://registry.npmjs.org/@types/retry/-/retry-0.12.0.tgz", "integrity": "sha512-wWKOClTTiizcZhXnPY4wikVAwmdYHp8q6DmC+EJUzAMsycb7HB32Kh9RN4+0gExjmPmZSAQjgURXIGATPegAvA==" }, + "@types/tail": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@types/tail/-/tail-2.0.0.tgz", + "integrity": "sha512-TYTfnILhrZUAZKGNgot5+sBDap7oPIzV3818p7g4VhKGc81+/eoEZ93wKBTGnSg/tpDjzWSb8Wx5E737FCH/Sw==", + "dev": true + }, "@typescript-eslint/eslint-plugin": { "version": "4.16.1", "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-4.16.1.tgz", @@ -9900,6 +9906,17 @@ "dev": true, "requires": { "p-timeout": "^3.1.0" + }, + "dependencies": { + "p-timeout": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/p-timeout/-/p-timeout-3.2.0.tgz", + "integrity": "sha512-rhIwUycgwwKcP9yTOOFK/AKsAopjjCakVqLHePO3CC6Mir1Z99xT+R63jZxAT5lFZLa2inS5h+ZS2GvR99/FBg==", + "dev": true, + "requires": { + "p-finally": "^1.0.0" + } + } } }, "p-fifo": { @@ -9994,12 +10011,10 @@ } }, "p-timeout": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/p-timeout/-/p-timeout-3.2.0.tgz", - "integrity": "sha512-rhIwUycgwwKcP9yTOOFK/AKsAopjjCakVqLHePO3CC6Mir1Z99xT+R63jZxAT5lFZLa2inS5h+ZS2GvR99/FBg==", - "requires": { - "p-finally": "^1.0.0" - } + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/p-timeout/-/p-timeout-4.1.0.tgz", + "integrity": "sha512-+/wmHtzJuWii1sXn3HCuH/FTwGhrp4tmJTxSKJbfS+vkipci6osxXM5mY0jUiRzWKMTgUT8l7HFbeSwZAynqHw==", + "dev": true }, "p-try": { "version": "2.2.0", @@ -10012,6 +10027,16 @@ "integrity": "sha512-wpgERjNkLrBiFmkMEjuZJEWKKDrNfHCKA1OhyN1wg1FrLkULbviEy6py1AyJUgZ72YWFbZ38FIpnqvVqAlDUwA==", "requires": { "p-timeout": "^3.0.0" + }, + "dependencies": { + "p-timeout": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/p-timeout/-/p-timeout-3.2.0.tgz", + "integrity": "sha512-rhIwUycgwwKcP9yTOOFK/AKsAopjjCakVqLHePO3CC6Mir1Z99xT+R63jZxAT5lFZLa2inS5h+ZS2GvR99/FBg==", + "requires": { + "p-finally": "^1.0.0" + } + } } }, "package-hash": { @@ -11919,6 +11944,12 @@ } } }, + "tail": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/tail/-/tail-2.2.0.tgz", + "integrity": "sha512-QqUMtWlnzArTvGyjVnAE5fAiXEm2Psvk/BlE7vWx2/dIEWMsAhcNPz7iW6WTiSM8h1fjtCkRMsaWBS1j6rpGBg==", + "dev": true + }, "teeny-request": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/teeny-request/-/teeny-request-6.0.1.tgz", diff --git a/package.json b/package.json index 509285f46a..dede9efa6a 100644 --- a/package.json +++ b/package.json @@ -61,6 +61,7 @@ "@istanbuljs/nyc-config-typescript": "^1.0.1", "@types/axios": "^0.14.0", "@types/node": "^14.14.31", + "@types/tail": "^2.0.0", "@typescript-eslint/eslint-plugin": "^4.0.1", "@typescript-eslint/parser": "^4.0.1", "ava": "^3.15.0", @@ -80,8 +81,10 @@ "npm-run-all": "^4.1.5", "nyc": "^15.1.0", "open-cli": "^6.0.1", + "p-timeout": "^4.1.0", "prettier": "^2.1.1", "standard-version": "^9.0.0", + "tail": "^2.2.0", "ts-node": "^9.0.0", "typedoc": "^0.20.29", "typescript": "^4.0.2" diff --git a/src/test_utils/log_file.ts b/src/test_utils/log_file.ts new file mode 100644 index 0000000000..048d5309f3 --- /dev/null +++ b/src/test_utils/log_file.ts @@ -0,0 +1,53 @@ +import fs, { promises as asyncFs } from 'fs'; + +import pTimeout from 'p-timeout'; +import { Tail } from 'tail'; + +import { delay } from './delay'; + +const existsAsync = (filepath: string) => + asyncFs.access(filepath, fs.constants.F_OK); + +async function waitForFile(path: string) { + let found = false; + do { + try { + await existsAsync(path); + found = true; + } catch (e) { + await delay(500); + } + } while (!found); +} + +export default async function waitForLine(filepath: string, logLine: string) { + await pTimeout(waitForFile(filepath), 2000); + + const options = { + fromBeginning: true, + follow: true, + }; + + const tail = new Tail(filepath, options); + + await pTimeout( + find(tail, logLine), + 60000, + `could not to find '${logLine}' in file '${filepath}'` + ); + tail.unwatch(); +} + +async function find(tail: Tail, line: string) { + return new Promise((resolve, reject) => { + tail.on('line', (data: string) => { + if (data.includes(line)) { + resolve(data); + } + }); + + tail.on('error', (err) => { + reject(err); + }); + }); +} diff --git a/src/test_utils/nim_waku.ts b/src/test_utils/nim_waku.ts index f7ea1f34f6..4c32d90d86 100644 --- a/src/test_utils/nim_waku.ts +++ b/src/test_utils/nim_waku.ts @@ -7,7 +7,7 @@ import Multiaddr from 'multiaddr'; import multiaddr from 'multiaddr'; import PeerId from 'peer-id'; -import { delay } from './delay'; +import waitForLine from './log_file'; const openAsync = promisify(fs.open); @@ -35,7 +35,9 @@ export class NimWaku { // Start a local only node with the right RPC commands // The fixed nodekey ensures the node has a fixed Peerid: 16Uiu2HAkyzsXzENw5XBDYEQQAeQTCYjBJpMLgBmEXuwbtcrgxBJ4 - const logFile = await openAsync('./nim-waku.log', 'w'); + const logPath = './nim-waku.log'; + + const logFile = await openAsync(logPath, 'w'); const mergedArgs = argsToArray(mergeArguments(args)); console.log(mergedArgs); @@ -52,10 +54,9 @@ export class NimWaku { console.log(`ERROR: nim-waku node stopped: ${signal}`); }); - // TODO: Wait for line "RPC Server started " - await delay(5000); + await waitForLine(logPath, 'RPC Server started'); + console.log('Nim waku RPC is started'); console.log(await this.info()); - console.log('Nim waku is hopefully started'); } /** Calls nim-waku2 JSON-RPC API `get_waku_v2_admin_v1_peers` to check