-
Notifications
You must be signed in to change notification settings - Fork 30.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
worker: handle calling terminate when kHandler is null
This PR makes a change to the Worker.terminate() when called if the kHandler is null. Before this pull request it was returning undefined, but the API is expecting a promise. With the changes in this PR if terminate is called a Promise.resolve() is returned, unless a callback is passed in which case the old behavior stays (returns undefined). PR-URL: #28370 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
- Loading branch information
1 parent
464136f
commit 43acce1
Showing
2 changed files
with
30 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { Worker } = require('worker_threads'); | ||
|
||
// Test that calling worker.terminate() if kHandler is null should return an | ||
// empty promise that resolves to undefined, even when a callback is passed | ||
|
||
const worker = new Worker(` | ||
const { parentPort } = require('worker_threads'); | ||
parentPort.postMessage({ hello: 'world' }); | ||
`, { eval: true }); | ||
|
||
process.once('beforeExit', common.mustCall(() => { | ||
console.log('beforeExit'); | ||
worker.ref(); | ||
})); | ||
|
||
worker.on('exit', common.mustCall(() => { | ||
console.log('exit'); | ||
worker.terminate().then((res) => assert.strictEqual(res, undefined)); | ||
worker.terminate(() => null).then( | ||
(res) => assert.strictEqual(res, undefined) | ||
); | ||
})); | ||
|
||
worker.unref(); |