Skip to content

Commit 37dbc25

Browse files
committed
Merge pull request #1574 from pguyot/w11/fix-wasm-web-ci
Fix wasm test with worker thread Calling alert from worker thread no longer works and probably never should have. Update example accordingly. These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
2 parents 4ed8ddf + 33dda72 commit 37dbc25

File tree

3 files changed

+45
-22
lines changed

3 files changed

+45
-22
lines changed

examples/emscripten/run_script.erl

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,14 @@
2222
-export([start/0]).
2323

2424
start() ->
25-
CounterPid = spawn(fun() -> counter_loop() end),
26-
emscripten:run_script(<<"alert('hello from Erlang in main thread')">>, [main_thread]),
25+
{CounterPid, CounterMonitor} = spawn_opt(fun() -> counter_loop() end, [monitor]),
26+
emscripten:run_script(
27+
<<
28+
"console.log('hello from Erlang in main thread');"
29+
"alert('hello from Erlang in main thread')"
30+
>>,
31+
[main_thread]
32+
),
2733
CounterPid ! {self(), terminate},
2834
receive
2935
{CounterPid, CounterValue, DeltaMS} ->
@@ -40,15 +46,20 @@ start() ->
4046
]
4147
)
4248
end,
43-
emscripten:run_script(<<"alert('hello from Erlang in main thread async')">>, [
44-
main_thread, async
45-
]),
46-
emscripten:run_script(<<"alert('hello from Erlang in worker thread')">>),
49+
% Ensure process terminated
50+
receive
51+
{'DOWN', CounterMonitor, process, CounterPid, normal} -> ok
52+
end,
4753
emscripten:run_script(
48-
<<"window.document.getElementById('demo-not').style = 'display: none';">>, [
49-
main_thread
54+
<<
55+
"console.log('hello from Erlang in main thread async');"
56+
"alert('hello from Erlang in main thread async')"
57+
>>,
58+
[
59+
main_thread, async
5060
]
51-
).
61+
),
62+
emscripten:run_script(<<"console.log('hello from Erlang in worker thread')">>).
5263

5364
counter_loop() ->
5465
StartTimeMS = erlang:system_time(millisecond),

examples/emscripten/run_script.html

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,26 @@ <h1>Run script example</h1>
3030
<code>emscripten:run_script/2</code> to run Javascript from Erlang.
3131
</p>
3232

33-
<p>It displays three alerts:</p>
33+
<p>It runs four Javascript scripts:</p>
3434
<ul>
35-
<li>An alert in the main thread, synchronously</li>
36-
<li>An alert in the main thread, asynchronously</li>
37-
<li>An alert in the worker thread</li>
35+
<li>One script from the main thread, synchronously, that prints a line to the console and displays an alert</li>
36+
<li>One script from the main thread, asynchronously, that prints a line to the console and displays an alert</li>
37+
<li>One script from the worker thread that prints a line to the console</li>
38+
<li>One script that updates the DOM to display the time required for first script to run</li>
3839
</ul>
39-
<p>Second alert is likely to appear after third alert.</p>
4040

4141
<p>
4242
On non-SMP builds, it demonstrates the fact that Erlang continues to run
4343
when <code>emscripten:run_script/2</code> is called with
4444
<code>[main_thread]</code> as a counter is incremented while waiting for
45-
first alert to complete. The value is <span id="demo-counter">unset</span>
45+
first line to be printed. The value is <span id="demo-counter">unset</span>
4646
</p>
4747

4848
<p>
4949
Beyond the counter, this example also demonstrates manipulating DOM (in
5050
the main thread) using JS function.
5151
</p>
5252

53-
<p>
54-
Style of this text was <span id="demo-not">not</span> modified by Erlang.
55-
</p>
5653
<script>
5754
// Arguments are loaded using fetch API.
5855
// wasm_webserver serves under /build/ files in build subdirectory.

src/platforms/emscripten/tests/cypress/e2e/examples.spec.cy.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,38 @@ describe("emscripten hello world BEAM", () => {
5757

5858
describe("emscripten run script", () => {
5959
it("should display three alerts and update counter", () => {
60+
cy.visit("/run_script.html", {
61+
onBeforeLoad(win) {
62+
cy.stub(win.console, "log").as("consoleLog");
63+
},
64+
});
6065
const alert = cy.stub().as("alert");
6166
cy.on("window:alert", alert);
62-
cy.visit("/run_script.html");
6367
cy.get("#demo-counter").should("contain", "unset");
64-
cy.get("@alert").should(
68+
cy.get("@consoleLog").should(
6569
"have.been.calledWith",
6670
"hello from Erlang in main thread",
6771
);
6872
cy.get("@alert").should(
69-
"have.been.calledWithMatch",
70-
"hello from Erlang in worker thread",
73+
"have.been.calledWith",
74+
"hello from Erlang in main thread",
75+
);
76+
// window.console is not the console invoked by our code from worker
77+
// thread, or at least cypress stub doesn't work
78+
// cy.get("@consoleLog").should(
79+
// "have.been.calledWithMatch",
80+
// "hello from Erlang in worker thread",
81+
// );
82+
cy.get("@consoleLog").should(
83+
"have.been.calledWith",
84+
"hello from Erlang in main thread async",
7185
);
7286
cy.get("@alert").should(
7387
"have.been.calledWith",
7488
"hello from Erlang in main thread async",
7589
);
7690
cy.get("#demo-counter").should("contain", "ms");
91+
cy.get("@consoleLog").should("be.calledWith", "Return value: ok");
7792
});
7893
});
7994

0 commit comments

Comments
 (0)