-
Notifications
You must be signed in to change notification settings - Fork 775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Client: Add tests to verify shutdown #1641
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
02a949b
Add tests to verify client shutdown
acolytec3 55bb90d
Add libp2p test
acolytec3 050a1c9
Address feedback
acolytec3 975740c
Merge branch 'master' into client-shutdown-tests
acolytec3 1295e44
most libp2p into separate file
acolytec3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it possible to keep these here in the end() function, but just after the new st.ok check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just gave up on checking for the exit message. If you remove them in function after the kill signal is sent, it removes them before the client prints the "Exiting..." message to the console so the test never registers. But the
res
value oftrue
should be just as good because thatSIGINT
is what we listen for in the client to shut it down and if the result we get back is something screwy, that return value should befalse
in our test.