From f58f9609d23e0aa893d550a07112a19397d21ce1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20K=C5=82osko?= Date: Mon, 28 Feb 2022 22:27:39 +0100 Subject: [PATCH] Fix race conditions in tests (#1031) * Fix race conditions in tests * Format * Increase session import redirect timeout * Increase intellisense timeout --- test/livebook_web/live/home_live_test.exs | 10 ++++----- test/livebook_web/live/session_live_test.exs | 22 ++++++++++++++++---- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/test/livebook_web/live/home_live_test.exs b/test/livebook_web/live/home_live_test.exs index 09ca6e52a..529dca8b7 100644 --- a/test/livebook_web/live/home_live_test.exs +++ b/test/livebook_web/live/home_live_test.exs @@ -245,8 +245,6 @@ defmodule LivebookWeb.HomeLiveTest do describe "notebook import" do test "allows importing notebook directly from content", %{conn: conn} do - Phoenix.PubSub.subscribe(Livebook.PubSub, "tracker_sessions") - {:ok, view, _} = live(conn, "/home/import/content") notebook_content = """ @@ -257,9 +255,9 @@ defmodule LivebookWeb.HomeLiveTest do |> element("form", "Import") |> render_submit(%{data: %{content: notebook_content}}) - assert_receive {:session_created, %{id: id, notebook_name: "My notebook"}} + {path, _flash} = assert_redirect(view, 1000) - {:ok, view, _} = live(conn, "/sessions/#{id}") + {:ok, view, _} = live(conn, path) assert render(view) =~ "My notebook" end @@ -274,7 +272,7 @@ defmodule LivebookWeb.HomeLiveTest do |> element("form", "Import") |> render_submit(%{data: %{content: notebook_content}}) - {_path, flash} = assert_redirect(view) + {_path, flash} = assert_redirect(view, 1000) assert flash["info"] =~ "You have imported a notebook, no code has been executed so far. You should read and evaluate code as needed." @@ -294,7 +292,7 @@ defmodule LivebookWeb.HomeLiveTest do |> element("form", "Import") |> render_submit(%{data: %{content: notebook_content}}) - {_path, flash} = assert_redirect(view) + {_path, flash} = assert_redirect(view, 1000) assert flash["warning"] =~ "We found problems while importing the file and tried to autofix them:\n- Downgrading all headings, because 3 instances of heading 1 were found" diff --git a/test/livebook_web/live/session_live_test.exs b/test/livebook_web/live/session_live_test.exs index 844f2583c..f95e6e1d8 100644 --- a/test/livebook_web/live/session_live_test.exs +++ b/test/livebook_web/live/session_live_test.exs @@ -222,6 +222,8 @@ defmodule LivebookWeb.SessionLiveTest do destination: test } + Phoenix.PubSub.subscribe(Livebook.PubSub, "sessions:#{session.id}") + insert_cell_with_output(session.pid, section_id, {:input, input}) {:ok, view, _} = live(conn, "/sessions/#{session.id}") @@ -249,6 +251,8 @@ defmodule LivebookWeb.SessionLiveTest do destination: test } + Phoenix.PubSub.subscribe(Livebook.PubSub, "sessions:#{session.id}") + insert_cell_with_output(session.pid, section_id, {:input, input}) {:ok, view, _} = live(conn, "/sessions/#{session.id}") @@ -285,6 +289,8 @@ defmodule LivebookWeb.SessionLiveTest do reset_on_submit: [] } + Phoenix.PubSub.subscribe(Livebook.PubSub, "sessions:#{session.id}") + insert_cell_with_output(session.pid, section_id, {:control, form_control}) {:ok, view, _} = live(conn, "/sessions/#{session.id}") @@ -320,6 +326,8 @@ defmodule LivebookWeb.SessionLiveTest do send(session.pid, {:runtime_evaluation_output, cell_id, {:stdout, "line 2\n"}}) wait_for_session_update(session.pid) + # Render once, so that stdout send_update is processed + _ = render(view) assert render(view) =~ "line 2" end @@ -514,10 +522,15 @@ defmodule LivebookWeb.SessionLiveTest do assert_reply view, %{"ref" => ref} assert ref != nil - assert_push_event(view, "intellisense_response", %{ - "ref" => ^ref, - "response" => %{items: [%{label: "version/0"}]} - }) + assert_push_event( + view, + "intellisense_response", + %{ + "ref" => ^ref, + "response" => %{items: [%{label: "version/0"}]} + }, + 1000 + ) end end @@ -906,6 +919,7 @@ defmodule LivebookWeb.SessionLiveTest do cell_id = insert_text_cell(session_pid, section_id, :elixir, code) Session.queue_cell_evaluation(session_pid, cell_id) + assert_receive {:operation, {:add_cell_evaluation_response, _, ^cell_id, _, _}} cell_id end