Skip to content

Commit

Permalink
test,worker: add more tests for worker.ref()/.unref()
Browse files Browse the repository at this point in the history
PR-URL: #26083
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
addaleax authored and rvagg committed Feb 28, 2019
1 parent 77a944c commit d1e3724
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
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();

0 comments on commit d1e3724

Please sign in to comment.