GeezerVison is showing github.com — test: increase coverage for worker_threads · nodejs/node@70ccec2
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboard +26Lines changed: 26 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +// When MessagePort.onmessage is set to a value that is not a function, the |
| 2 | +// setter should call .unref() and .stop(), clearing a previous onmessage |
| 3 | +// listener from holding the event loop open. This test confirms that |
| 4 | +// functionality. |
| 5 | + |
| 6 | +// Flags: --experimental-worker |
| 7 | +'use strict'; |
| 8 | +const common = require('../common'); |
| 9 | +const { Worker, parentPort } = require('worker_threads'); |
| 10 | + |
| 11 | +// Do not use isMainThread so that this test itself can be run inside a Worker. |
| 12 | +if (!process.env.HAS_STARTED_WORKER) { |
| 13 | + process.env.HAS_STARTED_WORKER = 1; |
| 14 | + const w = new Worker(__filename); |
| 15 | + w.postMessage(2); |
| 16 | +} else { |
| 17 | + // .onmessage uses a setter. Set .onmessage to a function that ultimately |
| 18 | + // should not be called. This will call .ref() and .start() which will keep |
| 19 | + // the event loop open (and prevent this from exiting) if the subsequent |
| 20 | + // assignment of a value to .onmessage doesn't call .unref() and .stop(). |
| 21 | + parentPort.onmessage = common.mustNotCall(); |
| 22 | + // Setting `onmessage` to a value that is not a function should clear the |
| 23 | + // previous value and also should allow the event loop to exit. (In other |
| 24 | + // words, this test should exit rather than run indefinitely.) |
| 25 | + parentPort.onmessage = 'fhqwhgads'; |
| 26 | +} |