mirror of
https://github.com/nodejs/node.git
synced 2025-04-28 21:46:48 +00:00

Since we were removing the hasRef() method before exposing the MessagePort object, the only way of knowing if the handle was keeping the event loop active was to parse the string returned by util.inspect(port), which is inconvenient and inconsistent with most of the other async resources. So this change stops removing hasRef() from the MessagePort prototype. The reason why this is also being documented is that while reporting active resources, async_hooks returns the same MessagePort object as the one that is accessible by users. Refs: https://github.com/nodejs/node/issues/42091#issuecomment-1104793189 Signed-off-by: Darshan Sen <raisinten@gmail.com> PR-URL: https://github.com/nodejs/node/pull/42849 Reviewed-By: Anna Henningsen <anna@addaleax.net>
58 lines
1.2 KiB
JavaScript
58 lines
1.2 KiB
JavaScript
'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);
|