Skip to content

Commit d1e3724

Browse files
addaleaxrvagg
authored andcommitted
test,worker: add more tests for worker.ref()/.unref()
PR-URL: #26083 Reviewed-By: Richard Lau <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 77a944c commit d1e3724

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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());

test/parallel/test-worker-ref.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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();

0 commit comments

Comments
 (0)