-
Notifications
You must be signed in to change notification settings - Fork 30.2k
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
test: fix flaky test-dgram-pingpong #5125
Closed
Closed
Changes from all commits
Commits
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
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 |
---|---|---|
@@ -1,89 +1,46 @@ | ||
'use strict'; | ||
var common = require('../common'); | ||
var assert = require('assert'); | ||
var Buffer = require('buffer').Buffer; | ||
var dgram = require('dgram'); | ||
|
||
var debug = false; | ||
var tests_run = 0; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const dgram = require('dgram'); | ||
|
||
function pingPongTest(port, host) { | ||
var callbacks = 0; | ||
var N = 500; | ||
var count = 0; | ||
|
||
var server = dgram.createSocket('udp4', function(msg, rinfo) { | ||
if (debug) console.log('server got: ' + msg + | ||
' from ' + rinfo.address + ':' + rinfo.port); | ||
|
||
if (/PING/.exec(msg)) { | ||
var buf = new Buffer(4); | ||
buf.write('PONG'); | ||
server.send(buf, 0, buf.length, | ||
rinfo.port, rinfo.address, | ||
function(err, sent) { | ||
callbacks++; | ||
}); | ||
} | ||
}); | ||
const server = dgram.createSocket('udp4', common.mustCall((msg, rinfo) => { | ||
assert.strictEqual('PING', msg.toString('ascii')); | ||
server.send('PONG', 0, 4, rinfo.port, rinfo.address); | ||
})); | ||
|
||
server.on('error', function(e) { | ||
throw e; | ||
}); | ||
|
||
server.on('listening', function() { | ||
console.log('server listening on ' + port + ' ' + host); | ||
console.log('server listening on ' + port); | ||
|
||
const buf = new Buffer('PING'); | ||
const client = dgram.createSocket('udp4'); | ||
|
||
client.on('message', function(msg, rinfo) { | ||
if (debug) console.log('client got: ' + msg + | ||
' from ' + rinfo.address + ':' + rinfo.port); | ||
assert.equal('PONG', msg.toString('ascii')); | ||
|
||
count += 1; | ||
|
||
if (count < N) { | ||
client.send(buf, 0, buf.length, port, 'localhost'); | ||
} else { | ||
client.send(buf, 0, buf.length, port, 'localhost', function() { | ||
client.close(); | ||
}); | ||
} | ||
}); | ||
client.on('message', function(msg) { | ||
assert.strictEqual('PONG', msg.toString('ascii')); | ||
|
||
client.on('close', function() { | ||
console.log('client has closed, closing server'); | ||
assert.equal(N, count); | ||
tests_run += 1; | ||
client.close(); | ||
server.close(); | ||
assert.equal(N - 1, callbacks); | ||
}); | ||
|
||
client.on('error', function(e) { | ||
throw e; | ||
}); | ||
|
||
console.log('Client sending to ' + port + ', localhost ' + buf); | ||
client.send(buf, 0, buf.length, port, 'localhost', function(err, bytes) { | ||
if (err) { | ||
throw err; | ||
} | ||
console.log('Client sent ' + bytes + ' bytes'); | ||
}); | ||
count += 1; | ||
console.log('Client sending to ' + port); | ||
|
||
function clientSend() { | ||
client.send('PING', 0, 4, port, 'localhost'); | ||
} | ||
|
||
clientSend(); | ||
}); | ||
server.bind(port, host); | ||
return server; | ||
} | ||
|
||
// All are run at once, so run on different ports | ||
pingPongTest(common.PORT + 0, 'localhost'); | ||
pingPongTest(common.PORT + 1, 'localhost'); | ||
pingPongTest(common.PORT + 2); | ||
//pingPongTest('/tmp/pingpong.sock'); | ||
|
||
process.on('exit', function() { | ||
assert.equal(3, tests_run); | ||
console.log('done'); | ||
}); | ||
const server = pingPongTest(common.PORT, 'localhost'); | ||
server.on('close', common.mustCall(pingPongTest.bind(undefined, common.PORT))); |
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.
Perhaps the callback here should be wrapped with
common.mustCall(..., N)
?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 could go either way on it. I left it without
common.mustCall()
and used theassert.strictEqual(pongsReceived, N);
instead so that it would be readily apparent that it's the analogous check toassert(pingsReceived >= N);
.