0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-net-socket-timeout-unref.js
Gibson Fahnestock 7a0e462f9f test: use eslint to fix var->const/let
Manually fix issues that eslint --fix couldn't do automatically.

PR-URL: https://github.com/nodejs/node/pull/10685
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
2017-01-11 11:43:52 +00: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();
let 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});
});