Skip to content

Commit

Permalink
Remove race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jan 23, 2023
1 parent df01d7c commit 8f9d560
Show file tree
Hide file tree
Showing 21 changed files with 19 additions and 131 deletions.
6 changes: 0 additions & 6 deletions test/phoenix_live_view/integrations/assigns_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,9 @@ defmodule Phoenix.LiveView.AssignsTest do

import Phoenix.LiveViewTest
alias Phoenix.LiveViewTest.Endpoint
alias Phoenix.LiveView.LiveReloadTestHelpers, as: Helpers

@endpoint Endpoint

setup_all do
Helpers.start_endpoint(@endpoint)
:ok
end

setup do
{:ok, conn: Plug.Test.init_test_session(Phoenix.ConnTest.build_conn(), %{})}
end
Expand Down
6 changes: 0 additions & 6 deletions test/phoenix_live_view/integrations/collocated_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@ defmodule Phoenix.LiveView.CollocatedTest do

import Phoenix.LiveViewTest
alias Phoenix.LiveViewTest.{Endpoint, CollocatedLive, CollocatedComponent}
alias Phoenix.LiveView.LiveReloadTestHelpers, as: Helpers

@endpoint Endpoint

setup_all do
Helpers.start_endpoint(@endpoint)
:ok
end

test "supports collocated views" do
{:ok, view, html} = live_isolated(build_conn(), CollocatedLive)
assert html =~ "Hello collocated world from live!\n</div>"
Expand Down
6 changes: 0 additions & 6 deletions test/phoenix_live_view/integrations/connect_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@ defmodule Phoenix.LiveView.ConnectTest do
use ExUnit.Case, async: false
import Phoenix.LiveViewTest
import Phoenix.ConnTest
alias Phoenix.LiveView.LiveReloadTestHelpers, as: Helpers

@endpoint Phoenix.LiveViewTest.Endpoint

setup_all do
Helpers.start_endpoint(@endpoint)
:ok
end

describe "connect_params" do
test "can be read on mount" do
{:ok, live, _html} =
Expand Down
6 changes: 0 additions & 6 deletions test/phoenix_live_view/integrations/elements_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ defmodule Phoenix.LiveView.ElementsTest do

import Phoenix.LiveViewTest
alias Phoenix.LiveViewTest.{Endpoint}
alias Phoenix.LiveView.LiveReloadTestHelpers, as: Helpers

@endpoint Endpoint

Expand All @@ -16,11 +15,6 @@ defmodule Phoenix.LiveView.ElementsTest do
view |> element("#component-last-event") |> render() |> HtmlEntities.decode()
end

setup_all do
Helpers.start_endpoint(@endpoint)
:ok
end

setup do
conn = Phoenix.ConnTest.build_conn()
{:ok, live, _} = live(conn, "/elements")
Expand Down
6 changes: 0 additions & 6 deletions test/phoenix_live_view/integrations/event_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,9 @@ defmodule Phoenix.LiveView.EventTest do

alias Phoenix.{Component, LiveView}
alias Phoenix.LiveViewTest.{Endpoint}
alias Phoenix.LiveView.LiveReloadTestHelpers, as: Helpers

@endpoint Endpoint

setup_all do
Helpers.start_endpoint(@endpoint)
:ok
end

setup config do
{:ok, conn: Plug.Test.init_test_session(build_conn(), config[:session] || %{})}
end
Expand Down
6 changes: 0 additions & 6 deletions test/phoenix_live_view/integrations/flash_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,9 @@ defmodule Phoenix.LiveView.FlashIntegrationTest do
import Phoenix.LiveViewTest
alias Phoenix.LiveView
alias Phoenix.LiveViewTest.{Endpoint, Router}
alias Phoenix.LiveView.LiveReloadTestHelpers, as: Helpers

@endpoint Endpoint

setup_all do
Helpers.start_endpoint(@endpoint)
:ok
end

setup do
conn =
Phoenix.ConnTest.build_conn(:get, "http://www.example.com/", nil)
Expand Down
6 changes: 0 additions & 6 deletions test/phoenix_live_view/integrations/hooks_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,9 @@ defmodule Phoenix.LiveView.HooksTest do

alias Phoenix.Component
alias Phoenix.LiveViewTest.{Endpoint, HooksLive}
alias Phoenix.LiveView.LiveReloadTestHelpers, as: Helpers

@endpoint Endpoint

setup_all do
Helpers.start_endpoint(@endpoint)
:ok
end

setup do
{:ok, conn: Plug.Test.init_test_session(build_conn(), %{})}
end
Expand Down
6 changes: 0 additions & 6 deletions test/phoenix_live_view/integrations/layout_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@ defmodule Phoenix.LiveView.LayoutTest do

import Phoenix.LiveViewTest
alias Phoenix.LiveViewTest.{Endpoint, LayoutView}
alias Phoenix.LiveView.LiveReloadTestHelpers, as: Helpers

@endpoint Endpoint

setup_all do
Helpers.start_endpoint(@endpoint)
:ok
end

setup config do
{:ok,
conn: Plug.Test.init_test_session(Phoenix.ConnTest.build_conn(), config[:session] || %{})}
Expand Down
6 changes: 0 additions & 6 deletions test/phoenix_live_view/integrations/live_components_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@ defmodule Phoenix.LiveView.LiveComponentsTest do

import Phoenix.LiveViewTest
alias Phoenix.LiveViewTest.{Endpoint, DOM, StatefulComponent}
alias Phoenix.LiveView.LiveReloadTestHelpers, as: Helpers

@endpoint Endpoint
@moduletag session: %{names: ["chris", "jose"], from: nil}

setup_all do
Helpers.start_endpoint(@endpoint)
:ok
end

setup config do
{:ok,
conn: Plug.Test.init_test_session(Phoenix.ConnTest.build_conn(), config[:session] || %{})}
Expand Down
28 changes: 17 additions & 11 deletions test/phoenix_live_view/integrations/live_reload_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,45 @@ defmodule Phoenix.LiveView.LiveReloadTest do

alias Phoenix.LiveReloader
alias Phoenix.LiveReloader.Channel
alias Phoenix.LiveView.LiveReloadTestHelpers, as: Helpers

@endpoint Helpers.Endpoint
@endpoint Phoenix.LiveView.LiveReloadTestHelpers.Endpoint
@pubsub Phoenix.LiveView.PubSub

setup config do
setup_all do
ExUnit.CaptureLog.capture_log(fn ->
start_supervised!(@endpoint)

{:ok, _} =
Supervisor.start_link([Phoenix.PubSub.child_spec(name: @pubsub)],
strategy: :one_for_one
)
start_supervised!(Phoenix.PubSub.child_spec(name: @pubsub))
end)

:ok
end

setup config do
{:ok, _, socket} =
LiveReloader.Socket |> socket() |> subscribe_and_join(Channel, "phoenix:live_reload", %{})

conn = Plug.Test.init_test_session(build_conn(), config[:session] || %{})
{:ok, conn: conn, socket: socket}
end

test "LiveView renders again when the phoenix_live_reload is received", %{conn: conn, socket: socket} do
test "LiveView renders again when the phoenix_live_reload is received", %{
conn: conn,
socket: socket
} do
Phoenix.PubSub.subscribe(@pubsub, "live_view")

Application.put_env(:phoenix_live_view, :vsn, 1)
{:ok, lv, _html} = live(conn, "/live-reload")
assert render(lv) =~ "<div>Version 1</div>"

send(socket.channel_pid, {:file_event, self(), {"lib/test_auth_web/live/user_live.ex", :created}})
send(
socket.channel_pid,
{:file_event, self(), {"lib/test_auth_web/live/user_live.ex", :created}}
)

Application.put_env(:phoenix_live_view, :vsn, 2)

assert_receive {:phoenix_live_reload, :live_view, "lib/test_auth_web/live/user_live.ex"}
assert render(lv) =~ "<div>Version 2</div>"
end

end
6 changes: 0 additions & 6 deletions test/phoenix_live_view/integrations/live_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,9 @@ defmodule Phoenix.LiveView.LiveViewTest do
alias Phoenix.HTML
alias Phoenix.LiveView
alias Phoenix.LiveViewTest.{Endpoint, DOM}
alias Phoenix.LiveView.LiveReloadTestHelpers, as: Helpers

@endpoint Endpoint

setup_all do
Helpers.start_endpoint(@endpoint)
:ok
end

setup config do
{:ok, conn: Plug.Test.init_test_session(build_conn(), config[:session] || %{})}
end
Expand Down
6 changes: 0 additions & 6 deletions test/phoenix_live_view/integrations/navigation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,13 @@ defmodule Phoenix.LiveView.NavigationTest do
import Phoenix.LiveViewTest

alias Phoenix.LiveViewTest.{Endpoint, DOM}
alias Phoenix.LiveView.LiveReloadTestHelpers, as: Helpers

@endpoint Endpoint

setup do
{:ok, conn: Plug.Test.init_test_session(build_conn(), %{})}
end

setup_all do
Helpers.start_endpoint(@endpoint)
:ok
end

# Nested used of navigation helpers go to nested_test.exs

describe "push_navigate" do
Expand Down
6 changes: 0 additions & 6 deletions test/phoenix_live_view/integrations/nested_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,9 @@ defmodule Phoenix.LiveView.NestedTest do

alias Phoenix.LiveView
alias Phoenix.LiveViewTest.{Endpoint, DOM, ClockLive, ClockControlsLive, LiveInComponent}
alias Phoenix.LiveView.LiveReloadTestHelpers, as: Helpers

@endpoint Endpoint

setup_all do
Helpers.start_endpoint(@endpoint)
:ok
end

setup config do
{:ok, conn: Plug.Test.init_test_session(build_conn(), config[:session] || %{})}
end
Expand Down
5 changes: 0 additions & 5 deletions test/phoenix_live_view/integrations/params_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ defmodule Phoenix.LiveView.ParamsTest do

@endpoint Endpoint

setup_all do
ExUnit.CaptureLog.capture_log(fn -> Endpoint.start_link() end)
:ok
end

setup do
conn =
Phoenix.ConnTest.build_conn(:get, "http://www.example.com/", nil)
Expand Down
6 changes: 0 additions & 6 deletions test/phoenix_live_view/integrations/telemetry_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,10 @@ defmodule Phoenix.LiveView.TelemtryTest do

alias Phoenix.LiveView.Socket
alias Phoenix.LiveViewTest.Endpoint
alias Phoenix.LiveView.LiveReloadTestHelpers, as: Helpers

@endpoint Endpoint
@moduletag session: %{names: ["chris", "jose"], from: nil}

setup_all do
Helpers.start_endpoint(@endpoint)
:ok
end

setup config do
{:ok, conn: Plug.Test.init_test_session(build_conn(), config[:session] || %{})}
end
Expand Down
6 changes: 0 additions & 6 deletions test/phoenix_live_view/integrations/update_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@ defmodule Phoenix.LiveView.UpdateTest do

import Phoenix.LiveViewTest
alias Phoenix.LiveViewTest.{Endpoint, DOM}
alias Phoenix.LiveView.LiveReloadTestHelpers, as: Helpers

@endpoint Endpoint

setup_all do
Helpers.start_endpoint(@endpoint)
:ok
end

setup config do
{:ok,
conn: Plug.Test.init_test_session(Phoenix.ConnTest.build_conn(), config[:session] || %{})}
Expand Down
6 changes: 0 additions & 6 deletions test/phoenix_live_view/router_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ defmodule Phoenix.LiveView.RouterTest do
alias Phoenix.LiveView.{Route, Session}
alias Phoenix.LiveViewTest.{Endpoint, DashboardLive, DOM}
alias Phoenix.LiveViewTest.Router.Helpers, as: Routes
alias Phoenix.LiveView.LiveReloadTestHelpers, as: Helpers

@endpoint Endpoint

Expand All @@ -19,11 +18,6 @@ defmodule Phoenix.LiveView.RouterTest do
live_session.session
end

setup_all do
Helpers.start_endpoint(@endpoint)
:ok
end

setup config do
conn = Plug.Test.init_test_session(build_conn(), config[:plug_session] || %{})
{:ok, conn: conn}
Expand Down
9 changes: 1 addition & 8 deletions test/phoenix_live_view/upload/channel_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,7 @@ defmodule Phoenix.LiveView.UploadChannelTest do
end

setup_all do
ExUnit.CaptureLog.capture_log(fn ->
start_supervised!(@endpoint)
{:ok, _} =
Supervisor.start_link([Phoenix.PubSub.child_spec(name: Phoenix.LiveView.PubSub)],
strategy: :one_for_one
)
end)

start_supervised!(Phoenix.PubSub.child_spec(name: Phoenix.LiveView.PubSub))
:ok
end

Expand Down
6 changes: 0 additions & 6 deletions test/phoenix_live_view/upload/external_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ defmodule Phoenix.LiveView.UploadExternalTest do

alias Phoenix.LiveView
alias Phoenix.LiveViewTest.UploadLive
alias Phoenix.LiveView.LiveReloadTestHelpers, as: Helpers

def inspect_html_safe(term) do
term
Expand All @@ -27,11 +26,6 @@ defmodule Phoenix.LiveView.UploadExternalTest do
{:ok, lv}
end

setup_all do
Helpers.start_endpoint(@endpoint)
:ok
end

setup %{allow: opts} do
external = Keyword.fetch!(opts, :external)

Expand Down
11 changes: 0 additions & 11 deletions test/support/live_reload_test_helpers.ex
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
defmodule Phoenix.LiveView.LiveReloadTestHelpers do

import ExUnit.Callbacks, only: [start_supervised!: 1]

def start_endpoint(endpoint \\ Phoenix.LiveView.LiveReloadTestHelpers.Endpoint) do
ExUnit.CaptureLog.capture_log(fn ->
_pid = start_supervised!(endpoint)
end)
end
end

defmodule Phoenix.LiveView.LiveReloadTestHelpers.Endpoint do
use Phoenix.Endpoint, otp_app: :phoenix_live_view

Expand Down
1 change: 1 addition & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
after_verify_exclude =
if Version.match?(System.version(), ">= 1.14.0-dev"), do: [], else: [:after_verify]

{:ok, _} = Phoenix.LiveViewTest.Endpoint.start_link()
ExUnit.start(exclude: after_verify_exclude)

0 comments on commit 8f9d560

Please sign in to comment.