Fix more race conditions in tests

This commit is contained in:
Jonatan Kłosko 2023-05-30 22:42:59 +02:00
parent cf3d0ee16e
commit 4145121ba9
2 changed files with 32 additions and 2 deletions

View file

@ -119,7 +119,8 @@ defmodule LivebookWeb.SessionLiveTest do
evaluate_setup(session.pid)
section_id = insert_section(session.pid)
cell_id = insert_text_cell(session.pid, section_id, :code, "Process.sleep(50)")
{source, continue_fun} = source_for_blocking()
cell_id = insert_text_cell(session.pid, section_id, :code, source)
{:ok, view, _} = live(conn, ~p"/sessions/#{session.id}")
@ -129,6 +130,8 @@ defmodule LivebookWeb.SessionLiveTest do
assert %{cell_infos: %{^cell_id => %{eval: %{status: :evaluating}}}} =
Session.get_data(session.pid)
continue_fun.()
end
test "reevaluting the setup cell", %{conn: conn, session: session} do
@ -1402,7 +1405,6 @@ defmodule LivebookWeb.SessionLiveTest do
section_id = insert_section(session.pid)
insert_cell_with_output(session.pid, section_id, {:text, "Hello from the app!"})
wait_for_session_update(session.pid)
slug = Livebook.Utils.random_short_id()
@ -1431,6 +1433,9 @@ defmodule LivebookWeb.SessionLiveTest do
|> live(~p"/apps/#{slug}")
|> follow_redirect(conn)
assert_receive {:app_updated,
%{slug: ^slug, sessions: [%{app_status: %{execution: :executed}}]}}
assert render(view) =~ "Hello from the app!"
Livebook.App.close(app.pid)

View file

@ -69,4 +69,29 @@ defmodule Livebook.TestHelpers do
end
|> Macro.to_string()
end
@doc """
Builds code that awaits for a messages before finishing.
Returns `{code, continue_fun}`, where calling `continue_fun` should
continue execution. Embedded runtime must be used for this to work.
"""
def source_for_blocking() do
name = Livebook.Utils.random_short_id() |> String.to_atom()
code =
quote do
# This test uses the Embedded runtime, so we can target the
# process by name
Process.register(self(), unquote(name))
receive do: (:stop -> :ok)
end
|> Macro.to_string()
ack_fun = fn ->
send(Process.whereis(name), :pong)
end
{code, ack_fun}
end
end