summaryrefslogtreecommitdiff
path: root/test/parallel/test-worker-message-port.js
diff options
context:
space:
mode:
authorMichaël Zasso <targos@protonmail.com>2018-06-24 21:53:34 +0200
committerMichaël Zasso <targos@protonmail.com>2018-06-30 18:25:34 +0200
commitb87b16456535743db5ecb93478f7b871f0f000a1 (patch)
treeba02c9e05cfd93f7ba99a7c1859ee600c84ad141 /test/parallel/test-worker-message-port.js
parent64a3fadf7192485197b9695b7bb2165c7e7d762d (diff)
downloadandroid-node-v8-b87b16456535743db5ecb93478f7b871f0f000a1.tar.gz
android-node-v8-b87b16456535743db5ecb93478f7b871f0f000a1.tar.bz2
android-node-v8-b87b16456535743db5ecb93478f7b871f0f000a1.zip
test: add worker prefix to test-message* tests
It makes it easier to locate all tests related to the worker subsystem. PR-URL: https://github.com/nodejs/node/pull/21512 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Minwoo Jung <minwoo@nodesource.com> Reviewed-By: Yuta Hiroto <hello@hiroppy.me>
Diffstat (limited to 'test/parallel/test-worker-message-port.js')
-rw-r--r--test/parallel/test-worker-message-port.js71
1 files changed, 71 insertions, 0 deletions
diff --git a/test/parallel/test-worker-message-port.js b/test/parallel/test-worker-message-port.js
new file mode 100644
index 0000000000..2d321611ec
--- /dev/null
+++ b/test/parallel/test-worker-message-port.js
@@ -0,0 +1,71 @@
+// Flags: --experimental-worker
+'use strict';
+const common = require('../common');
+const assert = require('assert');
+
+const { MessageChannel, MessagePort } = require('worker_threads');
+
+{
+ const { port1, port2 } = new MessageChannel();
+ assert(port1 instanceof MessagePort);
+ assert(port2 instanceof MessagePort);
+
+ const input = { a: 1 };
+ port1.postMessage(input);
+ port2.on('message', common.mustCall((received) => {
+ assert.deepStrictEqual(received, input);
+ port2.close(common.mustCall());
+ }));
+}
+
+{
+ const { port1, port2 } = new MessageChannel();
+
+ port1.onmessage = common.mustCall((message) => {
+ assert.strictEqual(message, 4);
+ port2.close(common.mustCall());
+ });
+
+ port1.postMessage(2);
+
+ port2.onmessage = common.mustCall((message) => {
+ port2.postMessage(message * 2);
+ });
+}
+
+{
+ const { port1, port2 } = new MessageChannel();
+
+ const input = { a: 1 };
+ port1.postMessage(input);
+ // Check that the message still gets delivered if `port2` has its
+ // `on('message')` handler attached at a later point in time.
+ setImmediate(() => {
+ port2.on('message', common.mustCall((received) => {
+ assert.deepStrictEqual(received, input);
+ port2.close(common.mustCall());
+ }));
+ });
+}
+
+{
+ const { port1, port2 } = new MessageChannel();
+
+ const input = { a: 1 };
+
+ const dummy = common.mustNotCall();
+ // Check that the message still gets delivered if `port2` has its
+ // `on('message')` handler attached at a later point in time, even if a
+ // listener was removed previously.
+ port2.addListener('message', dummy);
+ setImmediate(() => {
+ port2.removeListener('message', dummy);
+ port1.postMessage(input);
+ setImmediate(() => {
+ port2.on('message', common.mustCall((received) => {
+ assert.deepStrictEqual(received, input);
+ port2.close(common.mustCall());
+ }));
+ });
+ });
+}