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';
|
2016-12-30 23:38:06 +00:00
|
|
|
const common = require('../common');
|
|
|
|
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
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const echo_server = net.createServer(function(socket) {
|
2009-08-31 16:15:27 +00:00
|
|
|
socket.setTimeout(timeout);
|
|
|
|
|
2011-10-14 23:08:36 +00:00
|
|
|
socket.on('timeout', function() {
|
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
|
|
|
});
|
|
|
|
|
2011-10-14 23:08:36 +00:00
|
|
|
socket.on('error', function(e) {
|
2010-12-05 22:33:52 +00:00
|
|
|
throw new Error('Server side socket should not get error. ' +
|
|
|
|
'We disconnect willingly.');
|
|
|
|
});
|
2010-04-23 00:22:03 +00:00
|
|
|
|
2011-10-14 23:08:36 +00:00
|
|
|
socket.on('data', function(d) {
|
2010-06-24 00:40:51 +00:00
|
|
|
console.log(d);
|
2010-02-16 21:15:30 +00:00
|
|
|
socket.write(d);
|
2009-08-31 16:15:27 +00:00
|
|
|
});
|
|
|
|
|
2011-10-14 23:08:36 +00:00
|
|
|
socket.on('end', function() {
|
2010-04-08 17:44:22 +00:00
|
|
|
socket.end();
|
2009-08-31 16:15:27 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2010-12-03 01:03:18 +00:00
|
|
|
echo_server.listen(common.PORT, function() {
|
|
|
|
console.log('server listening at ' + common.PORT);
|
2009-08-31 16:15:27 +00:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const client = net.createConnection(common.PORT);
|
2010-12-03 01:03:18 +00:00
|
|
|
client.setEncoding('UTF8');
|
2010-10-23 21:11:30 +00:00
|
|
|
client.setTimeout(0); // disable the timeout for client
|
2011-10-14 23:08:36 +00:00
|
|
|
client.on('connect', function() {
|
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
|
|
|
|
2011-10-14 23:08:36 +00:00
|
|
|
client.on('data', function(chunk) {
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual('hello\r\n', chunk);
|
2010-10-23 21:11:30 +00:00
|
|
|
if (exchanges++ < 5) {
|
2010-12-03 01:03:18 +00:00
|
|
|
setTimeout(function() {
|
|
|
|
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) {
|
2010-12-03 01:03:18 +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
|
|
|
|
2011-10-14 23:08:36 +00:00
|
|
|
client.on('timeout', function() {
|
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
|
|
|
|
2011-10-14 23:08:36 +00:00
|
|
|
client.on('end', function() {
|
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
|
|
|
|
2011-10-14 23:08:36 +00:00
|
|
|
client.on('close', function() {
|
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
|
|
|
});
|
|
|
|
|
2011-10-14 23:08:36 +00:00
|
|
|
process.on('exit', function() {
|
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;
|
2010-12-03 01:03:18 +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
|
|
|
// Allow for 800 milliseconds more
|
2010-03-24 03:54:16 +00:00
|
|
|
assert.ok(diff < timeout + 800);
|
2009-08-31 16:15:27 +00:00
|
|
|
});
|