node/test/test-pingpong.js

52 lines
1.1 KiB
JavaScript
Raw Normal View History

2009-04-22 21:52:23 +08:00
include("mjsunit");
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-22 22:04:05 +08:00
assertTrue(count <= 10);
2009-04-22 21:52:23 +08:00
if (data === null) {
server.close();
connection.close();
return;
}
2009-04-22 22:04:05 +08:00
stdout.write ("-");
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) {
2009-04-22 22:04:05 +08:00
stdout.write (".");
2009-04-22 21:52:23 +08:00
assertEquals("PONG", data);
setTimeout(function() {
count += 1;
if (count < 10) {
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();
}
}, 100);
};
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-22 21:52:23 +08:00
assertEquals(10, count);
2009-04-22 22:04:05 +08:00
};
2009-04-22 21:52:23 +08:00
socket.connectTCP(12123, "localhost", function (status) {
if(status != 0)
process.exit(1);
socket.write("PING");
});
}