0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-30 15:30:56 +01:00
nodejs/test/mjsunit/test-tcp-reconnect.js

54 lines
1.2 KiB
JavaScript
Raw Normal View History

include("common.js");
2009-09-28 16:08:09 +02:00
tcp = require("/tcp.js");
var N = 50;
var port = 8921;
var c = 0;
var client_recv_count = 0;
var disconnect_count = 0;
2009-09-28 16:08:09 +02:00
var server = tcp.createServer(function (socket) {
2009-08-26 18:22:00 +02:00
socket.addListener("connect", function () {
socket.send("hello\r\n");
2009-06-27 20:40:43 +02:00
});
2009-08-26 18:22:00 +02:00
socket.addListener("eof", function () {
socket.close();
2009-06-27 20:40:43 +02:00
});
2009-08-26 18:22:00 +02:00
socket.addListener("close", function (had_error) {
//puts("server had_error: " + JSON.stringify(had_error));
assertFalse(had_error);
2009-06-27 20:40:43 +02:00
});
2009-08-26 18:22:00 +02:00
});
server.listen(port);
2009-09-28 16:08:09 +02:00
var client = tcp.createConnection(port);
2009-08-26 18:22:00 +02:00
client.setEncoding("UTF8");
client.addListener("connect", function () {
puts("client connected.");
});
client.addListener("receive", function (chunk) {
client_recv_count += 1;
puts("client_recv_count " + client_recv_count);
assertEquals("hello\r\n", chunk);
client.close();
});
client.addListener("close", function (had_error) {
puts("disconnect");
assertFalse(had_error);
2009-08-26 22:03:19 +02:00
if (disconnect_count++ < N)
2009-08-26 18:22:00 +02:00
client.connect(port); // reconnect
else
server.close();
});
process.addListener("exit", function () {
assertEquals(N+1, disconnect_count);
assertEquals(N+1, client_recv_count);
});