mirror of https://github.com/lukechilds/node.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
2.3 KiB
91 lines
2.3 KiB
14 years ago
|
var common = require('../common');
|
||
|
var assert = require('assert');
|
||
15 years ago
|
var Buffer = require('buffer').Buffer;
|
||
14 years ago
|
var dgram = require('dgram');
|
||
15 years ago
|
|
||
13 years ago
|
var debug = false;
|
||
15 years ago
|
var tests_run = 0;
|
||
|
|
||
14 years ago
|
function pingPongTest(port, host) {
|
||
15 years ago
|
var callbacks = 0;
|
||
15 years ago
|
var N = 500;
|
||
|
var count = 0;
|
||
|
var sent_final_ping = false;
|
||
|
|
||
14 years ago
|
var server = dgram.createSocket('udp4', function(msg, rinfo) {
|
||
13 years ago
|
if (debug) console.log('server got: ' + msg +
|
||
|
' from ' + rinfo.address + ':' + rinfo.port);
|
||
15 years ago
|
|
||
|
if (/PING/.exec(msg)) {
|
||
|
var buf = new Buffer(4);
|
||
|
buf.write('PONG');
|
||
14 years ago
|
server.send(buf, 0, buf.length,
|
||
|
rinfo.port, rinfo.address,
|
||
|
function(err, sent) {
|
||
|
callbacks++;
|
||
|
});
|
||
15 years ago
|
}
|
||
|
});
|
||
|
|
||
14 years ago
|
server.on('error', function(e) {
|
||
15 years ago
|
throw e;
|
||
|
});
|
||
|
|
||
14 years ago
|
server.on('listening', function() {
|
||
|
console.log('server listening on ' + port + ' ' + host);
|
||
15 years ago
|
|
||
14 years ago
|
var buf = new Buffer('PING'),
|
||
|
client = dgram.createSocket('udp4');
|
||
15 years ago
|
|
||
13 years ago
|
client.on('message', function(msg, rinfo) {
|
||
13 years ago
|
if (debug) console.log('client got: ' + msg +
|
||
|
' from ' + rinfo.address + ':' + rinfo.port);
|
||
14 years ago
|
assert.equal('PONG', msg.toString('ascii'));
|
||
15 years ago
|
|
||
|
count += 1;
|
||
|
|
||
|
if (count < N) {
|
||
14 years ago
|
client.send(buf, 0, buf.length, port, 'localhost');
|
||
15 years ago
|
} else {
|
||
|
sent_final_ping = true;
|
||
13 years ago
|
client.send(buf, 0, buf.length, port, 'localhost', function() {
|
||
15 years ago
|
client.close();
|
||
|
});
|
||
|
}
|
||
|
});
|
||
|
|
||
14 years ago
|
client.on('close', function() {
|
||
15 years ago
|
console.log('client has closed, closing server');
|
||
15 years ago
|
assert.equal(N, count);
|
||
|
tests_run += 1;
|
||
|
server.close();
|
||
14 years ago
|
assert.equal(N - 1, callbacks);
|
||
15 years ago
|
});
|
||
|
|
||
14 years ago
|
client.on('error', function(e) {
|
||
15 years ago
|
throw e;
|
||
|
});
|
||
|
|
||
14 years ago
|
console.log('Client sending to ' + port + ', localhost ' + buf);
|
||
|
client.send(buf, 0, buf.length, port, 'localhost', function(err, bytes) {
|
||
15 years ago
|
if (err) {
|
||
|
throw err;
|
||
|
}
|
||
14 years ago
|
console.log('Client sent ' + bytes + ' bytes');
|
||
15 years ago
|
});
|
||
15 years ago
|
count += 1;
|
||
|
});
|
||
15 years ago
|
server.bind(port, host);
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
// All are run at once, so run on different ports
|
||
12 years ago
|
pingPongTest(common.PORT + 0, 'localhost');
|
||
|
pingPongTest(common.PORT + 1, 'localhost');
|
||
|
pingPongTest(common.PORT + 2);
|
||
14 years ago
|
//pingPongTest('/tmp/pingpong.sock');
|
||
15 years ago
|
|
||
13 years ago
|
process.on('exit', function() {
|
||
15 years ago
|
assert.equal(3, tests_run);
|
||
15 years ago
|
console.log('done');
|
||
15 years ago
|
});
|