0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-net-reconnect.js

70 lines
1.6 KiB
JavaScript
Raw Normal View History

'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');
2010-12-04 23:45:52 +01:00
var N = 50;
var c = 0;
var client_recv_count = 0;
var client_end_count = 0;
var disconnect_count = 0;
2010-12-04 23:45:52 +01:00
var server = net.createServer(function(socket) {
console.error('SERVER: got socket connection');
socket.resume();
console.error('SERVER connect, writing');
socket.write('hello\r\n');
socket.on('end', function() {
console.error('SERVER socket end, calling end()');
socket.end();
2009-06-27 20:40:43 +02:00
});
socket.on('close', function(had_error) {
console.log('SERVER had_error: ' + JSON.stringify(had_error));
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() {
console.log('SERVER listening');
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
client.on('connect', function() {
console.error('CLIENT connected', client._writableState);
2010-03-10 01:27:49 +01:00
});
2009-08-26 18:22:00 +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);
console.error('CLIENT: calling end', client._writableState);
client.end();
2010-03-10 01:27:49 +01:00
});
2009-08-26 18:22:00 +02:00
client.on('end', function() {
console.error('CLIENT end');
client_end_count++;
});
client.on('close', function(had_error) {
console.log('CLIENT disconnect');
2010-03-10 01:27:49 +01:00
assert.equal(false, had_error);
if (disconnect_count++ < N)
client.connect(common.PORT); // reconnect
2010-03-10 01:27:49 +01:00
else
server.close();
});
2009-08-26 18:22:00 +02:00
});
process.on('exit', function() {
assert.equal(N + 1, disconnect_count);
assert.equal(N + 1, client_recv_count);
assert.equal(N + 1, client_end_count);
});
2010-03-10 01:27:49 +01:00