node/test/test-pingpong.js

67 lines
1.1 KiB
JavaScript
Raw Normal View History

2009-04-22 21:52:23 +08:00
include("mjsunit");
var port = 12123;
var N = 1000;
var count = 0;
function Ponger (socket, server) {
this.encoding = "UTF8";
this.timeout = 0;
this.onConnect = function () {
puts("got socket.");
};
this.onReceive = function (data) {
assertTrue(count <= N);
stdout.print("-");
if (/PING/.exec(data)) {
socket.send("PONG");
2009-04-22 21:52:23 +08:00
}
};
this.onEOF = function () {
puts("ponger: onEOF");
socket.send("QUIT");
socket.close();
};
this.onDisconnect = function () {
puts("ponger: onDisconnect");
server.close();
};
}
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);
count += 1;
if (count < N) {
socket.send("PING");
} else {
puts("sending FIN");
socket.sendEOF();
}
2009-04-22 21:52:23 +08:00
};
2009-04-22 22:05:14 +08:00
this.onEOF = function () {
puts("pinger: onEOF");
2009-04-23 07:14:11 +08:00
assertEquals(N, count);
2009-04-22 22:04:05 +08:00
};
}
2009-04-22 21:52:23 +08:00
function onLoad() {
var 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
}