Skip to content
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
test,worker: add more tests for worker.ref()/.unref()
  • Loading branch information
addaleax committed Feb 13, 2019
commit c341d8864f47317cca4c550230f2e512bc88b07a
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();