0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/pummel/test-net-many-clients.js

105 lines
2.7 KiB
JavaScript
Raw Normal View History

2011-03-10 09:54:52 +01: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.
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');
// settings
2010-12-03 02:03:18 +01:00
var bytes = 1024 * 40;
var concurrency = 100;
var connections_per_client = 5;
// measured
var total_connections = 0;
2010-12-03 02:03:18 +01:00
var body = '';
for (var i = 0; i < bytes; i++) {
2010-12-03 02:03:18 +01:00
body += 'C';
}
2010-12-03 02:03:18 +01:00
var server = net.createServer(function(c) {
c.on('connect', function() {
total_connections++;
2010-12-03 02:03:18 +01:00
common.print('#');
c.write(body);
c.end();
});
});
2010-12-03 02:03:18 +01:00
function runClient(callback) {
var client = net.createConnection(common.PORT);
2010-03-10 01:27:49 +01:00
client.connections = 0;
2010-03-10 01:27:49 +01:00
2010-12-03 02:03:18 +01:00
client.setEncoding('utf8');
client.on('connect', function() {
2010-12-03 02:03:18 +01:00
common.print('c');
client.recved = '';
client.connections += 1;
});
client.on('data', function(chunk) {
this.recved += chunk;
});
client.on('end', function() {
client.end();
});
client.on('error', function(e) {
2010-12-03 02:03:18 +01:00
console.log('\n\nERROOOOOr');
2010-03-10 01:27:49 +01:00
throw e;
});
client.on('close', function(had_error) {
2010-12-03 02:03:18 +01:00
common.print('.');
assert.equal(false, had_error);
assert.equal(bytes, client.recved.length);
2010-03-10 01:27:49 +01:00
if (client.fd) {
console.log(client.fd);
2010-03-10 01:27:49 +01:00
}
assert.ok(!client.fd);
if (this.connections < connections_per_client) {
this.connect(common.PORT);
} else {
2009-08-26 22:03:19 +02:00
callback();
}
});
}
2010-12-03 02:03:18 +01:00
server.listen(common.PORT, function() {
2010-03-10 01:27:49 +01:00
var finished_clients = 0;
for (var i = 0; i < concurrency; i++) {
2010-12-03 02:03:18 +01:00
runClient(function() {
2010-03-10 01:27:49 +01:00
if (++finished_clients == concurrency) server.close();
});
}
});
process.on('exit', function() {
assert.equal(connections_per_client * concurrency, total_connections);
2010-12-03 02:03:18 +01:00
console.log('\nokay!');
});