File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+ const common = require ( '../common' ) ;
3+ const { Worker } = require ( 'worker_threads' ) ;
4+
5+ // Check that worker.unref() makes the 'exit' event not be emitted, if it is
6+ // the only thing we would otherwise be waiting for.
7+
8+ const w = new Worker ( '' , { eval : true } ) ;
9+ w . unref ( ) ;
10+ w . on ( 'exit' , common . mustNotCall ( ) ) ;
Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+ const common = require ( '../common' ) ;
3+ const { Worker } = require ( 'worker_threads' ) ;
4+
5+ // Test that calling worker.unref() leads to 'beforeExit' being emitted, and
6+ // that we can resurrect the worker using worker.ref() from there.
7+
8+ const w = new Worker ( `
9+ const { parentPort } = require('worker_threads');
10+ parentPort.once('message', (msg) => {
11+ parentPort.postMessage(msg);
12+ });
13+ ` , { eval : true } ) ;
14+
15+ process . once ( 'beforeExit' , common . mustCall ( ( ) => {
16+ console . log ( 'beforeExit' ) ;
17+ w . ref ( ) ;
18+ w . postMessage ( { hello : 'world' } ) ;
19+ } ) ) ;
20+
21+ w . once ( 'message' , common . mustCall ( ( msg ) => {
22+ console . log ( 'message' , msg ) ;
23+ } ) ) ;
24+
25+ w . on ( 'exit' , common . mustCall ( ( ) => {
26+ console . log ( 'exit' ) ;
27+ } ) ) ;
28+
29+ w . unref ( ) ;
You can’t perform that action at this time.
0 commit comments