node/test/parallel/test-worker-message-port-terminate-transfer-list.js
Anna Henningsen 72650bcf72 worker: make transfer list behave like web MessagePort
Allow generic iterables as transfer list arguments, as well
as an options object with a `transfer` option, for web compatibility.

PR-URL: https://github.com/nodejs/node/pull/29319
Refs: https://github.com/nodejs/node/pull/28033#discussion_r289964991
Reviewed-By: James M Snell <jasnell@gmail.com>
2019-09-06 06:12:47 +02:00

27 lines
788 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const common = require('../common');
const { parentPort, MessageChannel, Worker } = require('worker_threads');
// Do not use isMainThread so that this test itself can be run inside a Worker.
if (!process.env.HAS_STARTED_WORKER) {
process.env.HAS_STARTED_WORKER = 1;
const w = new Worker(__filename);
w.once('message', common.mustCall(() => {
w.once('message', common.mustNotCall());
setTimeout(() => w.terminate(), 100);
}));
} else {
const { port1 } = new MessageChannel();
parentPort.postMessage('ready');
// Make sure we dont end up running JS after the infinite loop is broken.
port1.postMessage({}, {
transfer: (function*() { while (true); })()
});
parentPort.postMessage('UNREACHABLE');
process.kill(process.pid, 'SIGINT');
}