2017-01-03 13:16:48 -08:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
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);
|
2017-06-07 12:49:00 -07:00
|
|
|
assert.strictEqual(
|
|
|
|
server,
|
2020-09-06 22:27:07 +02:00
|
|
|
server.getConnections(common.mustSucceed((connections) => {
|
2017-06-07 12:49:00 -07:00
|
|
|
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.
|
2018-03-02 02:44:18 +00:00
|
|
|
assert.ok(socket.bufferSize >= 0 && socket.bufferSize <= 4);
|
2011-01-31 10:41:52 -08:00
|
|
|
|
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);
|
2019-03-22 03:44:26 +01:00
|
|
|
assert.strictEqual(socket.writable, true); // Because allowHalfOpen
|
2016-11-26 12:20:21 +01:00
|
|
|
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;
|
|
|
|
}
|
2020-04-08 18:58:03 +02: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 */
|
2017-12-24 22:38:11 -08:00
|
|
|
const tmpdir = require('../common/tmpdir');
|
|
|
|
tmpdir.refresh();
|
2011-07-21 21:22:56 +02:00
|
|
|
pingPongTest(common.PIPE);
|
2016-05-29 03:06:56 -04:00
|
|
|
pingPongTest(0);
|
2020-01-29 13:12:15 +01:00
|
|
|
if (common.hasIPv6) pingPongTest(0, '::1'); else pingPongTest(0, '127.0.0.1');
|