node/test/parallel/test-worker-messaging-errors-timeout.js
Paolo Insogna 66a635cece
worker: add postMessageToThread
PR-URL: https://github.com/nodejs/node/pull/53682
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
2024-07-09 07:16:04 +00:00

39 lines
941 B
JavaScript

'use strict';
const common = require('../common');
const {
postMessageToThread,
workerData,
Worker,
} = require('node:worker_threads');
const { rejects } = require('node:assert');
const memory = new SharedArrayBuffer(4);
async function test() {
const worker = new Worker(__filename, { workerData: { memory, children: true } });
const array = new Int32Array(memory);
await rejects(common.mustCall(function() {
return postMessageToThread(worker.threadId, 0, common.platformTimeout(500));
}), {
name: 'Error',
code: 'ERR_WORKER_MESSAGING_TIMEOUT',
});
Atomics.store(array, 0, 1);
Atomics.notify(array, 0);
}
if (!workerData?.children) {
test();
} else {
process.on('beforeExit', common.mustCall());
const array = new Int32Array(workerData.memory);
// Starve this thread waiting for the status to be unlocked.
// This happens in the main thread AFTER the timeout.
Atomics.wait(array, 0, 0);
}