node/test/pummel/test-net-pingpong.js

102 lines
2.6 KiB
JavaScript
Raw Normal View History

2010-12-05 07:20:34 +08:00
var common = require('../common');
var assert = require('assert');
2010-12-05 08:11:57 +08:00
var net = require('net');
2009-06-18 20:34:49 +08:00
var tests_run = 0;
2010-12-03 09:03:18 +08:00
function pingPongTest(port, host, on_complete) {
2009-06-18 20:34:49 +08:00
var N = 1000;
var count = 0;
var sent_final_ping = false;
2010-12-03 09:03:18 +08:00
var server = net.createServer({ allowHalfOpen: true }, function(socket) {
assert.equal(true, socket.remoteAddress !== null);
assert.equal(true, socket.remoteAddress !== undefined);
var address = socket.remoteAddress;
if (host === '127.0.0.1') {
assert.equal(address, '127.0.0.1');
} else if (host == null || host === 'localhost') {
assert(address === '127.0.0.1' || address === '::ffff:127.0.0.1');
} else {
console.log('host = ' + host + ', remoteAddress = ' + address);
assert.equal(address, '::1');
}
2009-06-18 20:34:49 +08:00
2010-12-03 09:03:18 +08:00
socket.setEncoding('utf8');
socket.setNoDelay();
2009-06-18 20:34:49 +08:00
socket.timeout = 0;
socket.on('data', function(data) {
2010-12-03 09:03:18 +08:00
console.log('server got: ' + JSON.stringify(data));
assert.equal('open', socket.readyState);
assert.equal(true, count <= N);
2009-06-18 20:34:49 +08:00
if (/PING/.exec(data)) {
2010-12-03 09:03:18 +08:00
socket.write('PONG');
2009-06-18 20:34:49 +08:00
}
2009-06-28 02:40:43 +08:00
});
2009-06-18 20:34:49 +08:00
socket.on('end', function() {
2010-12-03 09:03:18 +08:00
assert.equal('writeOnly', socket.readyState);
socket.end();
2009-06-28 02:40:43 +08:00
});
2009-06-18 20:34:49 +08:00
socket.on('close', function(had_error) {
assert.equal(false, had_error);
2010-12-03 09:03:18 +08:00
assert.equal('closed', socket.readyState);
2009-06-18 20:34:49 +08:00
socket.server.close();
2009-06-28 02:40:43 +08:00
});
2009-06-18 20:34:49 +08:00
});
2010-12-03 09:03:18 +08:00
server.listen(port, host, function() {
2010-08-13 23:54:40 +08:00
var client = net.createConnection(port, host);
2010-12-03 09:03:18 +08:00
client.setEncoding('utf8');
2009-04-22 21:52:23 +08:00
client.on('connect', function() {
2010-12-03 09:03:18 +08:00
assert.equal('open', client.readyState);
client.write('PING');
2010-08-13 23:54:40 +08:00
});
client.on('data', function(data) {
2010-08-13 23:54:40 +08:00
console.log('client got: ' + data);
2010-03-20 09:36:22 +08:00
2010-12-03 09:03:18 +08:00
assert.equal('PONG', data);
2010-08-13 23:54:40 +08:00
count += 1;
2010-08-13 23:54:40 +08:00
if (sent_final_ping) {
2010-12-03 09:03:18 +08:00
assert.equal('readOnly', client.readyState);
2010-08-13 23:54:40 +08:00
return;
} else {
2010-12-03 09:03:18 +08:00
assert.equal('open', client.readyState);
2010-08-13 23:54:40 +08:00
}
2010-08-13 23:54:40 +08:00
if (count < N) {
2010-12-03 09:03:18 +08:00
client.write('PING');
2010-08-13 23:54:40 +08:00
} else {
sent_final_ping = true;
2010-12-03 09:03:18 +08:00
client.write('PING');
2010-08-13 23:54:40 +08:00
client.end();
}
});
2009-06-18 20:34:49 +08:00
client.on('close', function() {
2010-12-03 09:03:18 +08:00
assert.equal(N + 1, count);
2010-08-13 23:54:40 +08:00
assert.equal(true, sent_final_ping);
if (on_complete) on_complete();
tests_run += 1;
});
2009-06-28 02:40:43 +08:00
});
2009-06-18 20:34:49 +08:00
}
2009-08-27 00:22:00 +08:00
/* All are run at once, so run on different ports */
2010-12-03 09:03:18 +08:00
pingPongTest(common.PORT, 'localhost');
pingPongTest(common.PORT + 1, null);
2010-03-03 05:10:05 +08:00
// This IPv6 isn't working on Solaris
var solaris = /sunos/i.test(process.platform);
2010-12-03 09:03:18 +08:00
if (!solaris) pingPongTest(common.PORT + 2, '::1');
process.on('exit', function() {
2010-03-03 05:10:05 +08:00
assert.equal(solaris ? 2 : 3, tests_run);
});