node/test/test-pingpong.js

53 lines
1.1 KiB
JavaScript
Raw Normal View History

2009-04-22 21:52:23 +08:00
include("mjsunit");
2009-04-23 07:14:11 +08:00
var N = 1000;
2009-04-22 21:52:23 +08:00
function onLoad() {
server = new Server(1024);
2009-04-22 22:04:05 +08:00
var count = 0;
2009-04-22 21:52:23 +08:00
server.listenTCP(12123, function (connection) {
puts("got connection.");
connection.onRead = function (data) {
2009-04-23 07:14:11 +08:00
assertTrue(count <= N);
2009-04-22 21:52:23 +08:00
if (data === null) {
server.close();
connection.close();
return;
}
stdout.print ("-");
2009-04-22 21:52:23 +08:00
if (/QUIT/.exec(data)) {
server.close();
connection.close();
} else if (/PING/.exec(data)) {
connection.write("PONG");
}
}
});
socket = new Socket;
socket.onRead = 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) {
2009-04-22 21:52:23 +08:00
socket.write("PING");
} else {
2009-04-22 22:04:05 +08:00
stdout.write ("\n");
2009-04-22 21:52:23 +08:00
socket.write("QUIT\n");
socket.close();
}
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
2009-04-22 21:52:23 +08:00
socket.onClose = function () {
2009-04-22 22:04:05 +08:00
puts("socket close.");
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
socket.connectTCP(12123, "127.0.0.1", function (status) {
2009-04-22 21:52:23 +08:00
if(status != 0)
process.exit(1);
socket.write("PING");
});
}