2010-02-25 14:36:17 +08:00
|
|
|
process.mixin(require("../common"));
|
2009-11-01 02:02:30 +08:00
|
|
|
tcp = require("tcp");
|
2009-07-15 13:54:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
var tests_run = 0;
|
|
|
|
|
|
|
|
function pingPongTest (port, host, on_complete) {
|
|
|
|
var N = 100;
|
|
|
|
var DELAY = 1;
|
|
|
|
var count = 0;
|
|
|
|
var client_closed = false;
|
|
|
|
|
2009-09-28 22:08:09 +08:00
|
|
|
var server = tcp.createServer(function (socket) {
|
2009-07-15 13:54:01 +08:00
|
|
|
socket.setEncoding("utf8");
|
|
|
|
|
2010-02-12 16:25:15 +08:00
|
|
|
socket.addListener("data", function (data) {
|
2009-07-15 13:54:01 +08:00
|
|
|
puts(data);
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal("PING", data);
|
|
|
|
assert.equal("open", socket.readyState);
|
|
|
|
assert.equal(true, count <= N);
|
2009-07-15 13:54:01 +08:00
|
|
|
setTimeout(function () {
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal("open", socket.readyState);
|
2010-02-17 05:15:30 +08:00
|
|
|
socket.write("PONG");
|
2009-07-15 13:54:01 +08:00
|
|
|
}, DELAY);
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.addListener("timeout", function () {
|
2009-09-28 18:06:30 +08:00
|
|
|
debug("server-side timeout!!");
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal(false, true);
|
2009-07-15 13:54:01 +08:00
|
|
|
});
|
|
|
|
|
2010-02-12 16:25:15 +08:00
|
|
|
socket.addListener("end", function () {
|
2009-07-15 13:54:01 +08:00
|
|
|
puts("server-side socket EOF");
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal("writeOnly", socket.readyState);
|
2009-07-15 13:54:01 +08:00
|
|
|
socket.close();
|
|
|
|
});
|
|
|
|
|
2009-08-14 18:51:46 +08:00
|
|
|
socket.addListener("close", function (had_error) {
|
|
|
|
puts("server-side socket close");
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal(false, had_error);
|
|
|
|
assert.equal("closed", socket.readyState);
|
2009-07-15 13:54:01 +08:00
|
|
|
socket.server.close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
server.listen(port, host);
|
|
|
|
|
2009-09-28 22:08:09 +08:00
|
|
|
var client = tcp.createConnection(port, host);
|
2009-07-15 13:54:01 +08:00
|
|
|
|
|
|
|
client.setEncoding("utf8");
|
|
|
|
|
|
|
|
client.addListener("connect", function () {
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal("open", client.readyState);
|
2010-02-17 05:15:30 +08:00
|
|
|
client.write("PING");
|
2009-07-15 13:54:01 +08:00
|
|
|
});
|
|
|
|
|
2010-02-12 16:25:15 +08:00
|
|
|
client.addListener("data", function (data) {
|
2009-07-15 13:54:01 +08:00
|
|
|
puts(data);
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal("PONG", data);
|
|
|
|
assert.equal("open", client.readyState);
|
2009-07-15 13:54:01 +08:00
|
|
|
|
|
|
|
setTimeout(function () {
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal("open", client.readyState);
|
2009-07-15 13:54:01 +08:00
|
|
|
if (count++ < N) {
|
2010-02-17 05:15:30 +08:00
|
|
|
client.write("PING");
|
2009-07-15 13:54:01 +08:00
|
|
|
} else {
|
|
|
|
puts("closing client");
|
|
|
|
client.close();
|
|
|
|
client_closed = true;
|
|
|
|
}
|
|
|
|
}, DELAY);
|
|
|
|
});
|
|
|
|
|
|
|
|
client.addListener("timeout", function () {
|
2009-09-28 18:06:30 +08:00
|
|
|
debug("client-side timeout!!");
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal(false, true);
|
2009-07-15 13:54:01 +08:00
|
|
|
});
|
|
|
|
|
2009-08-14 18:51:46 +08:00
|
|
|
client.addListener("close", function () {
|
|
|
|
puts("client close");
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal(N+1, count);
|
|
|
|
assert.equal(true, client_closed);
|
2009-07-15 13:54:01 +08:00
|
|
|
if (on_complete) on_complete();
|
|
|
|
tests_run += 1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2010-02-27 04:06:32 +08:00
|
|
|
pingPongTest(PORT);
|
2009-07-15 13:54:01 +08:00
|
|
|
|
2009-08-27 00:51:04 +08:00
|
|
|
process.addListener("exit", function () {
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal(1, tests_run);
|
2009-08-27 00:51:04 +08:00
|
|
|
});
|