Skip to content

Commit

Permalink
test: fix flaky test-net-socket-timeout
Browse files Browse the repository at this point in the history
The setTimeout() call is unneeded. If the socket never times out, then
the test will never finish. Because timers can be unreliable on machines
under load, using setTimeout() here effectively creates a race
condition.

PR-URL: nodejs#10172
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
  • Loading branch information
Trott committed Dec 8, 2016
1 parent a0a6ff2 commit 6dd0754
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions test/parallel/test-net-socket-timeout.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
'use strict';
var common = require('../common');
var net = require('net');
var assert = require('assert');
const common = require('../common');
const net = require('net');
const assert = require('assert');

// Verify that invalid delays throw
var noop = function() {};
var s = new net.Socket();
var nonNumericDelays = ['100', true, false, undefined, null, '', {}, noop, []];
var badRangeDelays = [-0.001, -1, -Infinity, Infinity, NaN];
var validDelays = [0, 0.001, 1, 1e6];
const noop = function() {};
const s = new net.Socket();
const nonNumericDelays = [
'100', true, false, undefined, null, '', {}, noop, []
];
const badRangeDelays = [-0.001, -1, -Infinity, Infinity, NaN];
const validDelays = [0, 0.001, 1, 1e6];


for (let i = 0; i < nonNumericDelays.length; i++) {
assert.throws(function() {
Expand All @@ -28,15 +31,11 @@ for (let i = 0; i < validDelays.length; i++) {
});
}

var server = net.Server();
const server = net.Server();
server.listen(0, common.mustCall(function() {
var socket = net.createConnection(this.address().port);
socket.setTimeout(100, common.mustCall(function() {
const socket = net.createConnection(this.address().port);
socket.setTimeout(1, common.mustCall(function() {
socket.destroy();
server.close();
clearTimeout(timer);
}));
var timer = setTimeout(function() {
process.exit(1);
}, common.platformTimeout(200));
}));

0 comments on commit 6dd0754

Please sign in to comment.