11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ciconst { 51cb0ef41Sopenharmony_ci BroadcastChannel, 61cb0ef41Sopenharmony_ci Worker, 71cb0ef41Sopenharmony_ci receiveMessageOnPort 81cb0ef41Sopenharmony_ci} = require('worker_threads'); 91cb0ef41Sopenharmony_ciconst assert = require('assert'); 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ciassert.throws(() => new BroadcastChannel(Symbol('test')), { 121cb0ef41Sopenharmony_ci message: /Cannot convert a Symbol value to a string/ 131cb0ef41Sopenharmony_ci}); 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ciassert.throws(() => new BroadcastChannel(), { 161cb0ef41Sopenharmony_ci message: /The "name" argument must be specified/ 171cb0ef41Sopenharmony_ci}); 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ci// These should all just work 201cb0ef41Sopenharmony_ci[undefined, 1, null, 'test', 1n, false, Infinity].forEach((i) => { 211cb0ef41Sopenharmony_ci const bc = new BroadcastChannel(i); 221cb0ef41Sopenharmony_ci assert.strictEqual(bc.name, `${i}`); 231cb0ef41Sopenharmony_ci bc.close(); 241cb0ef41Sopenharmony_ci}); 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci{ 271cb0ef41Sopenharmony_ci // Empty postMessage throws 281cb0ef41Sopenharmony_ci const bc = new BroadcastChannel('whatever'); 291cb0ef41Sopenharmony_ci assert.throws(() => bc.postMessage(), { 301cb0ef41Sopenharmony_ci message: /The "message" argument must be specified/ 311cb0ef41Sopenharmony_ci }); 321cb0ef41Sopenharmony_ci bc.close(); 331cb0ef41Sopenharmony_ci // Calling close multiple times should not throw 341cb0ef41Sopenharmony_ci bc.close(); 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ci // Calling postMessage after close should throw 371cb0ef41Sopenharmony_ci assert.throws(() => bc.postMessage(null), { 381cb0ef41Sopenharmony_ci message: /BroadcastChannel is closed/ 391cb0ef41Sopenharmony_ci }); 401cb0ef41Sopenharmony_ci} 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci{ 431cb0ef41Sopenharmony_ci const bc1 = new BroadcastChannel('channel1'); 441cb0ef41Sopenharmony_ci const bc2 = new BroadcastChannel('channel1'); 451cb0ef41Sopenharmony_ci const bc3 = new BroadcastChannel('channel1'); 461cb0ef41Sopenharmony_ci const bc4 = new BroadcastChannel('channel2'); 471cb0ef41Sopenharmony_ci assert.strictEqual(bc1.name, 'channel1'); 481cb0ef41Sopenharmony_ci assert.strictEqual(bc2.name, 'channel1'); 491cb0ef41Sopenharmony_ci assert.strictEqual(bc3.name, 'channel1'); 501cb0ef41Sopenharmony_ci assert.strictEqual(bc4.name, 'channel2'); 511cb0ef41Sopenharmony_ci bc1.addEventListener('message', common.mustCall((event) => { 521cb0ef41Sopenharmony_ci assert.strictEqual(event.data, 'hello'); 531cb0ef41Sopenharmony_ci bc1.close(); 541cb0ef41Sopenharmony_ci bc2.close(); 551cb0ef41Sopenharmony_ci bc4.close(); 561cb0ef41Sopenharmony_ci })); 571cb0ef41Sopenharmony_ci bc3.addEventListener('message', common.mustCall((event) => { 581cb0ef41Sopenharmony_ci assert.strictEqual(event.data, 'hello'); 591cb0ef41Sopenharmony_ci bc3.close(); 601cb0ef41Sopenharmony_ci })); 611cb0ef41Sopenharmony_ci bc2.addEventListener('message', common.mustNotCall()); 621cb0ef41Sopenharmony_ci bc4.addEventListener('message', common.mustNotCall()); 631cb0ef41Sopenharmony_ci bc2.postMessage('hello'); 641cb0ef41Sopenharmony_ci} 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci{ 671cb0ef41Sopenharmony_ci const bc1 = new BroadcastChannel('onmessage-channel1'); 681cb0ef41Sopenharmony_ci const bc2 = new BroadcastChannel('onmessage-channel1'); 691cb0ef41Sopenharmony_ci const bc3 = new BroadcastChannel('onmessage-channel1'); 701cb0ef41Sopenharmony_ci const bc4 = new BroadcastChannel('onmessage-channel2'); 711cb0ef41Sopenharmony_ci assert.strictEqual(bc1.name, 'onmessage-channel1'); 721cb0ef41Sopenharmony_ci assert.strictEqual(bc2.name, 'onmessage-channel1'); 731cb0ef41Sopenharmony_ci assert.strictEqual(bc3.name, 'onmessage-channel1'); 741cb0ef41Sopenharmony_ci assert.strictEqual(bc4.name, 'onmessage-channel2'); 751cb0ef41Sopenharmony_ci bc1.onmessage = common.mustCall((event) => { 761cb0ef41Sopenharmony_ci assert.strictEqual(event.data, 'hello'); 771cb0ef41Sopenharmony_ci bc1.close(); 781cb0ef41Sopenharmony_ci bc2.close(); 791cb0ef41Sopenharmony_ci bc4.close(); 801cb0ef41Sopenharmony_ci }); 811cb0ef41Sopenharmony_ci bc3.onmessage = common.mustCall((event) => { 821cb0ef41Sopenharmony_ci assert.strictEqual(event.data, 'hello'); 831cb0ef41Sopenharmony_ci bc3.close(); 841cb0ef41Sopenharmony_ci }); 851cb0ef41Sopenharmony_ci bc2.onmessage = common.mustNotCall(); 861cb0ef41Sopenharmony_ci bc4.onmessage = common.mustNotCall(); 871cb0ef41Sopenharmony_ci bc2.postMessage('hello'); 881cb0ef41Sopenharmony_ci} 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_ci{ 911cb0ef41Sopenharmony_ci const bc = new BroadcastChannel('worker1'); 921cb0ef41Sopenharmony_ci new Worker(` 931cb0ef41Sopenharmony_ci const assert = require('assert'); 941cb0ef41Sopenharmony_ci const { BroadcastChannel } = require('worker_threads'); 951cb0ef41Sopenharmony_ci const bc = new BroadcastChannel('worker1'); 961cb0ef41Sopenharmony_ci bc.addEventListener('message', (event) => { 971cb0ef41Sopenharmony_ci assert.strictEqual(event.data, 123); 981cb0ef41Sopenharmony_ci // If this close() is not executed, the test should hang and timeout. 991cb0ef41Sopenharmony_ci // If the test does hang and timeout in CI, then the first step should 1001cb0ef41Sopenharmony_ci // be to check that the two bc.close() calls are being made. 1011cb0ef41Sopenharmony_ci bc.close(); 1021cb0ef41Sopenharmony_ci }); 1031cb0ef41Sopenharmony_ci bc.postMessage(321); 1041cb0ef41Sopenharmony_ci `, { eval: true }); 1051cb0ef41Sopenharmony_ci bc.addEventListener('message', common.mustCall(({ data }) => { 1061cb0ef41Sopenharmony_ci assert.strictEqual(data, 321); 1071cb0ef41Sopenharmony_ci bc.postMessage(123); 1081cb0ef41Sopenharmony_ci bc.close(); 1091cb0ef41Sopenharmony_ci })); 1101cb0ef41Sopenharmony_ci} 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_ci{ 1131cb0ef41Sopenharmony_ci const bc1 = new BroadcastChannel('channel3'); 1141cb0ef41Sopenharmony_ci const bc2 = new BroadcastChannel('channel3'); 1151cb0ef41Sopenharmony_ci const bc3 = new BroadcastChannel('channel3'); 1161cb0ef41Sopenharmony_ci bc3.postMessage(new SharedArrayBuffer(10)); 1171cb0ef41Sopenharmony_ci let received = 0; 1181cb0ef41Sopenharmony_ci for (const bc of [bc1, bc2]) { 1191cb0ef41Sopenharmony_ci bc.addEventListener('message', common.mustCall(({ data }) => { 1201cb0ef41Sopenharmony_ci assert(data instanceof SharedArrayBuffer); 1211cb0ef41Sopenharmony_ci if (++received === 2) { 1221cb0ef41Sopenharmony_ci bc1.close(); 1231cb0ef41Sopenharmony_ci bc2.close(); 1241cb0ef41Sopenharmony_ci bc3.close(); 1251cb0ef41Sopenharmony_ci } 1261cb0ef41Sopenharmony_ci })); 1271cb0ef41Sopenharmony_ci } 1281cb0ef41Sopenharmony_ci} 1291cb0ef41Sopenharmony_ci 1301cb0ef41Sopenharmony_ci{ 1311cb0ef41Sopenharmony_ci const bc1 = new BroadcastChannel('channel3'); 1321cb0ef41Sopenharmony_ci const mc = new MessageChannel(); 1331cb0ef41Sopenharmony_ci assert.throws(() => bc1.postMessage(mc), { 1341cb0ef41Sopenharmony_ci message: /Object that needs transfer was found/ 1351cb0ef41Sopenharmony_ci }); 1361cb0ef41Sopenharmony_ci assert.throws(() => bc1.postMessage(Symbol()), { 1371cb0ef41Sopenharmony_ci message: /Symbol\(\) could not be cloned/ 1381cb0ef41Sopenharmony_ci }); 1391cb0ef41Sopenharmony_ci bc1.close(); 1401cb0ef41Sopenharmony_ci assert.throws(() => bc1.postMessage(Symbol()), { 1411cb0ef41Sopenharmony_ci message: /BroadcastChannel is closed/ 1421cb0ef41Sopenharmony_ci }); 1431cb0ef41Sopenharmony_ci} 1441cb0ef41Sopenharmony_ci 1451cb0ef41Sopenharmony_ci{ 1461cb0ef41Sopenharmony_ci const bc1 = new BroadcastChannel('channel4'); 1471cb0ef41Sopenharmony_ci const bc2 = new BroadcastChannel('channel4'); 1481cb0ef41Sopenharmony_ci bc1.postMessage('some data'); 1491cb0ef41Sopenharmony_ci assert.strictEqual(receiveMessageOnPort(bc2).message, 'some data'); 1501cb0ef41Sopenharmony_ci assert.strictEqual(receiveMessageOnPort(bc2), undefined); 1511cb0ef41Sopenharmony_ci bc1.close(); 1521cb0ef41Sopenharmony_ci bc2.close(); 1531cb0ef41Sopenharmony_ci} 1541cb0ef41Sopenharmony_ci 1551cb0ef41Sopenharmony_ci{ 1561cb0ef41Sopenharmony_ci assert.throws(() => Reflect.get(BroadcastChannel.prototype, 'name', {}), { 1571cb0ef41Sopenharmony_ci code: 'ERR_INVALID_THIS', 1581cb0ef41Sopenharmony_ci }); 1591cb0ef41Sopenharmony_ci 1601cb0ef41Sopenharmony_ci [ 1611cb0ef41Sopenharmony_ci 'close', 1621cb0ef41Sopenharmony_ci 'postMessage', 1631cb0ef41Sopenharmony_ci 'ref', 1641cb0ef41Sopenharmony_ci 'unref', 1651cb0ef41Sopenharmony_ci ].forEach((i) => { 1661cb0ef41Sopenharmony_ci assert.throws(() => Reflect.apply(BroadcastChannel.prototype[i], [], {}), { 1671cb0ef41Sopenharmony_ci code: 'ERR_INVALID_THIS', 1681cb0ef41Sopenharmony_ci }); 1691cb0ef41Sopenharmony_ci }); 1701cb0ef41Sopenharmony_ci} 171