forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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: nodejs#42091 Refs: mafintosh/why-is-node-running#59 Signed-off-by: Darshan Sen <raisinten@gmail.com> PR-URL: nodejs#42756 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
- Loading branch information
Showing
3 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
})); |