Skip to content

Commit

Permalink
test: fix flaky test-http-set-timeout-server
Browse files Browse the repository at this point in the history
Make the servers listen on a free port number picked by the OS to avoid
rare `EADDRINUSE` errors on `SmartOS`.

Fixes: #6197
PR-URL: #6248
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
santigimeno committed Apr 19, 2016
1 parent bf22c71 commit 57f0595
Showing 1 changed file with 33 additions and 22 deletions.
55 changes: 33 additions & 22 deletions test/parallel/test-http-set-timeout-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ test(function serverTimeout(cb) {
var server = http.createServer(function(req, res) {
// just do nothing, we should get a timeout event.
});
server.listen(common.PORT);
server.listen(common.mustCall(function() {
http.get({ port: server.address().port }).on('error', function() {});
}));
var s = server.setTimeout(50, function(socket) {
caughtTimeout = true;
socket.destroy();
server.close();
cb();
});
assert.ok(s instanceof http.Server);
http.get({ port: common.PORT }).on('error', function() {});
});

test(function serverRequestTimeout(cb) {
Expand All @@ -56,11 +57,13 @@ test(function serverRequestTimeout(cb) {
});
assert.ok(s instanceof http.IncomingMessage);
});
server.listen(common.PORT);
var req = http.request({ port: common.PORT, method: 'POST' });
req.on('error', function() {});
req.write('Hello');
// req is in progress
server.listen(common.mustCall(function() {
var port = server.address().port;
var req = http.request({ port: port, method: 'POST' });
req.on('error', function() {});
req.write('Hello');
// req is in progress
}));
});

test(function serverResponseTimeout(cb) {
Expand All @@ -78,8 +81,10 @@ test(function serverResponseTimeout(cb) {
});
assert.ok(s instanceof http.OutgoingMessage);
});
server.listen(common.PORT);
http.get({ port: common.PORT }).on('error', function() {});
server.listen(common.mustCall(function() {
var port = server.address().port;
http.get({ port: port }).on('error', function() {});
}));
});

test(function serverRequestNotTimeoutAfterEnd(cb) {
Expand All @@ -104,8 +109,10 @@ test(function serverRequestNotTimeoutAfterEnd(cb) {
server.close();
cb();
});
server.listen(common.PORT);
http.get({ port: common.PORT }).on('error', function() {});
server.listen(common.mustCall(function() {
var port = server.address().port;
http.get({ port: port }).on('error', function() {});
}));
});

test(function serverResponseTimeoutWithPipeline(cb) {
Expand All @@ -125,12 +132,14 @@ test(function serverResponseTimeoutWithPipeline(cb) {
server.close();
cb();
});
server.listen(common.PORT);
var c = net.connect({ port: common.PORT, allowHalfOpen: true }, function() {
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
c.write('GET /2 HTTP/1.1\r\nHost: localhost\r\n\r\n');
c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n');
});
server.listen(common.mustCall(function() {
var port = server.address().port;
var c = net.connect({ port: port, allowHalfOpen: true }, function() {
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
c.write('GET /2 HTTP/1.1\r\nHost: localhost\r\n\r\n');
c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n');
});
}));
});

test(function idleTimeout(cb) {
Expand Down Expand Up @@ -158,9 +167,11 @@ test(function idleTimeout(cb) {
cb();
});
assert.ok(s instanceof http.Server);
server.listen(common.PORT);
var c = net.connect({ port: common.PORT, allowHalfOpen: true }, function() {
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
// Keep-Alive
});
server.listen(common.mustCall(function() {
var port = server.address().port;
var c = net.connect({ port: port, allowHalfOpen: true }, function() {
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
// Keep-Alive
});
}));
});

0 comments on commit 57f0595

Please sign in to comment.