diff options
author | Darshan Sen <raisinten@gmail.com> | 2022-04-20 02:27:14 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-20 02:27:14 +0300 |
commit | 1b8d1794cfd764a233d060f1df96ed9fb10a0106 (patch) | |
tree | cea6fe2ba07dcfaa049a9e70e9f8aee8c74df0e6 /test | |
parent | fc326d674d4f366a78b882d3a32b0796448296bc (diff) |
worker: add hasRef() to the handle object
This should help projects like
https://github.com/mafintosh/why-is-node-running and
https://github.com/facebook/jest to detect if Worker instances are
keeping the event loop active correctly.
Fixes: https://github.com/nodejs/node/issues/42091
Refs: https://github.com/mafintosh/why-is-node-running/issues/59
Signed-off-by: Darshan Sen <raisinten@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/42756
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/parallel/test-worker-hasref.js | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/test/parallel/test-worker-hasref.js b/test/parallel/test-worker-hasref.js new file mode 100644 index 00000000000..51593b14725 --- /dev/null +++ b/test/parallel/test-worker-hasref.js @@ -0,0 +1,33 @@ +'use strict'; +const common = require('../common'); + +const { Worker } = require('worker_threads'); +const { createHook } = require('async_hooks'); +const { strictEqual } = require('assert'); + +let handle; + +createHook({ + init(asyncId, type, triggerAsyncId, resource) { + if (type === 'WORKER') { + handle = resource; + this.disable(); + } + } +}).enable(); + +const w = new Worker('', { eval: true }); + +strictEqual(handle.hasRef(), true); +w.unref(); +strictEqual(handle.hasRef(), false); +w.ref(); +strictEqual(handle.hasRef(), true); + +w.on('exit', common.mustCall((exitCode) => { + strictEqual(exitCode, 0); + strictEqual(handle.hasRef(), true); + setTimeout(common.mustCall(() => { + strictEqual(handle.hasRef(), undefined); + }), 0); +})); |