2010-12-05 07:20:34 +08:00
|
|
|
var common = require('../common');
|
2010-12-06 06:33:52 +08:00
|
|
|
var assert = require('assert');
|
2010-12-05 08:11:57 +08:00
|
|
|
var net = require('net');
|
2009-05-02 22:34:24 +08:00
|
|
|
|
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) {
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal(true, socket.remoteAddress !== null);
|
|
|
|
assert.equal(true, socket.remoteAddress !== undefined);
|
2014-05-22 19:00:55 +08:00
|
|
|
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');
|
2010-04-03 04:19:02 +08:00
|
|
|
} else {
|
2014-05-22 19:00:55 +08:00
|
|
|
console.log('host = ' + host + ', remoteAddress = ' + address);
|
|
|
|
assert.equal(address, '::1');
|
2010-04-03 04:19:02 +08:00
|
|
|
}
|
2009-06-18 20:34:49 +08:00
|
|
|
|
2010-12-03 09:03:18 +08:00
|
|
|
socket.setEncoding('utf8');
|
2009-09-23 21:35:03 +08:00
|
|
|
socket.setNoDelay();
|
2009-06-18 20:34:49 +08:00
|
|
|
socket.timeout = 0;
|
|
|
|
|
2011-10-15 07:08:36 +08:00
|
|
|
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);
|
2009-11-29 01:26:59 +08:00
|
|
|
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
|
|
|
|
2011-10-15 07:08:36 +08:00
|
|
|
socket.on('end', function() {
|
2010-12-03 09:03:18 +08:00
|
|
|
assert.equal('writeOnly', socket.readyState);
|
2010-04-09 01:44:22 +08:00
|
|
|
socket.end();
|
2009-06-28 02:40:43 +08:00
|
|
|
});
|
2009-06-18 20:34:49 +08:00
|
|
|
|
2011-10-15 07:08:36 +08:00
|
|
|
socket.on('close', function(had_error) {
|
2009-11-29 01:26:59 +08:00
|
|
|
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
|
|
|
});
|
2009-05-15 05:47:21 +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);
|
2009-05-15 05:47:21 +08:00
|
|
|
|
2010-12-03 09:03:18 +08:00
|
|
|
client.setEncoding('utf8');
|
2009-04-22 21:52:23 +08:00
|
|
|
|
2011-10-15 07:08:36 +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
|
|
|
});
|
2009-05-02 22:34:24 +08:00
|
|
|
|
2011-10-15 07:08:36 +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;
|
2009-05-20 02:24:37 +08:00
|
|
|
|
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
|
|
|
}
|
2009-05-20 02:24:37 +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
|
|
|
|
2011-10-15 07:08:36 +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');
|
2009-06-09 01:10:23 +08:00
|
|
|
|
2011-10-15 07:08:36 +08:00
|
|
|
process.on('exit', function() {
|
2010-03-03 05:10:05 +08:00
|
|
|
assert.equal(solaris ? 2 : 3, tests_run);
|
2009-08-27 00:51:04 +08:00
|
|
|
});
|