Skip to content

Commit

Permalink
Test cancel_async
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismccord committed Aug 6, 2023
1 parent b6a30b9 commit b75f7d8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
27 changes: 27 additions & 0 deletions test/phoenix_live_view/integrations/assign_async_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,32 @@ defmodule Phoenix.LiveView.AssignAsyncTest do
assert render_async(lv) =~ "error: {:exit, :boom}"
assert render(lv)
end

test "lv exit brings down asyncs", %{conn: conn} do
{:ok, lv, _html} = live(conn, "/async?test=lv_exit")
Process.unlink(lv.pid)
lv_ref = Process.monitor(lv.pid)
async_ref = Process.monitor(Process.whereis(:lv_exit))
send(lv.pid, :boom)

assert_receive {:DOWN, ^lv_ref, :process, _pid, :boom}
assert_receive {:DOWN, ^async_ref, :process, _pid, :boom}
end

test "cancel_async", %{conn: conn} do
{:ok, lv, _html} = live(conn, "/async?test=cancel")
Process.unlink(lv.pid)
async_ref = Process.monitor(Process.whereis(:cancel))
send(lv.pid, :cancel)

assert_receive {:DOWN, ^async_ref, :process, _pid, :killed}

assert render(lv) =~ "data canceled"

send(lv.pid, :renew_canceled)

assert render(lv) =~ "data loading..."
assert render_async(lv, 200) =~ "data: 123"
end
end
end
28 changes: 28 additions & 0 deletions test/support/live_views/general.ex
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ defmodule Phoenix.LiveViewTest.AsyncLive do
~H"""
<.live_component module={LC} id="lc" />
<div :if={@async.data.loading?}>data loading...</div>
<div :if={@async.data.canceled?}>data canceled</div>
<div :if={!@async.data.loading? && @async.data.result == nil}>no data found</div>
<div :if={!@async.data.loading? && @async.data.result}>data: <%= inspect(@async.data.result) %></div>
<div :if={err = @async.data.error}>error: <%= inspect(err) %></div>
Expand All @@ -384,4 +385,31 @@ defmodule Phoenix.LiveViewTest.AsyncLive do
def mount(%{"test" => "exit"}, _session, socket) do
{:ok, assign_async(socket, :data, fn -> exit(:boom) end)}
end

def mount(%{"test" => "lv_exit"}, _session, socket) do
{:ok,
assign_async(socket, :data, fn ->
Process.register(self(), :lv_exit)
Process.sleep(:infinity)
end)}
end

def mount(%{"test" => "cancel"}, _session, socket) do
{:ok,
assign_async(socket, :data, fn ->
Process.register(self(), :cancel)
Process.sleep(:infinity)
end)}
end

def handle_info(:boom, _socket), do: exit(:boom)

def handle_info(:cancel, socket), do: {:noreply, cancel_async(socket, :data)}

def handle_info(:renew_canceled, socket) do
{:noreply, assign_async(socket, :data, fn ->
Process.sleep(100)
{:ok, %{data: 123}}
end)}
end
end

0 comments on commit b75f7d8

Please sign in to comment.