• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23require('../common');
24const assert = require('assert');
25const net = require('net');
26
27// settings
28const bytes = 1024 * 40;
29const concurrency = 50;
30const connections_per_client = 3;
31
32// measured
33let total_connections = 0;
34
35const body = 'C'.repeat(bytes);
36
37const server = net.createServer(function(c) {
38  total_connections++;
39  console.log('connected', total_connections);
40  c.write(body);
41  c.end();
42});
43
44function runClient(port, callback) {
45  const client = net.createConnection(port);
46
47  client.connections = 0;
48
49  client.setEncoding('utf8');
50
51  client.on('connect', function() {
52    console.log('c');
53    client.recved = '';
54    client.connections += 1;
55  });
56
57  client.on('data', function(chunk) {
58    this.recved += chunk;
59  });
60
61  client.on('end', function() {
62    client.end();
63  });
64
65  client.on('error', function(e) {
66    console.log('\n\nERROOOOOr');
67    throw e;
68  });
69
70  client.on('close', function(had_error) {
71    console.log('.');
72    assert.strictEqual(had_error, false);
73    assert.strictEqual(client.recved.length, bytes);
74
75    if (client.fd) {
76      console.log(client.fd);
77    }
78    assert.ok(!client.fd);
79
80    if (this.connections < connections_per_client) {
81      this.connect(port);
82    } else {
83      callback();
84    }
85  });
86}
87
88server.listen(0, function() {
89  let finished_clients = 0;
90  for (let i = 0; i < concurrency; i++) {
91    runClient(server.address().port, function() {
92      if (++finished_clients === concurrency) server.close();
93    });
94  }
95});
96
97process.on('exit', function() {
98  assert.strictEqual(total_connections, connections_per_client * concurrency);
99  console.log('\nokay!');
100});
101