Skip to content

Commit f13c553

Browse files
committed
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.
1 parent 4d6b768 commit f13c553

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 server = net.createServer(function(socket) {
46+
socket.on('data', function(data) {
47+
console.error('received message: ' + data.toString());
48+
received.push(data.toString());
49+
socket.write('acknowledged');
50+
});
51+
});
52+
53+
server.maxConnections = 1;
54+
55+
server.listen(common.PORT, function() {
56+
createConnection(0)
57+
.then(createConnection.bind(null, 1))
58+
.then(function() {connections[0].end();})
59+
.then(createConnection.bind(null, 2))
60+
.then(createConnection.bind(null, 3))
61+
.then(function() {
62+
server.close(function() {
63+
// Confirm that all connections tried to send data...
64+
assert.deepEqual(sent,[0,1,2,3]);
65+
// ...but that only connections 0 and 2 were successful.
66+
assert.deepEqual(received,[0,2]);
67+
});
68+
connections[2].end();
69+
});
70+
});
71+
72+
process.on('unhandledRejection', function() {
73+
console.error('promise rejected');
74+
assert.fail(null, null, 'A promise in the chain rejected');
75+
});

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

Lines changed: 0 additions & 2 deletions
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)