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