2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2016-11-26 12:20:21 +01:00
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
const net = require('net');
|
2009-12-30 10:58:46 -08:00
|
|
|
|
2010-12-05 22:15:30 +03:00
|
|
|
function pingPongTest(port, host) {
|
2016-11-26 12:20:21 +01:00
|
|
|
const N = 1000;
|
2017-01-08 13:19:00 +00:00
|
|
|
let count = 0;
|
|
|
|
let sentPongs = 0;
|
|
|
|
let sent_final_ping = false;
|
2009-12-30 10:58:46 -08:00
|
|
|
|
2016-11-26 12:20:21 +01:00
|
|
|
const server = net.createServer(
|
|
|
|
{ allowHalfOpen: true },
|
|
|
|
common.mustCall(onSocket)
|
|
|
|
);
|
|
|
|
|
|
|
|
function onSocket(socket) {
|
|
|
|
assert.strictEqual(socket.server, server);
|
|
|
|
server.getConnections(common.mustCall(function(err, connections) {
|
|
|
|
assert.ifError(err);
|
|
|
|
assert.strictEqual(connections, 1);
|
|
|
|
}));
|
2009-12-30 10:58:46 -08:00
|
|
|
|
|
|
|
socket.setNoDelay();
|
|
|
|
socket.timeout = 0;
|
|
|
|
|
2010-03-19 02:24:11 -07:00
|
|
|
socket.setEncoding('utf8');
|
2016-11-26 12:20:21 +01:00
|
|
|
socket.on('data', common.mustCall(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);
|
|
|
|
|
2016-11-26 12:20:21 +01:00
|
|
|
assert.strictEqual(socket.writable, true);
|
|
|
|
assert.strictEqual(socket.readable, true);
|
|
|
|
assert.ok(count <= N);
|
|
|
|
assert.strictEqual(data, 'PING');
|
2015-08-18 11:11:28 -07:00
|
|
|
|
2016-11-26 12:20:21 +01:00
|
|
|
socket.write('PONG', common.mustCall(function() {
|
2015-08-18 11:11:28 -07:00
|
|
|
sentPongs++;
|
2016-11-26 12:20:21 +01:00
|
|
|
}));
|
|
|
|
}, N + 1));
|
2009-12-30 10:58:46 -08:00
|
|
|
|
2016-11-26 12:20:21 +01:00
|
|
|
socket.on('end', common.mustCall(function() {
|
|
|
|
assert.strictEqual(socket.allowHalfOpen, true);
|
|
|
|
assert.strictEqual(socket.writable, true); // because allowHalfOpen
|
|
|
|
assert.strictEqual(socket.readable, false);
|
2010-04-08 10:44:22 -07:00
|
|
|
socket.end();
|
2016-11-26 12:20:21 +01:00
|
|
|
}));
|
2009-12-30 10:58:46 -08:00
|
|
|
|
2017-02-03 14:54:19 -05:00
|
|
|
socket.on('error', common.mustNotCall());
|
2010-03-11 12:39:50 -08:00
|
|
|
|
2016-11-26 12:20:21 +01:00
|
|
|
socket.on('close', common.mustCall(function() {
|
|
|
|
assert.strictEqual(socket.writable, false);
|
|
|
|
assert.strictEqual(socket.readable, false);
|
2009-12-30 10:58:46 -08:00
|
|
|
socket.server.close();
|
2016-11-26 12:20:21 +01:00
|
|
|
}));
|
|
|
|
}
|
2009-12-30 10:58:46 -08:00
|
|
|
|
2010-06-15 12:38:22 -07:00
|
|
|
|
2016-11-26 12:20:21 +01:00
|
|
|
server.listen(port, host, common.mustCall(function() {
|
2016-05-29 03:06:56 -04:00
|
|
|
if (this.address().port)
|
|
|
|
port = this.address().port;
|
2009-12-30 10:58:46 -08:00
|
|
|
|
2016-11-26 12:20:21 +01:00
|
|
|
const client = net.createConnection(port, host);
|
2009-12-30 10:58:46 -08:00
|
|
|
|
2010-03-19 02:24:11 -07:00
|
|
|
client.setEncoding('ascii');
|
2016-11-26 12:20:21 +01:00
|
|
|
client.on('connect', common.mustCall(function() {
|
|
|
|
assert.strictEqual(client.readable, true);
|
|
|
|
assert.strictEqual(client.writable, true);
|
2010-12-05 01:45:52 +03:00
|
|
|
client.write('PING');
|
2016-11-26 12:20:21 +01:00
|
|
|
}));
|
2009-12-30 10:58:46 -08:00
|
|
|
|
2016-11-26 12:20:21 +01:00
|
|
|
client.on('data', common.mustCall(function(data) {
|
|
|
|
assert.strictEqual(data, 'PONG');
|
2009-12-30 11:51:43 -08:00
|
|
|
count += 1;
|
|
|
|
|
|
|
|
if (sent_final_ping) {
|
2016-11-26 12:20:21 +01:00
|
|
|
assert.strictEqual(client.writable, false);
|
|
|
|
assert.strictEqual(client.readable, true);
|
2009-12-30 11:51:43 -08:00
|
|
|
return;
|
|
|
|
} else {
|
2016-11-26 12:20:21 +01:00
|
|
|
assert.strictEqual(client.writable, true);
|
|
|
|
assert.strictEqual(client.readable, true);
|
2009-12-30 11:51:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2016-11-26 12:20:21 +01:00
|
|
|
}, N + 1));
|
|
|
|
|
|
|
|
client.on('close', common.mustCall(function() {
|
|
|
|
assert.strictEqual(count, N + 1);
|
|
|
|
assert.strictEqual(sentPongs, N + 1);
|
|
|
|
assert.strictEqual(sent_final_ping, true);
|
|
|
|
}));
|
|
|
|
|
2017-02-03 14:54:19 -05:00
|
|
|
client.on('error', common.mustNotCall());
|
2016-11-26 12:20:21 +01:00
|
|
|
}));
|
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');
|