11cb0ef41Sopenharmony_ci// When MessagePort.onmessage is set to a value that is not a function, the
21cb0ef41Sopenharmony_ci// setter should call .unref() and .stop(), clearing a previous onmessage
31cb0ef41Sopenharmony_ci// listener from holding the event loop open. This test confirms that
41cb0ef41Sopenharmony_ci// functionality.
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci'use strict';
71cb0ef41Sopenharmony_ciconst common = require('../common');
81cb0ef41Sopenharmony_ciconst { Worker, parentPort } = require('worker_threads');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci// Do not use isMainThread so that this test itself can be run inside a Worker.
111cb0ef41Sopenharmony_ciif (!process.env.HAS_STARTED_WORKER) {
121cb0ef41Sopenharmony_ci  process.env.HAS_STARTED_WORKER = 1;
131cb0ef41Sopenharmony_ci  const w = new Worker(__filename);
141cb0ef41Sopenharmony_ci  w.postMessage(2);
151cb0ef41Sopenharmony_ci} else {
161cb0ef41Sopenharmony_ci  // .onmessage uses a setter. Set .onmessage to a function that ultimately
171cb0ef41Sopenharmony_ci  // should not be called. This will call .ref() and .start() which will keep
181cb0ef41Sopenharmony_ci  // the event loop open (and prevent this from exiting) if the subsequent
191cb0ef41Sopenharmony_ci  // assignment of a value to .onmessage doesn't call .unref() and .stop().
201cb0ef41Sopenharmony_ci  parentPort.onmessage = common.mustNotCall();
211cb0ef41Sopenharmony_ci  // Setting `onmessage` to a value that is not a function should clear the
221cb0ef41Sopenharmony_ci  // previous value and also should allow the event loop to exit. (In other
231cb0ef41Sopenharmony_ci  // words, this test should exit rather than run indefinitely.)
241cb0ef41Sopenharmony_ci  parentPort.onmessage = 'fhqwhgads';
251cb0ef41Sopenharmony_ci}
26