From 090b064c84ec9ab1010a62cdb4e3468fac16a4f4 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Fri, 19 Mar 2021 16:07:56 +1100 Subject: [PATCH] Kill nim-waku & js-libp2p nodes after tests --- src/lib/waku.spec.ts | 3 +++ src/lib/waku.ts | 4 ++++ src/lib/waku_relay.spec.ts | 44 ++++++++++++++++---------------------- src/test_utils/nim_waku.ts | 8 +++---- 4 files changed, 30 insertions(+), 29 deletions(-) diff --git a/src/lib/waku.spec.ts b/src/lib/waku.spec.ts index ceaf19f563..18e89c70b5 100644 --- a/src/lib/waku.spec.ts +++ b/src/lib/waku.spec.ts @@ -32,6 +32,9 @@ describe('Waku', () => { const jsPeers = waku.libp2p.peerStore.peers; expect(jsPeers.has(nimPeerId.toB58String())).toBeTruthy(); + + nimWaku.stop(); + await waku.stop(); }); }); }); diff --git a/src/lib/waku.ts b/src/lib/waku.ts index 6a220d80b7..468da38edc 100644 --- a/src/lib/waku.ts +++ b/src/lib/waku.ts @@ -27,4 +27,8 @@ export default class Waku { return new Waku(libp2p, new WakuRelay(libp2p.pubsub)); } + + async stop() { + await this.libp2p.stop(); + } } diff --git a/src/lib/waku_relay.spec.ts b/src/lib/waku_relay.spec.ts index a56d1966af..fa50aa1fa5 100644 --- a/src/lib/waku_relay.spec.ts +++ b/src/lib/waku_relay.spec.ts @@ -37,6 +37,8 @@ describe('Waku Relay', () => { const node1Received = await promise; expect(node1Received.isEqualTo(message)).toBeTruthy(); + + await Promise.all([waku1.stop(), waku2.stop()]); }); test('Registers waku relay protocol', async () => { @@ -45,6 +47,8 @@ describe('Waku Relay', () => { const protocols = Array.from(waku.libp2p.upgrader.protocols.keys()); expect(protocols.findIndex((value) => value == CODEC)).toBeTruthy(); + + await waku.stop(); }); test('Does not register any sub protocol', async () => { @@ -52,22 +56,33 @@ describe('Waku Relay', () => { const protocols = Array.from(waku.libp2p.upgrader.protocols.keys()); expect(protocols.findIndex((value) => value.match(/sub/))).toBeTruthy(); + + await waku.stop(); }); describe('Interop: Nim', () => { - test('nim subscribes to js', async () => { - const waku = await Waku.create(); + let waku: Waku; + let nimWaku: NimWaku; + + beforeEach(async () => { + waku = await Waku.create(); const peerId = waku.libp2p.peerId.toB58String(); - const localMultiaddr = waku.libp2p.multiaddrs.find((addr) => addr.toString().match(/127\.0\.0\.1/) ); const multiAddrWithId = localMultiaddr + '/p2p/' + peerId; - const nimWaku = new NimWaku(expect.getState().currentTestName); + nimWaku = new NimWaku(expect.getState().currentTestName); await nimWaku.start({ staticnode: multiAddrWithId }); + }); + afterEach(async () => { + nimWaku ? nimWaku.stop() : null; + waku ? await waku.stop() : null; + }); + + test('nim subscribes to js', async () => { const nimPeerId = await nimWaku.getPeerId(); const subscribers = waku.libp2p.pubsub.getSubscribers(TOPIC); @@ -76,22 +91,11 @@ describe('Waku Relay', () => { test('Js publishes to nim', async () => { const message = Message.fromUtf8String('This is a message'); - const waku = await Waku.create(); - // TODO: nim-waku does follow the `StrictNoSign` policy hence we need to change // it for nim-waku to process our messages. Can be removed once // https://github.com/status-im/nim-waku/issues/422 is fixed waku.libp2p.pubsub.globalSignaturePolicy = 'StrictSign'; - const peerId = waku.libp2p.peerId.toB58String(); - const localMultiaddr = waku.libp2p.multiaddrs.find((addr) => - addr.toString().match(/127\.0\.0\.1/) - ); - const multiAddrWithId = localMultiaddr + '/p2p/' + peerId; - - const nimWaku = new NimWaku(expect.getState().currentTestName); - await nimWaku.start({ staticnode: multiAddrWithId }); - await patchPeerStore(nimWaku, waku.libp2p); await waku.relay.publish(message); @@ -109,16 +113,6 @@ describe('Waku Relay', () => { test('Nim publishes to js', async () => { const message = Message.fromUtf8String('Here is another message.'); - const waku = await Waku.create(); - - const peerId = waku.libp2p.peerId.toB58String(); - const localMultiaddr = waku.libp2p.multiaddrs.find((addr) => - addr.toString().match(/127\.0\.0\.1/) - ); - const multiAddrWithId = localMultiaddr + '/p2p/' + peerId; - - const nimWaku = new NimWaku(expect.getState().currentTestName); - await nimWaku.start({ staticnode: multiAddrWithId }); await patchPeerStore(nimWaku, waku.libp2p); diff --git a/src/test_utils/nim_waku.ts b/src/test_utils/nim_waku.ts index c0b0fd2b11..1c3868af50 100644 --- a/src/test_utils/nim_waku.ts +++ b/src/test_utils/nim_waku.ts @@ -74,13 +74,13 @@ export class NimWaku { ], }); - this.process.on('exit', (signal) => { - console.log(`ERROR: nim-waku node stopped: ${signal}`); - }); - await this.waitForLog('RPC Server started'); } + public stop() { + this.process ? this.process.kill('SIGINT') : null; + } + async waitForLog(msg: string) { return waitForLine(this.logPath, msg); }