Skip to content

Commit 70ccec2

Browse files
Trotttargos
authored andcommittedSep 23, 2018
test: increase coverage for worker_threads
Provide a test to cover adding setting `onmessage` to a non-function. This provides previously-missing coverage for an else block in the `onmessage` setter. PR-URL: #22942 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Yuta Hiroto <[email protected]>
1 parent a22485d commit 70ccec2

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
}

0 commit comments

Comments
 (0)
Please sign in to comment.