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
|
'use strict';
const common = require('../common');
const { MessageChannel } = require('worker_threads');
const { createHook } = require('async_hooks');
const { strictEqual } = require('assert');
const handles = [];
createHook({
init(asyncId, type, triggerAsyncId, resource) {
if (type === 'MESSAGEPORT') {
handles.push(resource);
}
}
}).enable();
const { port1, port2 } = new MessageChannel();
strictEqual(handles[0], port1);
strictEqual(handles[1], port2);
strictEqual(handles[0].hasRef(), false);
strictEqual(handles[1].hasRef(), false);
port1.unref();
strictEqual(handles[0].hasRef(), false);
port1.ref();
strictEqual(handles[0].hasRef(), true);
port1.unref();
strictEqual(handles[0].hasRef(), false);
port1.on('message', () => {});
strictEqual(handles[0].hasRef(), true);
port2.unref();
strictEqual(handles[1].hasRef(), false);
port2.ref();
strictEqual(handles[1].hasRef(), true);
port2.unref();
strictEqual(handles[1].hasRef(), false);
port2.on('message', () => {});
strictEqual(handles[0].hasRef(), true);
port1.on('close', common.mustCall(() => {
strictEqual(handles[0].hasRef(), false);
strictEqual(handles[1].hasRef(), false);
}));
port2.close();
strictEqual(handles[0].hasRef(), true);
strictEqual(handles[1].hasRef(), true);
|