3 <script src=
"/resources/testharness.js"></script>
4 <script src=
"/resources/testharnessreport.js"></script>
8 let c1
= new BroadcastChannel('worker');
9 let c2
= new BroadcastChannel('worker');
12 c1
.onmessage
= e
=> events
.push(e
);
13 c2
.onmessage
= e
=> events
.push(e
);
16 c2
.addEventListener('message', t
.step_func(e
=> {
17 if (e
.data
== 'from worker') {
18 c2
.postMessage('from c2');
19 c1
.postMessage('done');
20 } else if (e
.data
== 'done') {
21 assert_equals(events
.length
, 4);
22 assert_equals(events
[0].data
, 'from worker');
23 assert_equals(events
[0].target
, c1
);
24 assert_equals(events
[1].data
, 'from worker');
25 assert_equals(events
[1].target
, c2
);
26 assert_equals(events
[2].data
, 'from c2');
27 assert_equals(events
[3].data
, 'done');
28 if (++doneCount
== 2) t
.done();
32 let worker
= new Worker('resources/worker.js');
33 worker
.onmessage
= t
.step_func(e
=> {
34 assert_array_equals(e
.data
, ['from c2', 'done']);
35 if (++doneCount
== 2) t
.done();
37 worker
.postMessage({channel
: 'worker'});
39 }, 'BroadcastChannel works in workers');
42 let c1
= new BroadcastChannel('shared worker');
43 let c2
= new BroadcastChannel('shared worker');
46 c1
.onmessage
= e
=> events
.push(e
);
47 c2
.onmessage
= e
=> events
.push(e
);
50 c2
.addEventListener('message', t
.step_func(e
=> {
51 if (e
.data
== 'from worker') {
52 c2
.postMessage('from c2');
53 c1
.postMessage('done');
54 } else if (e
.data
== 'done') {
55 assert_equals(events
.length
, 4);
56 assert_equals(events
[0].data
, 'from worker');
57 assert_equals(events
[0].target
, c1
);
58 assert_equals(events
[1].data
, 'from worker');
59 assert_equals(events
[1].target
, c2
);
60 assert_equals(events
[2].data
, 'from c2');
61 assert_equals(events
[3].data
, 'done');
62 if (++doneCount
== 2) t
.done();
66 let worker
= new SharedWorker('resources/worker.js');
67 worker
.port
.onmessage
= t
.step_func(e
=> {
68 assert_array_equals(e
.data
, ['from c2', 'done']);
69 if (++doneCount
== 2) t
.done();
71 worker
.port
.postMessage({channel
: 'shared worker'});
73 }, 'BroadcastChannel works in shared workers');
76 let c
= new BroadcastChannel('worker-close');
79 c
.onmessage
= e
=> events
.push('c1: ' + e
.data
);
81 let worker
= new Worker('resources/worker.js');
82 worker
.onmessage
= t
.step_func(e
=> {
83 assert_array_equals(events
,
84 ['c1: from worker', 'c2: ready', 'c2: echo'],
85 'messages in document');
86 assert_array_equals(e
.data
, ['done'], 'messages in worker');
89 worker
.onmessagerror
=
90 t
.unreached_func('Worker\'s onmessageerror handler called');
92 c
.addEventListener('message', e
=> {
93 if (e
.data
== 'from worker') {
95 if (self
.gc
) self
.gc();
96 window
.setTimeout(() => {
97 let c2
= new BroadcastChannel('worker-close');
99 events
.push('c2: ' + e
.data
);
100 if (e
.data
=== 'ready') {
101 worker
.postMessage({ping
: 'echo'});
103 c2
.postMessage('done');
107 // For some implementations there may be a race condition between
108 // when the BroadcastChannel instance above is created / ready to
109 // receive messages and when the worker calls postMessage on it's
110 // BroadcastChannel instance. To avoid this, confirm that our
111 // instance can receive a message before indicating to the other
112 // thread that we are ready. For more details, see:
113 // https://github.com/whatwg/html/issues/7267
114 let c3
= new BroadcastChannel('worker-close');
115 c3
.postMessage('ready');
121 worker
.postMessage({channel
: 'worker-close'});
122 t
.add_cleanup(() => worker
.terminate());
124 }, 'Closing and re-opening a channel works.');