2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2010-12-05 00:20:34 +01:00
|
|
|
var common = require('../common');
|
2010-12-05 23:33:52 +01:00
|
|
|
var assert = require('assert');
|
2010-12-05 01:11:57 +01:00
|
|
|
var net = require('net');
|
|
|
|
|
|
|
|
var exchanges = 0;
|
|
|
|
var starttime = null;
|
|
|
|
var timeouttime = null;
|
|
|
|
var timeout = 1000;
|
2009-08-31 18:15:27 +02:00
|
|
|
|
2010-12-03 02:03:18 +01:00
|
|
|
var echo_server = net.createServer(function(socket) {
|
2009-08-31 18:15:27 +02:00
|
|
|
socket.setTimeout(timeout);
|
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
socket.on('timeout', function() {
|
2010-12-03 02:03:18 +01:00
|
|
|
console.log('server timeout');
|
2015-05-19 13:00:06 +02:00
|
|
|
timeouttime = new Date();
|
2010-10-12 01:09:02 +02:00
|
|
|
console.dir(timeouttime);
|
2010-05-12 19:01:27 +02:00
|
|
|
socket.destroy();
|
2009-08-31 18:15:27 +02:00
|
|
|
});
|
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
socket.on('error', function(e) {
|
2010-12-05 23:33:52 +01:00
|
|
|
throw new Error('Server side socket should not get error. ' +
|
|
|
|
'We disconnect willingly.');
|
|
|
|
});
|
2010-04-23 02:22:03 +02:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
socket.on('data', function(d) {
|
2010-06-24 02:40:51 +02:00
|
|
|
console.log(d);
|
2010-02-16 22:15:30 +01:00
|
|
|
socket.write(d);
|
2009-08-31 18:15:27 +02:00
|
|
|
});
|
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
socket.on('end', function() {
|
2010-04-08 19:44:22 +02:00
|
|
|
socket.end();
|
2009-08-31 18:15:27 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2010-12-03 02:03:18 +01:00
|
|
|
echo_server.listen(common.PORT, function() {
|
|
|
|
console.log('server listening at ' + common.PORT);
|
2009-08-31 18:15:27 +02:00
|
|
|
|
2010-10-23 23:11:30 +02:00
|
|
|
var client = net.createConnection(common.PORT);
|
2010-12-03 02:03:18 +01:00
|
|
|
client.setEncoding('UTF8');
|
2010-10-23 23:11:30 +02:00
|
|
|
client.setTimeout(0); // disable the timeout for client
|
2011-10-15 01:08:36 +02:00
|
|
|
client.on('connect', function() {
|
2010-12-03 02:03:18 +01:00
|
|
|
console.log('client connected.');
|
|
|
|
client.write('hello\r\n');
|
2010-10-23 23:11:30 +02:00
|
|
|
});
|
2009-08-31 18:15:27 +02:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
client.on('data', function(chunk) {
|
2010-12-03 02:03:18 +01:00
|
|
|
assert.equal('hello\r\n', chunk);
|
2010-10-23 23:11:30 +02:00
|
|
|
if (exchanges++ < 5) {
|
2010-12-03 02:03:18 +01:00
|
|
|
setTimeout(function() {
|
|
|
|
console.log('client write "hello"');
|
|
|
|
client.write('hello\r\n');
|
2010-10-23 23:11:30 +02:00
|
|
|
}, 500);
|
|
|
|
|
2016-08-17 06:11:22 +02:00
|
|
|
if (exchanges === 5) {
|
2010-12-03 02:03:18 +01:00
|
|
|
console.log('wait for timeout - should come in ' + timeout + ' ms');
|
2015-05-19 13:00:06 +02:00
|
|
|
starttime = new Date();
|
2010-10-23 23:11:30 +02:00
|
|
|
console.dir(starttime);
|
|
|
|
}
|
2009-08-31 18:15:27 +02:00
|
|
|
}
|
2010-10-23 23:11:30 +02:00
|
|
|
});
|
2009-08-31 18:15:27 +02:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
client.on('timeout', function() {
|
2010-10-23 23:11:30 +02:00
|
|
|
throw new Error("client timeout - this shouldn't happen");
|
|
|
|
});
|
2009-08-31 18:15:27 +02:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
client.on('end', function() {
|
2010-12-03 02:03:18 +01:00
|
|
|
console.log('client end');
|
2010-10-23 23:11:30 +02:00
|
|
|
client.end();
|
|
|
|
});
|
2009-08-31 18:15:27 +02:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
client.on('close', function() {
|
2010-12-03 02:03:18 +01:00
|
|
|
console.log('client disconnect');
|
2010-10-23 23:11:30 +02:00
|
|
|
echo_server.close();
|
|
|
|
});
|
2009-08-31 18:15:27 +02:00
|
|
|
});
|
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
process.on('exit', function() {
|
2010-03-24 04:54:16 +01:00
|
|
|
assert.ok(starttime != null);
|
|
|
|
assert.ok(timeouttime != null);
|
2009-08-31 18:15:27 +02:00
|
|
|
|
2015-05-29 19:28:01 +02:00
|
|
|
var diff = timeouttime - starttime;
|
2010-12-03 02:03:18 +01:00
|
|
|
console.log('diff = ' + diff);
|
2010-03-24 04:54:16 +01:00
|
|
|
|
|
|
|
assert.ok(timeout < diff);
|
|
|
|
|
2009-08-31 18:15:27 +02:00
|
|
|
// Allow for 800 milliseconds more
|
2010-03-24 04:54:16 +01:00
|
|
|
assert.ok(diff < timeout + 800);
|
2009-08-31 18:15:27 +02:00
|
|
|
});
|