diff options
author | mash-graz <mash-graz@users.noreply.github.com> | 2024-03-11 00:23:06 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-11 00:23:06 +0100 |
commit | 80dbcd3ddf589d20ae7786c716959c6d48ea1f1e (patch) | |
tree | 55967de66db8f4654539f462e6eea33385179aa3 /tests/node_compat | |
parent | 16dbbfa64a5d2905580535c52c1db51d1cf5b89f (diff) |
fix(ext/node) implement receiveMessageOnPort for node:worker_threads (#22766)
Implementation of `receiveMessageOnPort` for `node:worker_threads`
Fixes: #22702
Diffstat (limited to 'tests/node_compat')
-rw-r--r-- | tests/node_compat/config.jsonc | 3 | ||||
-rw-r--r-- | tests/node_compat/test/parallel/test-worker-message-port-receive-message.js | 40 |
2 files changed, 43 insertions, 0 deletions
diff --git a/tests/node_compat/config.jsonc b/tests/node_compat/config.jsonc index 3fde8be5a..c5902f6f9 100644 --- a/tests/node_compat/config.jsonc +++ b/tests/node_compat/config.jsonc @@ -104,6 +104,8 @@ "test-util.js", "test-webcrypto-sign-verify.js", "test-whatwg-url-properties.js", + // needs replace ".on" => ".addEventListener" in L29 + "test-worker-message-port-receive-message.js", "test-zlib-convenience-methods.js", "test-zlib-empty-buffer.js", "test-zlib-invalid-input.js", @@ -665,6 +667,7 @@ "test-whatwg-url-custom-tostringtag.js", "test-whatwg-url-override-hostname.js", "test-whatwg-url-properties.js", + "test-worker-message-port-receive-message.js", "test-zlib-close-after-error.js", "test-zlib-close-after-write.js", "test-zlib-convenience-methods.js", diff --git a/tests/node_compat/test/parallel/test-worker-message-port-receive-message.js b/tests/node_compat/test/parallel/test-worker-message-port-receive-message.js new file mode 100644 index 000000000..3945a8a1f --- /dev/null +++ b/tests/node_compat/test/parallel/test-worker-message-port-receive-message.js @@ -0,0 +1,40 @@ +// deno-fmt-ignore-file +// deno-lint-ignore-file + +// Copyright Joyent and Node contributors. All rights reserved. MIT license. +// Taken from Node 18.12.1 +// This file is automatically generated by `tools/node_compat/setup.ts`. Do not modify this file manually. + +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const { MessageChannel, receiveMessageOnPort } = require('worker_threads'); + +const { port1, port2 } = new MessageChannel(); + +const message1 = { hello: 'world' }; +const message2 = { foo: 'bar' }; + +// Make sure receiveMessageOnPort() works in a FIFO way, the same way it does +// when we’re using events. +assert.strictEqual(receiveMessageOnPort(port2), undefined); +port1.postMessage(message1); +port1.postMessage(message2); +assert.deepStrictEqual(receiveMessageOnPort(port2), { message: message1 }); +assert.deepStrictEqual(receiveMessageOnPort(port2), { message: message2 }); +assert.strictEqual(receiveMessageOnPort(port2), undefined); +assert.strictEqual(receiveMessageOnPort(port2), undefined); + +// Make sure message handlers aren’t called. +port2.addEventListener('message', common.mustNotCall()); +port1.postMessage(message1); +assert.deepStrictEqual(receiveMessageOnPort(port2), { message: message1 }); +port1.close(); + +for (const value of [null, 0, -1, {}, []]) { + assert.throws(() => receiveMessageOnPort(value), { + name: 'TypeError', + code: 'ERR_INVALID_ARG_TYPE', + message: 'The "port" argument must be a MessagePort instance' + }); +} |