node/test/test-pingpong.js

64 lines
1.1 KiB
JavaScript
Raw Normal View History

2009-04-22 21:52:23 +08:00
include("mjsunit");
var port = 12123;
var N = 100;
var count = 0;
var server;
function Ponger (socket) {
this.encoding = "UTF8";
this.onConnect = function () {
puts("got socket.");
};
this.onReceive = function (data) {
assertTrue(count <= N);
stdout.print ("-");
if (/QUIT/.exec(data)) {
socket.disconnect();
//server.close();
} else if (/PING/.exec(data)) {
socket.send("PONG");
2009-04-22 21:52:23 +08:00
}
};
}
function Pinger (socket) {
this.encoding = "UTF8";
2009-04-22 21:52:23 +08:00
this.onConnect = function () {
socket.send("PING");
};
this.onReceive = function (data) {
stdout.print(".");
2009-04-22 21:52:23 +08:00
assertEquals("PONG", data);
setTimeout(function() {
count += 1;
2009-04-23 07:14:11 +08:00
if (count < N) {
socket.send("PING");
2009-04-22 21:52:23 +08:00
} else {
stdout.write("\n");
socket.send("QUIT\n");
socket.disconnect();
2009-04-22 21:52:23 +08:00
}
2009-04-23 07:14:11 +08:00
}, 10);
2009-04-22 21:52:23 +08:00
};
2009-04-22 22:05:14 +08:00
this.onDisconnect = function () {
2009-04-22 22:04:05 +08:00
puts("socket close.");
2009-04-23 07:14:11 +08:00
assertEquals(N, count);
server.close();
2009-04-22 22:04:05 +08:00
};
}
2009-04-22 21:52:23 +08:00
function onLoad() {
server = new TCPServer(Ponger);
server.listen(port);
2009-04-22 21:52:23 +08:00
var client = new TCPConnection(Pinger);
client.connect(port);
2009-04-22 21:52:23 +08:00
}