2010-12-05 00:20:34 +01:00
|
|
|
var common = require('../common');
|
2010-12-05 23:33:52 +01:00
|
|
|
var assert = require('assert');
|
2010-12-05 01:11:57 +01:00
|
|
|
var net = require('net');
|
2009-07-15 07:54:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
var tests_run = 0;
|
|
|
|
|
2010-12-03 02:03:18 +01:00
|
|
|
function pingPongTest(port, host, on_complete) {
|
2009-07-15 07:54:01 +02:00
|
|
|
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-12-03 02:03:18 +01:00
|
|
|
var server = net.createServer({ allowHalfOpen: true }, function(socket) {
|
|
|
|
socket.setEncoding('utf8');
|
2009-07-15 07:54:01 +02:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
socket.on('data', function(data) {
|
2010-06-24 02:40:51 +02:00
|
|
|
console.log(data);
|
2010-12-03 02:03:18 +01:00
|
|
|
assert.equal('PING', data);
|
|
|
|
assert.equal('open', socket.readyState);
|
2009-11-28 18:26:59 +01:00
|
|
|
assert.equal(true, count <= N);
|
2010-12-03 02:03:18 +01:00
|
|
|
setTimeout(function() {
|
|
|
|
assert.equal('open', socket.readyState);
|
|
|
|
socket.write('PONG');
|
2009-07-15 07:54:01 +02:00
|
|
|
}, DELAY);
|
|
|
|
});
|
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
socket.on('timeout', function() {
|
2010-12-03 02:03:18 +01:00
|
|
|
common.debug('server-side timeout!!');
|
2009-11-28 18:26:59 +01:00
|
|
|
assert.equal(false, true);
|
2009-07-15 07:54:01 +02:00
|
|
|
});
|
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
socket.on('end', function() {
|
2010-12-03 02:03:18 +01:00
|
|
|
console.log('server-side socket EOF');
|
|
|
|
assert.equal('writeOnly', socket.readyState);
|
2010-04-08 19:44:22 +02:00
|
|
|
socket.end();
|
2009-07-15 07:54:01 +02:00
|
|
|
});
|
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
socket.on('close', function(had_error) {
|
2010-12-03 02:03:18 +01:00
|
|
|
console.log('server-side socket.end');
|
2009-11-28 18:26:59 +01:00
|
|
|
assert.equal(false, had_error);
|
2010-12-03 02:03:18 +01:00
|
|
|
assert.equal('closed', socket.readyState);
|
2009-07-15 07:54:01 +02:00
|
|
|
socket.server.close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2010-12-03 02:03:18 +01:00
|
|
|
server.listen(port, host, function() {
|
2010-08-13 17:54:40 +02:00
|
|
|
var client = net.createConnection(port, host);
|
2009-07-15 07:54:01 +02:00
|
|
|
|
2010-12-03 02:03:18 +01:00
|
|
|
client.setEncoding('utf8');
|
2009-07-15 07:54:01 +02:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
client.on('connect', function() {
|
2010-12-03 02:03:18 +01:00
|
|
|
assert.equal('open', client.readyState);
|
|
|
|
client.write('PING');
|
2010-08-13 17:54:40 +02:00
|
|
|
});
|
2009-07-15 07:54:01 +02:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
client.on('data', function(data) {
|
2010-08-13 17:54:40 +02:00
|
|
|
console.log(data);
|
2010-12-03 02:03:18 +01:00
|
|
|
assert.equal('PONG', data);
|
|
|
|
assert.equal('open', client.readyState);
|
2009-07-15 07:54:01 +02:00
|
|
|
|
2010-12-03 02:03:18 +01:00
|
|
|
setTimeout(function() {
|
|
|
|
assert.equal('open', client.readyState);
|
2010-08-13 17:54:40 +02:00
|
|
|
if (count++ < N) {
|
2010-12-03 02:03:18 +01:00
|
|
|
client.write('PING');
|
2010-08-13 17:54:40 +02:00
|
|
|
} else {
|
2010-12-03 02:03:18 +01:00
|
|
|
console.log('closing client');
|
2010-08-13 17:54:40 +02:00
|
|
|
client.end();
|
|
|
|
client_ended = true;
|
|
|
|
}
|
|
|
|
}, DELAY);
|
|
|
|
});
|
2009-07-15 07:54:01 +02:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
client.on('timeout', function() {
|
2010-12-03 02:03:18 +01:00
|
|
|
common.debug('client-side timeout!!');
|
2010-08-13 17:54:40 +02:00
|
|
|
assert.equal(false, true);
|
|
|
|
});
|
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
client.on('close', function() {
|
2010-12-03 02:03:18 +01:00
|
|
|
console.log('client.end');
|
|
|
|
assert.equal(N + 1, count);
|
2010-08-13 17:54:40 +02:00
|
|
|
assert.ok(client_ended);
|
|
|
|
if (on_complete) on_complete();
|
|
|
|
tests_run += 1;
|
|
|
|
});
|
2009-07-15 07:54:01 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2010-07-15 20:47:25 +02:00
|
|
|
pingPongTest(common.PORT);
|
2009-07-15 07:54:01 +02:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
process.on('exit', function() {
|
2009-11-28 18:26:59 +01:00
|
|
|
assert.equal(1, tests_run);
|
2009-08-26 18:51:04 +02:00
|
|
|
});
|