Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test,worker: add more tests for worker.ref()/.unref() #26083

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions test/parallel/test-worker-ref-onexit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';
const common = require('../common');
const { Worker } = require('worker_threads');

// Check that worker.unref() makes the 'exit' event not be emitted, if it is
// the only thing we would otherwise be waiting for.

const w = new Worker('', { eval: true });
w.unref();
w.on('exit', common.mustNotCall());
29 changes: 29 additions & 0 deletions test/parallel/test-worker-ref.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';
const common = require('../common');
const { Worker } = require('worker_threads');

// Test that calling worker.unref() leads to 'beforeExit' being emitted, and
// that we can resurrect the worker using worker.ref() from there.

const w = new Worker(`
const { parentPort } = require('worker_threads');
parentPort.once('message', (msg) => {
parentPort.postMessage(msg);
});
`, { eval: true });

process.once('beforeExit', common.mustCall(() => {
console.log('beforeExit');
w.ref();
w.postMessage({ hello: 'world' });
}));

w.once('message', common.mustCall((msg) => {
console.log('message', msg);
}));

w.on('exit', common.mustCall(() => {
console.log('exit');
}));

w.unref();