Skip to content

Commit 8271800

Browse files
committed
test: fix test/simple/test-net-server-max-connections.js is racey
Fixes #1333.
1 parent 93298af commit 8271800

File tree

1 file changed

+48
-46
lines changed

1 file changed

+48
-46
lines changed

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

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ var server = net.createServer(function(connection) {
4242
});
4343

4444
server.listen(common.PORT, function() {
45-
for (var i = 0; i < N; i++) {
46-
makeConnection(i);
47-
}
45+
makeConnection(0);
4846
});
4947

5048
server.maxConnections = N / 2;
@@ -53,50 +51,54 @@ console.error('server.maxConnections = %d', server.maxConnections);
5351

5452

5553
function makeConnection(index) {
56-
setTimeout(function() {
57-
var c = net.createConnection(common.PORT);
58-
var gotData = false;
59-
60-
c.on('end', function() { c.end(); });
61-
62-
c.on('data', function(b) {
63-
gotData = true;
64-
assert.ok(0 < b.length);
65-
});
66-
67-
c.on('error', function(e) {
68-
console.error('error %d: %s', index, e);
69-
});
70-
71-
c.on('close', function() {
72-
console.error('closed %d', index);
73-
closes++;
74-
75-
if (closes < N / 2) {
76-
assert.ok(server.maxConnections <= index,
77-
index +
78-
' was one of the first closed connections ' +
79-
'but shouldnt have been');
54+
var c = net.createConnection(common.PORT);
55+
var gotData = false;
56+
57+
c.on('connect', function() {
58+
if (index + 1 < N) {
59+
makeConnection(index + 1);
60+
}
61+
});
62+
63+
c.on('end', function() { c.end(); });
64+
65+
c.on('data', function(b) {
66+
gotData = true;
67+
assert.ok(0 < b.length);
68+
});
69+
70+
c.on('error', function(e) {
71+
console.error('error %d: %s', index, e);
72+
});
73+
74+
c.on('close', function() {
75+
console.error('closed %d', index);
76+
closes++;
77+
78+
if (closes < N / 2) {
79+
assert.ok(server.maxConnections <= index,
80+
index +
81+
' was one of the first closed connections ' +
82+
'but shouldnt have been');
83+
}
84+
85+
if (closes === N / 2) {
86+
var cb;
87+
console.error('calling wait callback.');
88+
while (cb = waits.shift()) {
89+
cb();
8090
}
81-
82-
if (closes === N / 2) {
83-
var cb;
84-
console.error('calling wait callback.');
85-
while (cb = waits.shift()) {
86-
cb();
87-
}
88-
server.close();
89-
}
90-
91-
if (index < server.maxConnections) {
92-
assert.equal(true, gotData,
93-
index + ' didn\'t get data, but should have');
94-
} else {
95-
assert.equal(false, gotData,
96-
index + ' got data, but shouldn\'t have');
97-
}
98-
});
99-
}, index);
91+
server.close();
92+
}
93+
94+
if (index < server.maxConnections) {
95+
assert.equal(true, gotData,
96+
index + ' didn\'t get data, but should have');
97+
} else {
98+
assert.equal(false, gotData,
99+
index + ' got data, but shouldn\'t have');
100+
}
101+
});
100102
}
101103

102104

0 commit comments

Comments
 (0)