2017-01-03 21:16:48 +00: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 11:00:06 +00:00
|
|
|
'use strict';
|
2020-02-12 03:14:57 +00:00
|
|
|
require('../common');
|
2016-12-30 23:38:06 +00:00
|
|
|
const assert = require('assert');
|
|
|
|
const net = require('net');
|
2010-12-05 00:11:57 +00:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
let exchanges = 0;
|
|
|
|
let starttime = null;
|
|
|
|
let timeouttime = null;
|
|
|
|
const timeout = 1000;
|
2009-08-31 16:15:27 +00:00
|
|
|
|
2018-11-17 12:29:15 +00:00
|
|
|
const echo_server = net.createServer((socket) => {
|
2009-08-31 16:15:27 +00:00
|
|
|
socket.setTimeout(timeout);
|
|
|
|
|
2018-11-17 12:29:15 +00:00
|
|
|
socket.on('timeout', () => {
|
2010-12-03 01:03:18 +00:00
|
|
|
console.log('server timeout');
|
2015-05-19 11:00:06 +00:00
|
|
|
timeouttime = new Date();
|
2010-10-11 23:09:02 +00:00
|
|
|
console.dir(timeouttime);
|
2010-05-12 17:01:27 +00:00
|
|
|
socket.destroy();
|
2009-08-31 16:15:27 +00:00
|
|
|
});
|
|
|
|
|
2018-11-17 12:29:15 +00:00
|
|
|
socket.on('error', (e) => {
|
2017-05-12 00:09:13 +00:00
|
|
|
throw new Error(
|
|
|
|
'Server side socket should not get error. We disconnect willingly.');
|
2010-12-05 22:33:52 +00:00
|
|
|
});
|
2010-04-23 00:22:03 +00:00
|
|
|
|
2018-11-17 12:29:15 +00:00
|
|
|
socket.on('data', (d) => {
|
2010-02-16 21:15:30 +00:00
|
|
|
socket.write(d);
|
2009-08-31 16:15:27 +00:00
|
|
|
});
|
|
|
|
|
2018-11-17 12:29:15 +00:00
|
|
|
socket.on('end', () => {
|
2010-04-08 17:44:22 +00:00
|
|
|
socket.end();
|
2009-08-31 16:15:27 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-02-12 03:14:57 +00:00
|
|
|
echo_server.listen(0, () => {
|
|
|
|
const port = echo_server.address().port;
|
|
|
|
console.log(`server listening at ${port}`);
|
2009-08-31 16:15:27 +00:00
|
|
|
|
2020-02-12 03:14:57 +00:00
|
|
|
const client = net.createConnection(port);
|
2010-12-03 01:03:18 +00:00
|
|
|
client.setEncoding('UTF8');
|
2019-03-07 00:03:53 +00:00
|
|
|
client.setTimeout(0); // Disable the timeout for client
|
2018-11-17 12:29:15 +00:00
|
|
|
client.on('connect', () => {
|
2010-12-03 01:03:18 +00:00
|
|
|
console.log('client connected.');
|
|
|
|
client.write('hello\r\n');
|
2010-10-23 21:11:30 +00:00
|
|
|
});
|
2009-08-31 16:15:27 +00:00
|
|
|
|
2018-11-17 12:29:15 +00:00
|
|
|
client.on('data', (chunk) => {
|
2018-10-12 17:19:56 +00:00
|
|
|
assert.strictEqual(chunk, 'hello\r\n');
|
2010-10-23 21:11:30 +00:00
|
|
|
if (exchanges++ < 5) {
|
2018-11-17 12:29:15 +00:00
|
|
|
setTimeout(() => {
|
2010-12-03 01:03:18 +00:00
|
|
|
console.log('client write "hello"');
|
|
|
|
client.write('hello\r\n');
|
2010-10-23 21:11:30 +00:00
|
|
|
}, 500);
|
|
|
|
|
2016-08-17 04:11:22 +00:00
|
|
|
if (exchanges === 5) {
|
2017-04-28 01:06:42 +00:00
|
|
|
console.log(`wait for timeout - should come in ${timeout} ms`);
|
2015-05-19 11:00:06 +00:00
|
|
|
starttime = new Date();
|
2010-10-23 21:11:30 +00:00
|
|
|
console.dir(starttime);
|
|
|
|
}
|
2009-08-31 16:15:27 +00:00
|
|
|
}
|
2010-10-23 21:11:30 +00:00
|
|
|
});
|
2009-08-31 16:15:27 +00:00
|
|
|
|
2018-11-17 12:29:15 +00:00
|
|
|
client.on('timeout', () => {
|
2010-10-23 21:11:30 +00:00
|
|
|
throw new Error("client timeout - this shouldn't happen");
|
|
|
|
});
|
2009-08-31 16:15:27 +00:00
|
|
|
|
2018-11-17 12:29:15 +00:00
|
|
|
client.on('end', () => {
|
2010-12-03 01:03:18 +00:00
|
|
|
console.log('client end');
|
2010-10-23 21:11:30 +00:00
|
|
|
client.end();
|
|
|
|
});
|
2009-08-31 16:15:27 +00:00
|
|
|
|
2018-11-17 12:29:15 +00:00
|
|
|
client.on('close', () => {
|
2010-12-03 01:03:18 +00:00
|
|
|
console.log('client disconnect');
|
2010-10-23 21:11:30 +00:00
|
|
|
echo_server.close();
|
|
|
|
});
|
2009-08-31 16:15:27 +00:00
|
|
|
});
|
|
|
|
|
2018-11-17 12:29:15 +00:00
|
|
|
process.on('exit', () => {
|
2010-03-24 03:54:16 +00:00
|
|
|
assert.ok(starttime != null);
|
|
|
|
assert.ok(timeouttime != null);
|
2009-08-31 16:15:27 +00:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const diff = timeouttime - starttime;
|
2017-04-28 01:06:42 +00:00
|
|
|
console.log(`diff = ${diff}`);
|
2010-03-24 03:54:16 +00:00
|
|
|
|
|
|
|
assert.ok(timeout < diff);
|
2009-08-31 16:15:27 +00:00
|
|
|
});
|