Skip to content

Commit

Permalink
Client: Add tests to verify shutdown (#1641)
Browse files Browse the repository at this point in the history
* Add tests to verify client shutdown

* Add libp2p test

* Address feedback

* most libp2p into separate file
  • Loading branch information
acolytec3 authored Jan 18, 2022
1 parent 9440a83 commit 6bb2f23
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
77 changes: 77 additions & 0 deletions packages/client/test/cli/cli-libp2p.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
}
})
})
})
4 changes: 2 additions & 2 deletions packages/client/test/cli/cli-rpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const end = (child: ChildProcessWithoutNullStreams, hasEnded: boolean, st: tape.
hasEnded = true
child.stdout.removeAllListeners()
child.stderr.removeAllListeners()
child.kill('SIGINT')
const res = child.kill('SIGINT')
st.ok(res, 'client shut down successfully')
st.end()
}

Expand All @@ -21,7 +22,6 @@ tape('[CLI] rpc', (t) => {

child.stdout.on('data', async (data) => {
const message = data.toString()

if (message.includes('http://')) {
// if http endpoint startup message detected, call http endpoint with RPC method
const client = Client.http({ port: 8545 })
Expand Down

0 comments on commit 6bb2f23

Please sign in to comment.