0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-30 15:30:56 +01:00
nodejs/test/pummel/test-net-pingpong-delay.js

94 lines
2.2 KiB
JavaScript
Raw Normal View History

2010-12-05 00:20:34 +01:00
var common = require('../common');
var assert = require('assert');
2010-12-05 01:11:57 +01:00
var net = require('net');
var tests_run = 0;
2010-12-03 02:03:18 +01:00
function pingPongTest(port, host, on_complete) {
var N = 100;
var DELAY = 1;
var count = 0;
var client_ended = false;
2010-12-03 02:03:18 +01:00
var server = net.createServer({ allowHalfOpen: true }, function(socket) {
socket.setEncoding('utf8');
socket.on('data', function(data) {
console.log(data);
2010-12-03 02:03:18 +01:00
assert.equal('PING', data);
assert.equal('open', socket.readyState);
assert.equal(true, count <= N);
2010-12-03 02:03:18 +01:00
setTimeout(function() {
assert.equal('open', socket.readyState);
socket.write('PONG');
}, DELAY);
});
socket.on('timeout', function() {
2010-12-03 02:03:18 +01:00
common.debug('server-side timeout!!');
assert.equal(false, true);
});
socket.on('end', function() {
2010-12-03 02:03:18 +01:00
console.log('server-side socket EOF');
assert.equal('writeOnly', socket.readyState);
socket.end();
});
socket.on('close', function(had_error) {
2010-12-03 02:03:18 +01:00
console.log('server-side socket.end');
assert.equal(false, had_error);
2010-12-03 02:03:18 +01:00
assert.equal('closed', socket.readyState);
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);
2010-12-03 02:03:18 +01:00
client.setEncoding('utf8');
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
});
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);
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);
});
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);
});
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;
});
});
}
pingPongTest(common.PORT);
process.on('exit', function() {
assert.equal(1, tests_run);
});