test,worker: add more tests for worker.ref()/.unref()
PR-URL: https://github.com/nodejs/node/pull/26083 Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
parent
5adda2c447
commit
6a2dde579c
10
test/parallel/test-worker-ref-onexit.js
Normal file
10
test/parallel/test-worker-ref-onexit.js
Normal file
@ -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
test/parallel/test-worker-ref.js
Normal file
29
test/parallel/test-worker-ref.js
Normal file
@ -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();
|
Loading…
x
Reference in New Issue
Block a user