2010-02-25 14:36:17 +08:00
|
|
|
process.mixin(require("../common"));
|
2009-11-01 02:02:30 +08:00
|
|
|
tcp = require("tcp");
|
2009-05-02 22:34:24 +08:00
|
|
|
|
2009-06-18 20:34:49 +08:00
|
|
|
var tests_run = 0;
|
|
|
|
|
|
|
|
function pingPongTest (port, host, on_complete) {
|
|
|
|
var N = 1000;
|
|
|
|
var count = 0;
|
|
|
|
var sent_final_ping = false;
|
|
|
|
|
2009-09-28 22:08:09 +08:00
|
|
|
var server = tcp.createServer(function (socket) {
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal(true, socket.remoteAddress !== null);
|
|
|
|
assert.equal(true, socket.remoteAddress !== undefined);
|
2009-06-18 20:34:49 +08:00
|
|
|
if (host === "127.0.0.1")
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal(socket.remoteAddress, "127.0.0.1");
|
2009-06-18 20:34:49 +08:00
|
|
|
else if (host == null)
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal(socket.remoteAddress, "127.0.0.1");
|
2009-06-18 20:34:49 +08:00
|
|
|
|
|
|
|
socket.setEncoding("utf8");
|
2009-09-23 21:35:03 +08:00
|
|
|
socket.setNoDelay();
|
2009-06-18 20:34:49 +08:00
|
|
|
socket.timeout = 0;
|
|
|
|
|
2010-02-12 16:25:15 +08:00
|
|
|
socket.addListener("data", function (data) {
|
2009-09-23 21:35:03 +08:00
|
|
|
puts("server got: " + JSON.stringify(data));
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal("open", socket.readyState);
|
|
|
|
assert.equal(true, count <= N);
|
2009-06-18 20:34:49 +08:00
|
|
|
if (/PING/.exec(data)) {
|
2010-02-17 05:15:30 +08:00
|
|
|
socket.write("PONG");
|
2009-06-18 20:34:49 +08:00
|
|
|
}
|
2009-06-28 02:40:43 +08:00
|
|
|
});
|
2009-06-18 20:34:49 +08:00
|
|
|
|
2010-02-12 16:25:15 +08:00
|
|
|
socket.addListener("end", function () {
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal("writeOnly", socket.readyState);
|
2009-06-18 20:34:49 +08:00
|
|
|
socket.close();
|
2009-06-28 02:40:43 +08:00
|
|
|
});
|
2009-06-18 20:34:49 +08:00
|
|
|
|
2009-08-14 18:51:46 +08:00
|
|
|
socket.addListener("close", function (had_error) {
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal(false, had_error);
|
|
|
|
assert.equal("closed", socket.readyState);
|
2009-06-18 20:34:49 +08:00
|
|
|
socket.server.close();
|
2009-06-28 02:40:43 +08:00
|
|
|
});
|
2009-06-18 20:34:49 +08:00
|
|
|
});
|
|
|
|
server.listen(port, host);
|
2009-05-15 05:47:21 +08:00
|
|
|
|
2009-09-28 22:08:09 +08:00
|
|
|
var client = tcp.createConnection(port, host);
|
2009-05-15 05:47:21 +08:00
|
|
|
|
2009-05-20 16:02:02 +08:00
|
|
|
client.setEncoding("utf8");
|
2009-04-22 21:52:23 +08:00
|
|
|
|
2009-06-29 19:18:30 +08:00
|
|
|
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-06-28 02:40:43 +08:00
|
|
|
});
|
2009-05-02 22:34:24 +08:00
|
|
|
|
2010-02-12 16:25:15 +08:00
|
|
|
client.addListener("data", function (data) {
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal("PONG", data);
|
2009-08-27 04:03:19 +08:00
|
|
|
count += 1;
|
2009-05-20 02:24:37 +08:00
|
|
|
|
|
|
|
if (sent_final_ping) {
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal("readOnly", client.readyState);
|
2009-05-20 02:24:37 +08:00
|
|
|
return;
|
|
|
|
} else {
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal("open", client.readyState);
|
2009-05-20 02:24:37 +08:00
|
|
|
}
|
|
|
|
|
2009-05-03 07:01:42 +08:00
|
|
|
if (count < N) {
|
2010-02-17 05:15:30 +08:00
|
|
|
client.write("PING");
|
2009-05-03 07:01:42 +08:00
|
|
|
} else {
|
2009-05-20 02:24:37 +08:00
|
|
|
sent_final_ping = true;
|
2010-02-17 05:15:30 +08:00
|
|
|
client.write("PING");
|
2009-05-15 05:47:21 +08:00
|
|
|
client.close();
|
2009-05-03 07:01:42 +08:00
|
|
|
}
|
2009-06-28 02:40:43 +08:00
|
|
|
});
|
2009-06-18 20:34:49 +08:00
|
|
|
|
2009-08-14 18:51:46 +08:00
|
|
|
client.addListener("close", function () {
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal(N+1, count);
|
|
|
|
assert.equal(true, sent_final_ping);
|
2009-06-18 20:34:49 +08:00
|
|
|
if (on_complete) on_complete();
|
|
|
|
tests_run += 1;
|
2009-06-28 02:40:43 +08:00
|
|
|
});
|
2009-06-18 20:34:49 +08:00
|
|
|
}
|
|
|
|
|
2009-08-27 00:22:00 +08:00
|
|
|
/* All are run at once, so run on different ports */
|
2010-02-27 04:06:32 +08:00
|
|
|
pingPongTest(PORT, "localhost");
|
|
|
|
pingPongTest(PORT+1, null);
|
2010-03-03 05:10:05 +08:00
|
|
|
|
|
|
|
// This IPv6 isn't working on Solaris
|
|
|
|
var solaris = /sunos/i.test(process.platform);
|
|
|
|
if (!solaris) pingPongTest(PORT+2, "::1");
|
2009-06-09 01:10:23 +08:00
|
|
|
|
2009-08-27 00:51:04 +08:00
|
|
|
process.addListener("exit", function () {
|
2010-03-03 05:10:05 +08:00
|
|
|
assert.equal(solaris ? 2 : 3, tests_run);
|
2009-08-27 00:51:04 +08:00
|
|
|
});
|