2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2010-12-04 15:20:34 -08:00
|
|
|
var common = require('../common');
|
|
|
|
var assert = require('assert');
|
2010-03-15 15:11:40 -07:00
|
|
|
|
2010-12-04 15:20:34 -08:00
|
|
|
var net = require('net');
|
2009-12-30 10:58:46 -08:00
|
|
|
|
|
|
|
var tests_run = 0;
|
|
|
|
|
2010-12-05 22:15:30 +03:00
|
|
|
function pingPongTest(port, host) {
|
2009-12-30 10:58:46 -08:00
|
|
|
var N = 1000;
|
|
|
|
var count = 0;
|
2010-12-15 15:47:02 -08:00
|
|
|
var sentPongs = 0;
|
2009-12-30 10:58:46 -08:00
|
|
|
var sent_final_ping = false;
|
|
|
|
|
2010-12-05 01:45:52 +03:00
|
|
|
var server = net.createServer({ allowHalfOpen: true }, function(socket) {
|
|
|
|
console.log('connection: ' + socket.remoteAddress);
|
2009-12-30 10:58:46 -08:00
|
|
|
assert.equal(server, socket.server);
|
2010-08-15 14:01:44 -07:00
|
|
|
assert.equal(1, server.connections);
|
2009-12-30 10:58:46 -08:00
|
|
|
|
|
|
|
socket.setNoDelay();
|
|
|
|
socket.timeout = 0;
|
|
|
|
|
2010-03-19 02:24:11 -07:00
|
|
|
socket.setEncoding('utf8');
|
2011-10-15 01:08:36 +02:00
|
|
|
socket.on('data', function(data) {
|
2011-01-31 10:41:52 -08:00
|
|
|
// Since we never queue data (we're always waiting for the PING
|
|
|
|
// before sending a pong) the writeQueueSize should always be less
|
|
|
|
// than one message.
|
|
|
|
assert.ok(0 <= socket.bufferSize && socket.bufferSize <= 4);
|
|
|
|
|
2009-12-30 10:58:46 -08:00
|
|
|
assert.equal(true, socket.writable);
|
|
|
|
assert.equal(true, socket.readable);
|
|
|
|
assert.equal(true, count <= N);
|
2015-08-18 11:11:28 -07:00
|
|
|
assert.equal(data, 'PING');
|
|
|
|
|
|
|
|
socket.write('PONG', function() {
|
|
|
|
sentPongs++;
|
|
|
|
});
|
2009-12-30 10:58:46 -08:00
|
|
|
});
|
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
socket.on('end', function() {
|
2012-12-13 09:51:31 -08:00
|
|
|
assert.equal(true, socket.allowHalfOpen);
|
2010-10-25 22:04:39 -07:00
|
|
|
assert.equal(true, socket.writable); // because allowHalfOpen
|
2009-12-30 10:58:46 -08:00
|
|
|
assert.equal(false, socket.readable);
|
2010-04-08 10:44:22 -07:00
|
|
|
socket.end();
|
2009-12-30 10:58:46 -08:00
|
|
|
});
|
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
socket.on('error', function(e) {
|
2010-03-11 12:39:50 -08:00
|
|
|
throw e;
|
|
|
|
});
|
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
socket.on('close', function() {
|
2016-05-29 03:06:56 -04:00
|
|
|
console.log('server socket.end');
|
2009-12-30 10:58:46 -08:00
|
|
|
assert.equal(false, socket.writable);
|
|
|
|
assert.equal(false, socket.readable);
|
|
|
|
socket.server.close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2010-06-15 12:38:22 -07:00
|
|
|
|
2010-12-05 01:45:52 +03:00
|
|
|
server.listen(port, host, function() {
|
2016-05-29 03:06:56 -04:00
|
|
|
if (this.address().port)
|
|
|
|
port = this.address().port;
|
|
|
|
console.log(`server listening on ${port} ${host}`);
|
2009-12-30 10:58:46 -08:00
|
|
|
|
2009-12-30 11:51:43 -08:00
|
|
|
var client = net.createConnection(port, host);
|
2009-12-30 10:58:46 -08:00
|
|
|
|
2010-03-19 02:24:11 -07:00
|
|
|
client.setEncoding('ascii');
|
2011-10-15 01:08:36 +02:00
|
|
|
client.on('connect', function() {
|
2009-12-30 10:58:46 -08:00
|
|
|
assert.equal(true, client.readable);
|
|
|
|
assert.equal(true, client.writable);
|
2010-12-05 01:45:52 +03:00
|
|
|
client.write('PING');
|
2009-12-30 11:51:43 -08:00
|
|
|
});
|
2009-12-30 10:58:46 -08:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
client.on('data', function(data) {
|
2010-12-05 01:45:52 +03:00
|
|
|
assert.equal('PONG', data);
|
2009-12-30 11:51:43 -08:00
|
|
|
count += 1;
|
|
|
|
|
|
|
|
if (sent_final_ping) {
|
|
|
|
assert.equal(false, client.writable);
|
|
|
|
assert.equal(true, client.readable);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
assert.equal(true, client.writable);
|
|
|
|
assert.equal(true, client.readable);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count < N) {
|
2010-12-05 01:45:52 +03:00
|
|
|
client.write('PING');
|
2009-12-30 11:51:43 -08:00
|
|
|
} else {
|
|
|
|
sent_final_ping = true;
|
2010-12-05 01:45:52 +03:00
|
|
|
client.write('PING');
|
2010-04-08 10:44:22 -07:00
|
|
|
client.end();
|
2009-12-30 11:51:43 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
client.on('close', function() {
|
2010-12-15 15:47:02 -08:00
|
|
|
console.log('client.end');
|
2010-12-05 22:15:30 +03:00
|
|
|
assert.equal(N + 1, count);
|
2010-12-15 15:47:02 -08:00
|
|
|
assert.equal(N + 1, sentPongs);
|
2009-12-30 11:51:43 -08:00
|
|
|
assert.equal(true, sent_final_ping);
|
|
|
|
tests_run += 1;
|
|
|
|
});
|
2010-03-11 12:39:50 -08:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
client.on('error', function(e) {
|
2010-03-11 12:39:50 -08:00
|
|
|
throw e;
|
|
|
|
});
|
2009-12-30 10:58:46 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/* All are run at once, so run on different ports */
|
2015-10-06 21:12:45 -07:00
|
|
|
common.refreshTmpDir();
|
2011-07-21 21:22:56 +02:00
|
|
|
pingPongTest(common.PIPE);
|
2016-05-29 03:06:56 -04:00
|
|
|
pingPongTest(0);
|
|
|
|
pingPongTest(0, 'localhost');
|
2014-08-02 12:42:42 +04:00
|
|
|
if (common.hasIPv6)
|
2016-05-29 03:06:56 -04:00
|
|
|
pingPongTest(0, '::1');
|
2009-12-30 10:58:46 -08:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
process.on('exit', function() {
|
2014-08-02 12:42:42 +04:00
|
|
|
if (common.hasIPv6)
|
|
|
|
assert.equal(4, tests_run);
|
|
|
|
else
|
|
|
|
assert.equal(3, tests_run);
|
2010-06-23 17:40:51 -07:00
|
|
|
console.log('done');
|
2009-12-30 10:58:46 -08:00
|
|
|
});
|