Skip to content

Commit 2ab1237

Browse files
TrottMyles Borins
authored and
Myles Borins
committed
test: fix flaky test-net-socket-timeout-unref
Throw immediately on socket timeout rather than checking boolean in exit handler. PR-URL: #6003 Fixes: #5128 Reviewed-By: Myles Borins <[email protected]>
1 parent dd25984 commit 2ab1237

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed
+16-20
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,35 @@
11
'use strict';
2-
var common = require('../common');
3-
var assert = require('assert');
4-
var net = require('net');
52

6-
var server = net.createServer(function(c) {
3+
// Test that unref'ed sockets with timeouts do not prevent exit.
4+
5+
const common = require('../common');
6+
const net = require('net');
7+
8+
const server = net.createServer(function(c) {
79
c.write('hello');
810
c.unref();
911
});
1012
server.listen(common.PORT);
1113
server.unref();
1214

13-
var timedout = false;
1415
var connections = 0;
15-
var sockets = [];
16-
var delays = [8, 5, 3, 6, 2, 4];
16+
const sockets = [];
17+
const delays = [8, 5, 3, 6, 2, 4];
1718

1819
delays.forEach(function(T) {
19-
var socket = net.createConnection(common.PORT, 'localhost');
20-
socket.on('connect', function() {
20+
const socket = net.createConnection(common.PORT, 'localhost');
21+
socket.on('connect', common.mustCall(function() {
2122
if (++connections === delays.length) {
2223
sockets.forEach(function(s) {
23-
s[0].setTimeout(s[1] * 1000, function() {
24-
timedout = true;
25-
s[0].destroy();
24+
s.socket.setTimeout(s.timeout, function() {
25+
s.socket.destroy();
26+
throw new Error('socket timed out unexpectedly');
2627
});
2728

28-
s[0].unref();
29+
s.socket.unref();
2930
});
3031
}
31-
});
32-
33-
sockets.push([socket, T]);
34-
});
32+
}));
3533

36-
process.on('exit', function() {
37-
assert.strictEqual(timedout, false,
38-
'Socket timeout should not hold loop open');
34+
sockets.push({socket: socket, timeout: T * 1000});
3935
});

0 commit comments

Comments
 (0)