node/test/simple/test-tcp-reconnect.js

53 lines
1.2 KiB
JavaScript
Raw Normal View History

2010-02-25 14:36:17 +08:00
process.mixin(require("../common"));
tcp = require("tcp");
var N = 50;
var c = 0;
var client_recv_count = 0;
var disconnect_count = 0;
2009-09-28 22:08:09 +08:00
var server = tcp.createServer(function (socket) {
2009-08-27 00:22:00 +08:00
socket.addListener("connect", function () {
socket.write("hello\r\n");
2009-06-28 02:40:43 +08:00
});
socket.addListener("end", function () {
2009-08-27 00:22:00 +08:00
socket.close();
2009-06-28 02:40:43 +08:00
});
2009-08-27 00:22:00 +08:00
socket.addListener("close", function (had_error) {
//puts("server had_error: " + JSON.stringify(had_error));
assert.equal(false, had_error);
2009-06-28 02:40:43 +08:00
});
2009-08-27 00:22:00 +08:00
});
2010-02-27 04:06:32 +08:00
server.listen(PORT);
2009-08-27 00:22:00 +08:00
2010-02-27 04:06:32 +08:00
var client = tcp.createConnection(PORT);
2009-08-27 00:22:00 +08:00
client.setEncoding("UTF8");
client.addListener("connect", function () {
puts("client connected.");
});
client.addListener("data", function (chunk) {
2009-08-27 00:22:00 +08:00
client_recv_count += 1;
puts("client_recv_count " + client_recv_count);
assert.equal("hello\r\n", chunk);
2009-08-27 00:22:00 +08:00
client.close();
});
client.addListener("close", function (had_error) {
puts("disconnect");
assert.equal(false, had_error);
2009-08-27 04:03:19 +08:00
if (disconnect_count++ < N)
2010-02-27 04:06:32 +08:00
client.connect(PORT); // reconnect
2009-08-27 00:22:00 +08:00
else
server.close();
});
process.addListener("exit", function () {
assert.equal(N+1, disconnect_count);
assert.equal(N+1, client_recv_count);
});