Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorDarshan Sen <raisinten@gmail.com>2022-05-02 07:39:39 +0300
committerMichaƫl Zasso <targos@protonmail.com>2022-05-02 15:39:33 +0300
commitbf9240ae8c82f2b275109528eff4ffa43207fde1 (patch)
treebf8ea70f6457e5d5cc2f94f8901bc9a9337088cb /test
parent1e7479d34c678e3f169f4a95f337d6cc9e4d346b (diff)
worker: add hasRef() to MessagePort
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>
Diffstat (limited to 'test')
-rw-r--r--test/parallel/test-messageport-hasref.js57
-rw-r--r--test/parallel/test-worker-message-port.js4
-rw-r--r--test/parallel/test-worker-messageport-hasref.js45
3 files changed, 104 insertions, 2 deletions
diff --git a/test/parallel/test-messageport-hasref.js b/test/parallel/test-messageport-hasref.js
new file mode 100644
index 00000000000..bc213f7897d
--- /dev/null
+++ b/test/parallel/test-messageport-hasref.js
@@ -0,0 +1,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);
diff --git a/test/parallel/test-worker-message-port.js b/test/parallel/test-worker-message-port.js
index ca28f6ccb73..2663dde2a1b 100644
--- a/test/parallel/test-worker-message-port.js
+++ b/test/parallel/test-worker-message-port.js
@@ -179,7 +179,7 @@ const { MessageChannel, MessagePort } = require('worker_threads');
assert.deepStrictEqual(
Object.getOwnPropertyNames(MessagePort.prototype).sort(),
[
- 'close', 'constructor', 'onmessage', 'onmessageerror', 'postMessage',
- 'ref', 'start', 'unref',
+ 'close', 'constructor', 'hasRef', 'onmessage', 'onmessageerror',
+ 'postMessage', 'ref', 'start', 'unref',
]);
}
diff --git a/test/parallel/test-worker-messageport-hasref.js b/test/parallel/test-worker-messageport-hasref.js
new file mode 100644
index 00000000000..448787742e3
--- /dev/null
+++ b/test/parallel/test-worker-messageport-hasref.js
@@ -0,0 +1,45 @@
+'use strict';
+const common = require('../common');
+
+const { Worker } = require('worker_threads');
+const { createHook } = require('async_hooks');
+const { deepStrictEqual, strictEqual } = require('assert');
+
+const m = new Map();
+createHook({
+ init(asyncId, type, triggerAsyncId, resource) {
+ if (['WORKER', 'MESSAGEPORT'].includes(type)) {
+ m.set(asyncId, { type, resource });
+ }
+ },
+ destroy(asyncId) {
+ m.delete(asyncId);
+ }
+}).enable();
+
+function getActiveWorkerAndMessagePortTypes() {
+ const activeWorkerAndMessagePortTypes = [];
+ for (const asyncId of m.keys()) {
+ const { type, resource } = m.get(asyncId);
+ // Same logic as https://github.com/mafintosh/why-is-node-running/blob/24fb4c878753390a05d00959e6173d0d3c31fddd/index.js#L31-L32.
+ if (typeof resource.hasRef !== 'function' || resource.hasRef() === true) {
+ activeWorkerAndMessagePortTypes.push(type);
+ }
+ }
+ return activeWorkerAndMessagePortTypes;
+}
+
+const w = new Worker('', { eval: true });
+deepStrictEqual(getActiveWorkerAndMessagePortTypes(), ['WORKER']);
+w.unref();
+deepStrictEqual(getActiveWorkerAndMessagePortTypes(), []);
+w.ref();
+deepStrictEqual(getActiveWorkerAndMessagePortTypes(), ['WORKER', 'MESSAGEPORT']);
+
+w.on('exit', common.mustCall((exitCode) => {
+ strictEqual(exitCode, 0);
+ deepStrictEqual(getActiveWorkerAndMessagePortTypes(), ['WORKER']);
+ setTimeout(common.mustCall(() => {
+ deepStrictEqual(getActiveWorkerAndMessagePortTypes(), []);
+ }), 0);
+}));