From 1295e44b38eb2649fce306d4574d1ca16a588640 Mon Sep 17 00:00:00 2001 From: acolytec3 <17355484+acolytec3@users.noreply.github.com> Date: Mon, 17 Jan 2022 16:08:59 -0500 Subject: [PATCH] most libp2p into separate file --- packages/client/test/cli/cli-libp2p.spec.ts | 77 +++++++++++++++++++++ packages/client/test/cli/cli-rpc.spec.ts | 63 ----------------- 2 files changed, 77 insertions(+), 63 deletions(-) create mode 100644 packages/client/test/cli/cli-libp2p.spec.ts diff --git a/packages/client/test/cli/cli-libp2p.spec.ts b/packages/client/test/cli/cli-libp2p.spec.ts new file mode 100644 index 0000000000..208e8c9a49 --- /dev/null +++ b/packages/client/test/cli/cli-libp2p.spec.ts @@ -0,0 +1,77 @@ +import { ChildProcessWithoutNullStreams, spawn } from 'child_process' +import tape from 'tape' + +const end = (child: ChildProcessWithoutNullStreams, hasEnded: boolean, st: tape.Test) => { + if (hasEnded) return + hasEnded = true + child.stdout.removeAllListeners() + child.stderr.removeAllListeners() + const res = child.kill('SIGINT') + st.ok(res, 'client shut down successfully') + st.end() +} + +tape('[CLI] rpc', (t) => { + t.test('libp2p should start up', (st) => { + const file = require.resolve('../../dist/bin/cli.js') + const child = spawn(process.execPath, [ + file, + ...[ + '--transports=libp2p', + '--dev', + '--lightserv=true', + '--multiaddrs=/ip4/127.0.0.1/tcp/50505/', + ], + ]) + let child2: ChildProcessWithoutNullStreams + const hasEnded = false + + child.stdout.on('data', async (data) => { + const message = data.toString() + + if (message.includes('transport=libp2p')) { + st.pass('libp2p server started') + const bootnodeAddressArray = message.split(' ') + const bootnodeAddressIndex = bootnodeAddressArray.findIndex((chunk: string) => + chunk.startsWith('url=') + ) + const bootNodeAddress = bootnodeAddressArray[bootnodeAddressIndex].split('=')[1] + child2 = spawn(process.execPath, [ + file, + ...[ + '--transports=libp2p', + `--bootnodes=${bootNodeAddress}`, + '--datadir=data2', + '--mine=false', + '--dev', + '--multiaddrs=/ip4/0.0.0.0/tcp/50506', + '--syncmode=light', + '--loglevel=debug', + ], + ]) + child2.stdout.on('data', async (data) => { + const message = data.toString() + if (message.includes('Peer added')) { + st.pass('connected to peer over libp2p') + child2.kill('SIGINT') + child2.stdout.removeAllListeners() + end(child, false, st) + } + }) + } + }) + + child.stderr.on('data', (data) => { + const message = data.toString() + st.fail(`stderr: ${message}`) + end(child, hasEnded, st) + }) + + child.on('close', (code) => { + if (code && code > 0) { + st.fail(`child process exited with code ${code}`) + end(child, hasEnded, st) + } + }) + }) +}) diff --git a/packages/client/test/cli/cli-rpc.spec.ts b/packages/client/test/cli/cli-rpc.spec.ts index 8aeb772163..7d5c764d42 100644 --- a/packages/client/test/cli/cli-rpc.spec.ts +++ b/packages/client/test/cli/cli-rpc.spec.ts @@ -88,67 +88,4 @@ tape('[CLI] rpc', (t) => { } }) }) - - t.test('libp2p should start up', (st) => { - const file = require.resolve('../../dist/bin/cli.js') - const child = spawn(process.execPath, [ - file, - ...[ - '--transports=libp2p', - '--dev', - '--lightserv=true', - '--multiaddrs=/ip4/127.0.0.1/tcp/50505/', - ], - ]) - let child2: ChildProcessWithoutNullStreams - const hasEnded = false - - child.stdout.on('data', async (data) => { - const message = data.toString() - - if (message.includes('transport=libp2p')) { - st.pass('libp2p server started') - const bootnodeAddressArray = message.split(' ') - const bootnodeAddressIndex = bootnodeAddressArray.findIndex((chunk: string) => - chunk.startsWith('url=') - ) - const bootNodeAddress = bootnodeAddressArray[bootnodeAddressIndex].split('=')[1] - child2 = spawn(process.execPath, [ - file, - ...[ - '--transports=libp2p', - `--bootnodes=${bootNodeAddress}`, - '--datadir=data2', - '--mine=false', - '--dev', - '--multiaddrs=/ip4/0.0.0.0/tcp/50506', - '--syncmode=light', - '--loglevel=debug', - ], - ]) - child2.stdout.on('data', async (data) => { - const message = data.toString() - if (message.includes('Peer added')) { - st.pass('connected to peer over libp2p') - child2.kill('SIGINT') - child2.stdout.removeAllListeners() - end(child, false, st) - } - }) - } - }) - - child.stderr.on('data', (data) => { - const message = data.toString() - st.fail(`stderr: ${message}`) - end(child, hasEnded, st) - }) - - child.on('close', (code) => { - if (code && code > 0) { - st.fail(`child process exited with code ${code}`) - end(child, hasEnded, st) - } - }) - }) })