summaryrefslogtreecommitdiff
path: root/test/parallel/test-handle-wrap-isrefed.js
blob: b4fccc0529c2fb14f5ede4a0e5e2fad2f4a2a262 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'use strict';

const common = require('../common');
const strictEqual = require('assert').strictEqual;

// child_process
{
  const spawn = require('child_process').spawn;
  const cmd = common.isWindows ? 'rundll32' : 'ls';
  const cp = spawn(cmd);
  strictEqual(Object.getPrototypeOf(cp._handle).hasOwnProperty('hasRef'),
              true, 'process_wrap: hasRef() missing');
  strictEqual(cp._handle.hasRef(),
              true, 'process_wrap: not initially refed');
  cp.unref();
  strictEqual(cp._handle.hasRef(),
              false, 'process_wrap: unref() ineffective');
  cp.ref();
  strictEqual(cp._handle.hasRef(),
              true, 'process_wrap: ref() ineffective');
  cp._handle.close(common.mustCall(() =>
    strictEqual(cp._handle.hasRef(),
                false, 'process_wrap: not unrefed on close')));
}


const dgram = require('dgram');

// dgram ipv4
{
  const sock4 = dgram.createSocket('udp4');
  strictEqual(Object.getPrototypeOf(sock4._handle).hasOwnProperty('hasRef'),
              true, 'udp_wrap: ipv4: hasRef() missing');
  strictEqual(sock4._handle.hasRef(),
              true, 'udp_wrap: ipv4: not initially refed');
  sock4.unref();
  strictEqual(sock4._handle.hasRef(),
              false, 'udp_wrap: ipv4: unref() ineffective');
  sock4.ref();
  strictEqual(sock4._handle.hasRef(),
              true, 'udp_wrap: ipv4: ref() ineffective');
  sock4._handle.close(common.mustCall(() =>
    strictEqual(sock4._handle.hasRef(),
                false, 'udp_wrap: ipv4: not unrefed on close')));
}


// dgram ipv6
{
  const sock6 = dgram.createSocket('udp6');
  strictEqual(Object.getPrototypeOf(sock6._handle).hasOwnProperty('hasRef'),
              true, 'udp_wrap: ipv6: hasRef() missing');
  strictEqual(sock6._handle.hasRef(),
              true, 'udp_wrap: ipv6: not initially refed');
  sock6.unref();
  strictEqual(sock6._handle.hasRef(),
              false, 'udp_wrap: ipv6: unref() ineffective');
  sock6.ref();
  strictEqual(sock6._handle.hasRef(),
              true, 'udp_wrap: ipv6: ref() ineffective');
  sock6._handle.close(common.mustCall(() =>
    strictEqual(sock6._handle.hasRef(),
                false, 'udp_wrap: ipv6: not unrefed on close')));
}


// pipe
{
  const Pipe = process.binding('pipe_wrap').Pipe;
  const handle = new Pipe();
  strictEqual(Object.getPrototypeOf(handle).hasOwnProperty('hasRef'),
              true, 'pipe_wrap: hasRef() missing');
  strictEqual(handle.hasRef(),
              true, 'pipe_wrap: not initially refed');
  handle.unref();
  strictEqual(handle.hasRef(),
              false, 'pipe_wrap: unref() ineffective');
  handle.ref();
  strictEqual(handle.hasRef(),
              true, 'pipe_wrap: ref() ineffective');
  handle.close(common.mustCall(() =>
    strictEqual(handle.hasRef(),
                false, 'pipe_wrap: not unrefed on close')));
}


// tcp
{
  const net = require('net');
  const server = net.createServer(() => {}).listen(0);
  strictEqual(Object.getPrototypeOf(server._handle).hasOwnProperty('hasRef'),
              true, 'tcp_wrap: hasRef() missing');
  strictEqual(server._handle.hasRef(),
              true, 'tcp_wrap: not initially refed');
  strictEqual(server._unref,
              false, 'tcp_wrap: _unref initially incorrect');
  server.unref();
  strictEqual(server._handle.hasRef(),
              false, 'tcp_wrap: unref() ineffective');
  strictEqual(server._unref,
              true, 'tcp_wrap: _unref not updated on unref()');
  server.ref();
  strictEqual(server._handle.hasRef(),
              true, 'tcp_wrap: ref() ineffective');
  strictEqual(server._unref,
              false, 'tcp_wrap: _unref not updated on ref()');
  server._handle.close(common.mustCall(() =>
    strictEqual(server._handle.hasRef(),
                false, 'tcp_wrap: not unrefed on close')));
}


// timers
{
  const timer = setTimeout(() => {}, 500);
  timer.unref();
  strictEqual(Object.getPrototypeOf(timer._handle).hasOwnProperty('hasRef'),
              true, 'timer_wrap: hasRef() missing');
  strictEqual(timer._handle.hasRef(),
              false, 'timer_wrap: unref() ineffective');
  timer.ref();
  strictEqual(timer._handle.hasRef(),
              true, 'timer_wrap: ref() ineffective');
  timer._handle.close(common.mustCall(() =>
    strictEqual(timer._handle.hasRef(),
                false, 'timer_wrap: not unrefed on close')));
}


// see also test/pseudo-tty/test-handle-wrap-isrefed-tty.js