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:
Anna Henningsen 2019-02-13 22:01:55 +01:00
parent 5adda2c447
commit 6a2dde579c
No known key found for this signature in database
GPG Key ID: 9C63F3A6CD2AD8F9
2 changed files with 39 additions and 0 deletions

View 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());

View 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();