|
1 | 1 | 'use strict'; |
2 | | -var common = require('../common'); |
3 | | -var assert = require('assert'); |
4 | | -var net = require('net'); |
5 | 2 |
|
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) { |
7 | 9 | c.write('hello'); |
8 | 10 | c.unref(); |
9 | 11 | }); |
10 | 12 | server.listen(common.PORT); |
11 | 13 | server.unref(); |
12 | 14 |
|
13 | | -var timedout = false; |
14 | 15 | 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]; |
17 | 18 |
|
18 | 19 | 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() { |
21 | 22 | if (++connections === delays.length) { |
22 | 23 | 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'); |
26 | 27 | }); |
27 | 28 |
|
28 | | - s[0].unref(); |
| 29 | + s.socket.unref(); |
29 | 30 | }); |
30 | 31 | } |
31 | | - }); |
32 | | - |
33 | | - sockets.push([socket, T]); |
34 | | -}); |
| 32 | + })); |
35 | 33 |
|
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}); |
39 | 35 | }); |
0 commit comments