1// Flags: --expose-internals 2'use strict'; 3 4const common = require('../common'); 5const strictEqual = require('assert').strictEqual; 6const { internalBinding } = require('internal/test/binding'); 7 8// child_process 9{ 10 const spawn = require('child_process').spawn; 11 const cmd = common.isWindows ? 'rundll32' : 'ls'; 12 const cp = spawn(cmd); 13 strictEqual(cp._handle.hasRef(), 14 true, 'process_wrap: not initially refed'); 15 cp.unref(); 16 strictEqual(cp._handle.hasRef(), 17 false, 'process_wrap: unref() ineffective'); 18 cp.ref(); 19 strictEqual(cp._handle.hasRef(), 20 true, 'process_wrap: ref() ineffective'); 21 cp._handle.close(common.mustCall(() => 22 strictEqual(cp._handle.hasRef(), 23 false, 'process_wrap: not unrefed on close'))); 24} 25 26 27const dgram = require('dgram'); 28const { kStateSymbol } = require('internal/dgram'); 29 30// dgram ipv4 31{ 32 const sock4 = dgram.createSocket('udp4'); 33 const handle = sock4[kStateSymbol].handle; 34 35 strictEqual(handle.hasRef(), 36 true, 'udp_wrap: ipv4: not initially refed'); 37 sock4.unref(); 38 strictEqual(handle.hasRef(), 39 false, 'udp_wrap: ipv4: unref() ineffective'); 40 sock4.ref(); 41 strictEqual(handle.hasRef(), 42 true, 'udp_wrap: ipv4: ref() ineffective'); 43 handle.close(common.mustCall(() => 44 strictEqual(handle.hasRef(), 45 false, 'udp_wrap: ipv4: not unrefed on close'))); 46} 47 48 49// dgram ipv6 50{ 51 const sock6 = dgram.createSocket('udp6'); 52 const handle = sock6[kStateSymbol].handle; 53 54 strictEqual(handle.hasRef(), 55 true, 'udp_wrap: ipv6: not initially refed'); 56 sock6.unref(); 57 strictEqual(handle.hasRef(), 58 false, 'udp_wrap: ipv6: unref() ineffective'); 59 sock6.ref(); 60 strictEqual(handle.hasRef(), 61 true, 'udp_wrap: ipv6: ref() ineffective'); 62 handle.close(common.mustCall(() => 63 strictEqual(handle.hasRef(), 64 false, 'udp_wrap: ipv6: not unrefed on close'))); 65} 66 67 68// pipe 69{ 70 const { Pipe, constants: PipeConstants } = internalBinding('pipe_wrap'); 71 const handle = new Pipe(PipeConstants.SOCKET); 72 strictEqual(handle.hasRef(), 73 true, 'pipe_wrap: not initially refed'); 74 handle.unref(); 75 strictEqual(handle.hasRef(), 76 false, 'pipe_wrap: unref() ineffective'); 77 handle.ref(); 78 strictEqual(handle.hasRef(), 79 true, 'pipe_wrap: ref() ineffective'); 80 handle.close(common.mustCall(() => 81 strictEqual(handle.hasRef(), 82 false, 'pipe_wrap: not unrefed on close'))); 83} 84 85 86// tcp 87{ 88 const net = require('net'); 89 const server = net.createServer(() => {}).listen(0); 90 strictEqual(server._handle.hasRef(), 91 true, 'tcp_wrap: not initially refed'); 92 strictEqual(server._unref, 93 false, 'tcp_wrap: _unref initially incorrect'); 94 server.unref(); 95 strictEqual(server._handle.hasRef(), 96 false, 'tcp_wrap: unref() ineffective'); 97 strictEqual(server._unref, 98 true, 'tcp_wrap: _unref not updated on unref()'); 99 server.ref(); 100 strictEqual(server._handle.hasRef(), 101 true, 'tcp_wrap: ref() ineffective'); 102 strictEqual(server._unref, 103 false, 'tcp_wrap: _unref not updated on ref()'); 104 server._handle.close(common.mustCall(() => 105 strictEqual(server._handle.hasRef(), 106 false, 'tcp_wrap: not unrefed on close'))); 107} 108 109// timers 110{ 111 strictEqual(process.getActiveResourcesInfo().filter( 112 (type) => type === 'Timeout').length, 0); 113 const timeout = setTimeout(() => {}, 500); 114 strictEqual(process.getActiveResourcesInfo().filter( 115 (type) => type === 'Timeout').length, 1); 116 timeout.unref(); 117 strictEqual(process.getActiveResourcesInfo().filter( 118 (type) => type === 'Timeout').length, 0); 119 timeout.ref(); 120 strictEqual(process.getActiveResourcesInfo().filter( 121 (type) => type === 'Timeout').length, 1); 122 123 strictEqual(process.getActiveResourcesInfo().filter( 124 (type) => type === 'Immediate').length, 0); 125 const immediate = setImmediate(() => {}); 126 strictEqual(process.getActiveResourcesInfo().filter( 127 (type) => type === 'Immediate').length, 1); 128 immediate.unref(); 129 strictEqual(process.getActiveResourcesInfo().filter( 130 (type) => type === 'Immediate').length, 0); 131 immediate.ref(); 132 strictEqual(process.getActiveResourcesInfo().filter( 133 (type) => type === 'Immediate').length, 1); 134} 135 136// See also test/pseudo-tty/test-handle-wrap-hasref-tty.js 137