2009-06-08 21:30:10 +08:00
|
|
|
include("mjsunit.js");
|
2009-06-09 01:10:23 +08:00
|
|
|
var N = 50;
|
2009-05-20 03:53:26 +08:00
|
|
|
var port = 8921;
|
|
|
|
|
2009-08-14 18:51:46 +08:00
|
|
|
var c = 0;
|
2009-06-09 01:10:23 +08:00
|
|
|
var client_recv_count = 0;
|
2009-08-14 18:51:46 +08:00
|
|
|
var disconnect_count = 0;
|
2009-06-09 01:10:23 +08:00
|
|
|
|
2009-05-20 03:53:26 +08:00
|
|
|
function onLoad () {
|
|
|
|
|
2009-06-26 18:51:27 +08:00
|
|
|
var server = node.tcp.createServer(function (socket) {
|
2009-06-29 19:18:30 +08:00
|
|
|
socket.addListener("connect", function () {
|
2009-05-20 03:53:26 +08:00
|
|
|
socket.send("hello\r\n");
|
2009-06-28 02:40:43 +08:00
|
|
|
});
|
2009-05-20 03:53:26 +08:00
|
|
|
|
2009-06-29 19:18:30 +08:00
|
|
|
socket.addListener("eof", function () {
|
2009-05-20 03:53:26 +08:00
|
|
|
socket.close();
|
2009-06-28 02:40:43 +08:00
|
|
|
});
|
2009-05-20 03:53:26 +08:00
|
|
|
|
2009-08-14 18:51:46 +08:00
|
|
|
socket.addListener("close", function (had_error) {
|
2009-06-04 18:31:45 +08:00
|
|
|
//puts("server had_error: " + JSON.stringify(had_error));
|
|
|
|
assertFalse(had_error);
|
2009-06-28 02:40:43 +08:00
|
|
|
});
|
2009-06-04 18:31:45 +08:00
|
|
|
});
|
|
|
|
server.listen(port);
|
2009-06-30 19:56:52 +08:00
|
|
|
|
|
|
|
var client = node.tcp.createConnection(port);
|
2009-05-20 03:53:26 +08:00
|
|
|
|
2009-05-20 16:02:02 +08:00
|
|
|
client.setEncoding("UTF8");
|
2009-06-30 19:56:52 +08:00
|
|
|
|
2009-06-29 19:18:30 +08:00
|
|
|
client.addListener("connect", function () {
|
2009-06-30 19:56:52 +08:00
|
|
|
puts("client connected.");
|
2009-06-28 02:40:43 +08:00
|
|
|
});
|
2009-05-20 03:53:26 +08:00
|
|
|
|
2009-06-29 19:18:30 +08:00
|
|
|
client.addListener("receive", function (chunk) {
|
2009-06-09 01:10:23 +08:00
|
|
|
client_recv_count += 1;
|
2009-07-12 21:02:13 +08:00
|
|
|
puts("client_recv_count " + client_recv_count);
|
2009-05-20 03:53:26 +08:00
|
|
|
assertEquals("hello\r\n", chunk);
|
|
|
|
client.fullClose();
|
2009-06-28 02:40:43 +08:00
|
|
|
});
|
2009-05-20 03:53:26 +08:00
|
|
|
|
2009-08-14 18:51:46 +08:00
|
|
|
client.addListener("close", function (had_error) {
|
2009-07-12 21:02:13 +08:00
|
|
|
puts("disconnect");
|
2009-06-04 18:31:45 +08:00
|
|
|
assertFalse(had_error);
|
2009-06-09 01:10:23 +08:00
|
|
|
if (disconnect_count++ < N)
|
2009-06-04 18:31:45 +08:00
|
|
|
client.connect(port); // reconnect
|
|
|
|
else
|
|
|
|
server.close();
|
2009-06-28 02:40:43 +08:00
|
|
|
});
|
2009-05-20 03:53:26 +08:00
|
|
|
}
|
2009-06-09 01:10:23 +08:00
|
|
|
|
|
|
|
function onExit () {
|
|
|
|
assertEquals(N+1, disconnect_count);
|
|
|
|
assertEquals(N+1, client_recv_count);
|
|
|
|
}
|