node/test/parallel/test-net-socket-timeout-unref.js
Brian White 2bc7841d0f
test: use random ports where possible
This helps to prevent issues where a failed test can keep a bound
socket open long enough to cause other tests to fail with EADDRINUSE
because the same port number is used.

PR-URL: https://github.com/nodejs/node/pull/7045
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Rod Vagg <rod@vagg.org>
2016-06-10 22:30:55 -04:00

36 lines
839 B
JavaScript

'use strict';
// Test that unref'ed sockets with timeouts do not prevent exit.
const common = require('../common');
const net = require('net');
const server = net.createServer(function(c) {
c.write('hello');
c.unref();
});
server.listen(0);
server.unref();
var connections = 0;
const sockets = [];
const delays = [8, 5, 3, 6, 2, 4];
delays.forEach(function(T) {
const socket = net.createConnection(server.address().port, 'localhost');
socket.on('connect', common.mustCall(function() {
if (++connections === delays.length) {
sockets.forEach(function(s) {
s.socket.setTimeout(s.timeout, function() {
s.socket.destroy();
throw new Error('socket timed out unexpectedly');
});
s.socket.unref();
});
}
}));
sockets.push({socket: socket, timeout: T * 1000});
});