node/test/pummel/test-net-pingpong.js

124 lines
3.7 KiB
JavaScript
Raw Normal View History

2011-03-10 16:54:52 +08:00
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2011-11-03 07:26:43 +08:00
2011-08-24 14:42:23 +08:00
2010-12-05 07:20:34 +08:00
var common = require('../common');
var assert = require('assert');
2010-12-05 08:11:57 +08:00
var net = require('net');
2009-06-18 20:34:49 +08:00
var tests_run = 0;
2010-12-03 09:03:18 +08:00
function pingPongTest(port, host, on_complete) {
2009-06-18 20:34:49 +08:00
var N = 1000;
var count = 0;
var sent_final_ping = false;
2010-12-03 09:03:18 +08:00
var server = net.createServer({ allowHalfOpen: true }, function(socket) {
assert.equal(true, socket.remoteAddress !== null);
assert.equal(true, socket.remoteAddress !== undefined);
2010-12-03 09:03:18 +08:00
if (host === '127.0.0.1' || host === 'localhost' || !host) {
assert.equal(socket.remoteAddress, '127.0.0.1');
} else {
2010-12-03 09:03:18 +08:00
console.log('host = ' + host +
', remoteAddress = ' + socket.remoteAddress);
assert.equal(socket.remoteAddress, '::1');
}
2009-06-18 20:34:49 +08:00
2010-12-03 09:03:18 +08:00
socket.setEncoding('utf8');
socket.setNoDelay();
2009-06-18 20:34:49 +08:00
socket.timeout = 0;
socket.on('data', function(data) {
2010-12-03 09:03:18 +08:00
console.log('server got: ' + JSON.stringify(data));
assert.equal('open', socket.readyState);
assert.equal(true, count <= N);
2009-06-18 20:34:49 +08:00
if (/PING/.exec(data)) {
2010-12-03 09:03:18 +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
socket.on('end', function() {
2010-12-03 09:03:18 +08:00
assert.equal('writeOnly', socket.readyState);
socket.end();
2009-06-28 02:40:43 +08:00
});
2009-06-18 20:34:49 +08:00
socket.on('close', function(had_error) {
assert.equal(false, had_error);
2010-12-03 09:03:18 +08:00
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
});
2010-12-03 09:03:18 +08:00
server.listen(port, host, function() {
2010-08-13 23:54:40 +08:00
var client = net.createConnection(port, host);
2010-12-03 09:03:18 +08:00
client.setEncoding('utf8');
2009-04-22 21:52:23 +08:00
client.on('connect', function() {
2010-12-03 09:03:18 +08:00
assert.equal('open', client.readyState);
client.write('PING');
2010-08-13 23:54:40 +08:00
});
client.on('data', function(data) {
2010-08-13 23:54:40 +08:00
console.log('client got: ' + data);
2010-03-20 09:36:22 +08:00
2010-12-03 09:03:18 +08:00
assert.equal('PONG', data);
2010-08-13 23:54:40 +08:00
count += 1;
2010-08-13 23:54:40 +08:00
if (sent_final_ping) {
2010-12-03 09:03:18 +08:00
assert.equal('readOnly', client.readyState);
2010-08-13 23:54:40 +08:00
return;
} else {
2010-12-03 09:03:18 +08:00
assert.equal('open', client.readyState);
2010-08-13 23:54:40 +08:00
}
2010-08-13 23:54:40 +08:00
if (count < N) {
2010-12-03 09:03:18 +08:00
client.write('PING');
2010-08-13 23:54:40 +08:00
} else {
sent_final_ping = true;
2010-12-03 09:03:18 +08:00
client.write('PING');
2010-08-13 23:54:40 +08:00
client.end();
}
});
2009-06-18 20:34:49 +08:00
client.on('close', function() {
2010-12-03 09:03:18 +08:00
assert.equal(N + 1, count);
2010-08-13 23:54:40 +08:00
assert.equal(true, sent_final_ping);
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-12-03 09:03:18 +08:00
pingPongTest(common.PORT, 'localhost');
pingPongTest(common.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);
2010-12-03 09:03:18 +08:00
if (!solaris) pingPongTest(common.PORT + 2, '::1');
process.on('exit', function() {
2010-03-03 05:10:05 +08:00
assert.equal(solaris ? 2 : 3, tests_run);
});