|  | 
|  | 1 | +'use strict'; | 
|  | 2 | +const common = require('../common'); | 
|  | 3 | + | 
|  | 4 | +const { MessageChannel } = require('worker_threads'); | 
|  | 5 | +const { createHook } = require('async_hooks'); | 
|  | 6 | +const { strictEqual } = require('assert'); | 
|  | 7 | + | 
|  | 8 | +const handles = []; | 
|  | 9 | + | 
|  | 10 | +createHook({ | 
|  | 11 | +  init(asyncId, type, triggerAsyncId, resource) { | 
|  | 12 | +    if (type === 'MESSAGEPORT') { | 
|  | 13 | +      handles.push(resource); | 
|  | 14 | +    } | 
|  | 15 | +  } | 
|  | 16 | +}).enable(); | 
|  | 17 | + | 
|  | 18 | +const { port1, port2 } = new MessageChannel(); | 
|  | 19 | +strictEqual(handles[0], port1); | 
|  | 20 | +strictEqual(handles[1], port2); | 
|  | 21 | + | 
|  | 22 | +strictEqual(handles[0].hasRef(), false); | 
|  | 23 | +strictEqual(handles[1].hasRef(), false); | 
|  | 24 | + | 
|  | 25 | +port1.unref(); | 
|  | 26 | +strictEqual(handles[0].hasRef(), false); | 
|  | 27 | + | 
|  | 28 | +port1.ref(); | 
|  | 29 | +strictEqual(handles[0].hasRef(), true); | 
|  | 30 | + | 
|  | 31 | +port1.unref(); | 
|  | 32 | +strictEqual(handles[0].hasRef(), false); | 
|  | 33 | + | 
|  | 34 | +port1.on('message', () => {}); | 
|  | 35 | +strictEqual(handles[0].hasRef(), true); | 
|  | 36 | + | 
|  | 37 | +port2.unref(); | 
|  | 38 | +strictEqual(handles[1].hasRef(), false); | 
|  | 39 | + | 
|  | 40 | +port2.ref(); | 
|  | 41 | +strictEqual(handles[1].hasRef(), true); | 
|  | 42 | + | 
|  | 43 | +port2.unref(); | 
|  | 44 | +strictEqual(handles[1].hasRef(), false); | 
|  | 45 | + | 
|  | 46 | +port2.on('message', () => {}); | 
|  | 47 | +strictEqual(handles[0].hasRef(), true); | 
|  | 48 | + | 
|  | 49 | +port1.on('close', common.mustCall(() => { | 
|  | 50 | +  strictEqual(handles[0].hasRef(), false); | 
|  | 51 | +  strictEqual(handles[1].hasRef(), false); | 
|  | 52 | +})); | 
|  | 53 | + | 
|  | 54 | +port2.close(); | 
|  | 55 | + | 
|  | 56 | +strictEqual(handles[0].hasRef(), true); | 
|  | 57 | +strictEqual(handles[1].hasRef(), true); | 
0 commit comments