2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2010-12-05 00:20:34 +01:00
|
|
|
var common = require('../common');
|
|
|
|
var assert = require('assert');
|
2010-12-04 23:45:52 +01:00
|
|
|
|
2010-12-05 00:20:34 +01:00
|
|
|
var net = require('net');
|
2009-05-19 21:53:26 +02:00
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
var N = 50;
|
2009-08-14 12:51:46 +02:00
|
|
|
var c = 0;
|
2009-06-08 19:10:23 +02:00
|
|
|
var client_recv_count = 0;
|
2014-01-22 22:15:04 +01:00
|
|
|
var client_end_count = 0;
|
2009-08-14 12:51:46 +02:00
|
|
|
var disconnect_count = 0;
|
2009-06-08 19:10:23 +02:00
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
var server = net.createServer(function(socket) {
|
2012-12-13 18:51:31 +01:00
|
|
|
console.error('SERVER: got socket connection');
|
|
|
|
socket.resume();
|
|
|
|
|
2013-02-28 23:25:29 +01:00
|
|
|
console.error('SERVER connect, writing');
|
|
|
|
socket.write('hello\r\n');
|
2009-05-19 21:53:26 +02:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
socket.on('end', function() {
|
2012-12-13 18:51:31 +01:00
|
|
|
console.error('SERVER socket end, calling end()');
|
2010-04-08 19:44:22 +02:00
|
|
|
socket.end();
|
2009-06-27 20:40:43 +02:00
|
|
|
});
|
2009-05-19 21:53:26 +02:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
socket.on('close', function(had_error) {
|
2012-12-13 18:51:31 +01:00
|
|
|
console.log('SERVER had_error: ' + JSON.stringify(had_error));
|
2009-11-28 18:26:59 +01:00
|
|
|
assert.equal(false, had_error);
|
2009-06-27 20:40:43 +02:00
|
|
|
});
|
2009-08-26 18:22:00 +02:00
|
|
|
});
|
2010-03-10 01:27:49 +01:00
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
server.listen(common.PORT, function() {
|
2012-12-13 18:51:31 +01:00
|
|
|
console.log('SERVER listening');
|
2010-07-15 20:47:25 +02:00
|
|
|
var client = net.createConnection(common.PORT);
|
2009-08-26 18:22:00 +02:00
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
client.setEncoding('UTF8');
|
2009-08-26 18:22:00 +02:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
client.on('connect', function() {
|
2012-12-13 18:51:31 +01:00
|
|
|
console.error('CLIENT connected', client._writableState);
|
2010-03-10 01:27:49 +01:00
|
|
|
});
|
2009-08-26 18:22:00 +02:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
client.on('data', function(chunk) {
|
2010-03-10 01:27:49 +01:00
|
|
|
client_recv_count += 1;
|
2010-12-04 23:45:52 +01:00
|
|
|
console.log('client_recv_count ' + client_recv_count);
|
|
|
|
assert.equal('hello\r\n', chunk);
|
2012-12-13 18:51:31 +01:00
|
|
|
console.error('CLIENT: calling end', client._writableState);
|
2010-04-08 19:44:22 +02:00
|
|
|
client.end();
|
2010-03-10 01:27:49 +01:00
|
|
|
});
|
2009-08-26 18:22:00 +02:00
|
|
|
|
2012-12-13 18:51:31 +01:00
|
|
|
client.on('end', function() {
|
|
|
|
console.error('CLIENT end');
|
2014-01-22 22:15:04 +01:00
|
|
|
client_end_count++;
|
2012-12-13 18:51:31 +01:00
|
|
|
});
|
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
client.on('close', function(had_error) {
|
2012-12-13 18:51:31 +01:00
|
|
|
console.log('CLIENT disconnect');
|
2010-03-10 01:27:49 +01:00
|
|
|
assert.equal(false, had_error);
|
|
|
|
if (disconnect_count++ < N)
|
2010-07-15 20:47:25 +02:00
|
|
|
client.connect(common.PORT); // reconnect
|
2010-03-10 01:27:49 +01:00
|
|
|
else
|
|
|
|
server.close();
|
|
|
|
});
|
2009-08-26 18:22:00 +02:00
|
|
|
});
|
2009-06-08 19:10:23 +02:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
process.on('exit', function() {
|
2010-12-05 20:15:30 +01:00
|
|
|
assert.equal(N + 1, disconnect_count);
|
|
|
|
assert.equal(N + 1, client_recv_count);
|
2014-01-22 22:15:04 +01:00
|
|
|
assert.equal(N + 1, client_end_count);
|
2009-08-26 18:51:04 +02:00
|
|
|
});
|
2010-03-10 01:27:49 +01:00
|
|
|
|