mirror of https://github.com/lukechilds/node.git
Browse Source
There is no guarantee UDP messages will be received. Accommodate the occasional dropped message. This is a functionality test, not a performance benchmark. Speed up the test by not sending 1500 messages across three ports. Fixes: https://github.com/nodejs/node/issues/4526 PR-URL: https://github.com/nodejs/node/pull/5125 Reviewed-By: Brian White <mscdex@mscdex.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>v4.x
Rich Trott
9 years ago
committed by
Myles Borins
2 changed files with 21 additions and 65 deletions
@ -1,89 +1,46 @@ |
|||||
'use strict'; |
'use strict'; |
||||
var common = require('../common'); |
const common = require('../common'); |
||||
var assert = require('assert'); |
const assert = require('assert'); |
||||
var Buffer = require('buffer').Buffer; |
const dgram = require('dgram'); |
||||
var dgram = require('dgram'); |
|
||||
|
|
||||
var debug = false; |
|
||||
var tests_run = 0; |
|
||||
|
|
||||
function pingPongTest(port, host) { |
function pingPongTest(port, host) { |
||||
var callbacks = 0; |
|
||||
var N = 500; |
|
||||
var count = 0; |
|
||||
|
|
||||
var server = dgram.createSocket('udp4', function(msg, rinfo) { |
const server = dgram.createSocket('udp4', common.mustCall((msg, rinfo) => { |
||||
if (debug) console.log('server got: ' + msg + |
assert.strictEqual('PING', msg.toString('ascii')); |
||||
' from ' + rinfo.address + ':' + rinfo.port); |
server.send('PONG', 0, 4, rinfo.port, rinfo.address); |
||||
|
})); |
||||
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++; |
|
||||
}); |
|
||||
} |
|
||||
}); |
|
||||
|
|
||||
server.on('error', function(e) { |
server.on('error', function(e) { |
||||
throw e; |
throw e; |
||||
}); |
}); |
||||
|
|
||||
server.on('listening', function() { |
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'); |
const client = dgram.createSocket('udp4'); |
||||
|
|
||||
client.on('message', function(msg, rinfo) { |
client.on('message', function(msg) { |
||||
if (debug) console.log('client got: ' + msg + |
assert.strictEqual('PONG', msg.toString('ascii')); |
||||
' 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.close(); |
||||
}); |
|
||||
} |
|
||||
}); |
|
||||
|
|
||||
client.on('close', function() { |
|
||||
console.log('client has closed, closing server'); |
|
||||
assert.equal(N, count); |
|
||||
tests_run += 1; |
|
||||
server.close(); |
server.close(); |
||||
assert.equal(N - 1, callbacks); |
|
||||
}); |
}); |
||||
|
|
||||
client.on('error', function(e) { |
client.on('error', function(e) { |
||||
throw e; |
throw e; |
||||
}); |
}); |
||||
|
|
||||
console.log('Client sending to ' + port + ', localhost ' + buf); |
console.log('Client sending to ' + port); |
||||
client.send(buf, 0, buf.length, port, 'localhost', function(err, bytes) { |
|
||||
if (err) { |
function clientSend() { |
||||
throw err; |
client.send('PING', 0, 4, port, 'localhost'); |
||||
} |
} |
||||
console.log('Client sent ' + bytes + ' bytes'); |
|
||||
}); |
clientSend(); |
||||
count += 1; |
|
||||
}); |
}); |
||||
server.bind(port, host); |
server.bind(port, host); |
||||
|
return server; |
||||
} |
} |
||||
|
|
||||
// All are run at once, so run on different ports
|
const server = pingPongTest(common.PORT, 'localhost'); |
||||
pingPongTest(common.PORT + 0, 'localhost'); |
server.on('close', common.mustCall(pingPongTest.bind(undefined, common.PORT))); |
||||
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'); |
|
||||
}); |
|
||||
|
Loading…
Reference in new issue