1'use strict';
2const common = require('../common');
3const { 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
8const w = new Worker(`
9const { parentPort } = require('worker_threads');
10parentPort.once('message', (msg) => {
11  parentPort.postMessage(msg);
12});
13`, { eval: true });
14
15process.once('beforeExit', common.mustCall(() => {
16  console.log('beforeExit');
17  w.ref();
18  w.postMessage({ hello: 'world' });
19}));
20
21w.once('message', common.mustCall((msg) => {
22  console.log('message', msg);
23}));
24
25w.on('exit', common.mustCall(() => {
26  console.log('exit');
27}));
28
29w.unref();
30