Skip to content

Commit bd99e8d

Browse files
Trottbrendanashworth
authored andcommitted
test: more test coverage for maxConnections
If the server is not accepting connections because maxConnections is exceeded, the server should start accepting connections again when a connection closes. PR-URL: #1855 Reviewed-By: Brendan Ashworth <[email protected]>
1 parent 8c71a92 commit bd99e8d

2 files changed

+85
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'use strict';
2+
var common = require('../common');
3+
var assert = require('assert');
4+
5+
var net = require('net');
6+
7+
// Sets the server's maxConnections property to 1.
8+
// Open 2 connections (connection 0 and connection 1).
9+
// Connection 0 should be accepted.
10+
// Connection 1 should be rejected.
11+
// Closes connection 0.
12+
// Open 2 more connections (connection 2 and 3).
13+
// Connection 2 should be accepted.
14+
// Connection 3 should be rejected.
15+
16+
var connections = [];
17+
var received = [];
18+
var sent = [];
19+
20+
var createConnection = function(index) {
21+
console.error('creating connection ' + index);
22+
23+
return new Promise(function(resolve, reject) {
24+
var connection = net.createConnection(common.PORT, function() {
25+
var msg = '' + index;
26+
console.error('sending message: ' + msg);
27+
this.write(msg);
28+
sent.push(msg);
29+
});
30+
31+
connection.on('data', function(e) {
32+
console.error('connection ' + index + ' received response');
33+
resolve();
34+
});
35+
36+
connection.on('end', function() {
37+
console.error('ending ' + index);
38+
resolve();
39+
});
40+
41+
connections[index] = connection;
42+
});
43+
};
44+
45+
var closeConnection = function(index) {
46+
console.error('closing connection ' + index);
47+
return new Promise(function(resolve, reject) {
48+
connections[index].on('end', function() {
49+
resolve();
50+
});
51+
connections[index].end();
52+
});
53+
};
54+
55+
var server = net.createServer(function(socket) {
56+
socket.on('data', function(data) {
57+
console.error('received message: ' + data);
58+
received.push('' + data);
59+
socket.write('acknowledged');
60+
});
61+
});
62+
63+
server.maxConnections = 1;
64+
65+
server.listen(common.PORT, function() {
66+
createConnection(0)
67+
.then(createConnection.bind(null, 1))
68+
.then(closeConnection.bind(null, 0))
69+
.then(createConnection.bind(null, 2))
70+
.then(createConnection.bind(null, 3))
71+
.then(server.close.bind(server))
72+
.then(closeConnection.bind(null, 2));
73+
});
74+
75+
process.on('exit', function() {
76+
// Confirm that all connections tried to send data...
77+
assert.deepEqual(sent, [0, 1, 2, 3]);
78+
// ...but that only connections 0 and 2 were successful.
79+
assert.deepEqual(received, [0, 2]);
80+
});
81+
82+
process.on('unhandledRejection', function() {
83+
console.error('promise rejected');
84+
assert.fail(null, null, 'A promise in the chain rejected');
85+
});

test/parallel/test-net-server-max-connections.js

-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ var net = require('net');
77
// This test creates 200 connections to a server and sets the server's
88
// maxConnections property to 100. The first 100 connections make it through
99
// and the last 100 connections are rejected.
10-
// TODO: test that the server can accept more connections after it reaches
11-
// its maximum and some are closed.
1210

1311
var N = 200;
1412
var count = 0;

0 commit comments

Comments
 (0)