Skip to content

Commit 8765d24

Browse files
committed
Fix flaky async tests relying on monitor
This is a continuation of 0f9cc64. Calling Process.monitor/1 is an asynchronous operation. It could happen that by the time the monitor was actually set up, the process was already stopped, leading to noproc messages. So while the previous commit ensured we don't monitor too early, this commit ensures we don't monitor too late.
1 parent af2c40f commit 8765d24

File tree

6 files changed

+39
-95
lines changed

6 files changed

+39
-95
lines changed

test/phoenix_live_view/integrations/assign_async_test.exs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ defmodule Phoenix.LiveView.AssignAsyncTest do
33
import Phoenix.ConnTest
44

55
import Phoenix.LiveViewTest
6+
import Phoenix.LiveViewTest.Support.AsyncSync
67
alias Phoenix.LiveViewTest.Support.Endpoint
78

89
@endpoint Endpoint
@@ -55,11 +56,7 @@ defmodule Phoenix.LiveView.AssignAsyncTest do
5556
{:ok, lv, _html} = live(conn, "/assign_async?test=lv_exit")
5657
lv_ref = Process.monitor(lv.pid)
5758

58-
receive do
59-
:async_ready -> :ok
60-
end
61-
62-
async_ref = Process.monitor(Process.whereis(:lv_exit))
59+
async_ref = wait_for_async_ready_and_monitor(:lv_exit)
6360
send(lv.pid, :boom)
6461

6562
assert_receive {:DOWN, ^lv_ref, :process, _pid, :boom}, 1000
@@ -70,11 +67,7 @@ defmodule Phoenix.LiveView.AssignAsyncTest do
7067
Process.register(self(), :assign_async_test_process)
7168
{:ok, lv, _html} = live(conn, "/assign_async?test=cancel")
7269

73-
receive do
74-
:async_ready -> :ok
75-
end
76-
77-
async_ref = Process.monitor(Process.whereis(:cancel))
70+
async_ref = wait_for_async_ready_and_monitor(:cancel)
7871
send(lv.pid, :cancel)
7972

8073
assert_receive {:DOWN, ^async_ref, :process, _pid, {:shutdown, :cancel}}, 1000
@@ -189,11 +182,7 @@ defmodule Phoenix.LiveView.AssignAsyncTest do
189182
{:ok, lv, _html} = live(conn, "/assign_async?test=lc_lv_exit")
190183
lv_ref = Process.monitor(lv.pid)
191184

192-
receive do
193-
:async_ready -> :ok
194-
end
195-
196-
async_ref = Process.monitor(Process.whereis(:lc_exit))
185+
async_ref = wait_for_async_ready_and_monitor(:lc_exit)
197186
send(lv.pid, :boom)
198187

199188
assert_receive {:DOWN, ^lv_ref, :process, _pid, :boom}, 1000
@@ -204,11 +193,7 @@ defmodule Phoenix.LiveView.AssignAsyncTest do
204193
Process.register(self(), :assign_async_test_process)
205194
{:ok, lv, _html} = live(conn, "/assign_async?test=lc_cancel")
206195

207-
receive do
208-
:async_ready -> :ok
209-
end
210-
211-
async_ref = Process.monitor(Process.whereis(:lc_cancel))
196+
async_ref = wait_for_async_ready_and_monitor(:lc_cancel)
212197

213198
Phoenix.LiveView.send_update(lv.pid, Phoenix.LiveViewTest.Support.AssignAsyncLive.LC,
214199
id: "lc",

test/phoenix_live_view/integrations/start_async_test.exs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ defmodule Phoenix.LiveView.StartAsyncTest do
33
import Phoenix.ConnTest
44

55
import Phoenix.LiveViewTest
6+
import Phoenix.LiveViewTest.Support.AsyncSync
67
alias Phoenix.LiveViewTest.Support.Endpoint
78

89
@endpoint Endpoint
@@ -38,11 +39,7 @@ defmodule Phoenix.LiveView.StartAsyncTest do
3839
{:ok, lv, _html} = live(conn, "/start_async?test=lv_exit")
3940
lv_ref = Process.monitor(lv.pid)
4041

41-
receive do
42-
:async_ready -> :ok
43-
end
44-
45-
async_ref = Process.monitor(Process.whereis(:start_async_exit))
42+
async_ref = wait_for_async_ready_and_monitor(:start_async_exit)
4643
send(lv.pid, :boom)
4744

4845
assert_receive {:DOWN, ^lv_ref, :process, _pid, :boom}, 1000
@@ -53,11 +50,7 @@ defmodule Phoenix.LiveView.StartAsyncTest do
5350
Process.register(self(), :start_async_test_process)
5451
{:ok, lv, _html} = live(conn, "/start_async?test=cancel")
5552

56-
receive do
57-
:async_ready -> :ok
58-
end
59-
60-
async_ref = Process.monitor(Process.whereis(:start_async_cancel))
53+
async_ref = wait_for_async_ready_and_monitor(:start_async_cancel)
6154

6255
assert render(lv) =~ "result: :loading"
6356

@@ -139,11 +132,7 @@ defmodule Phoenix.LiveView.StartAsyncTest do
139132
{:ok, lv, _html} = live(conn, "/start_async?test=lc_lv_exit")
140133
lv_ref = Process.monitor(lv.pid)
141134

142-
receive do
143-
:async_ready -> :ok
144-
end
145-
146-
async_ref = Process.monitor(Process.whereis(:start_async_exit))
135+
async_ref = wait_for_async_ready_and_monitor(:start_async_exit)
147136
send(lv.pid, :boom)
148137

149138
assert_receive {:DOWN, ^lv_ref, :process, _pid, :boom}, 1000
@@ -154,11 +143,7 @@ defmodule Phoenix.LiveView.StartAsyncTest do
154143
Process.register(self(), :start_async_test_process)
155144
{:ok, lv, _html} = live(conn, "/start_async?test=lc_cancel")
156145

157-
receive do
158-
:async_ready -> :ok
159-
end
160-
161-
async_ref = Process.monitor(Process.whereis(:start_async_cancel))
146+
async_ref = wait_for_async_ready_and_monitor(:start_async_cancel)
162147

163148
Phoenix.LiveView.send_update(lv.pid, Phoenix.LiveViewTest.Support.StartAsyncLive.LC,
164149
id: "lc",

test/phoenix_live_view/integrations/stream_async_test.exs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ defmodule Phoenix.LiveView.StreamAsyncTest do
33
import Phoenix.ConnTest
44

55
import Phoenix.LiveViewTest
6+
import Phoenix.LiveViewTest.Support.AsyncSync
67
alias Phoenix.LiveViewTest.Support.Endpoint
78

89
@endpoint Endpoint
@@ -101,11 +102,7 @@ defmodule Phoenix.LiveView.StreamAsyncTest do
101102
{:ok, lv, _html} = live(conn, "/stream_async?test=lv_exit")
102103
lv_ref = Process.monitor(lv.pid)
103104

104-
receive do
105-
:async_ready -> :ok
106-
end
107-
108-
async_ref = Process.monitor(Process.whereis(:stream_async_exit))
105+
async_ref = wait_for_async_ready_and_monitor(:stream_async_exit)
109106
send(lv.pid, :boom)
110107

111108
assert_receive {:DOWN, ^lv_ref, :process, _pid, :boom}, 1000
@@ -116,11 +113,7 @@ defmodule Phoenix.LiveView.StreamAsyncTest do
116113
Process.register(self(), :stream_async_test_process)
117114
{:ok, lv, _html} = live(conn, "/stream_async?test=cancel")
118115

119-
receive do
120-
:async_ready -> :ok
121-
end
122-
123-
async_ref = Process.monitor(Process.whereis(:cancel_stream))
116+
async_ref = wait_for_async_ready_and_monitor(:cancel_stream)
124117
send(lv.pid, :cancel)
125118
assert_receive {:DOWN, ^async_ref, :process, _pid, {:shutdown, :cancel}}, 1000
126119

@@ -233,11 +226,7 @@ defmodule Phoenix.LiveView.StreamAsyncTest do
233226
{:ok, lv, _html} = live(conn, "/stream_async?test=lc_lv_exit")
234227
lv_ref = Process.monitor(lv.pid)
235228

236-
receive do
237-
:async_ready -> :ok
238-
end
239-
240-
async_ref = Process.monitor(Process.whereis(:lc_stream_exit))
229+
async_ref = wait_for_async_ready_and_monitor(:lc_stream_exit)
241230
send(lv.pid, :boom)
242231

243232
assert_receive {:DOWN, ^lv_ref, :process, _pid, :boom}, 1000
@@ -248,11 +237,8 @@ defmodule Phoenix.LiveView.StreamAsyncTest do
248237
Process.register(self(), :stream_async_test_process)
249238
{:ok, lv, _html} = live(conn, "/stream_async?test=lc_cancel")
250239

251-
receive do
252-
:async_ready -> :ok
253-
end
240+
async_ref = wait_for_async_ready_and_monitor(:lc_stream_cancel)
254241

255-
async_ref = Process.monitor(Process.whereis(:lc_stream_cancel))
256242
# Send cancel to the LiveView, which will forward to the component
257243
send(lv.pid, {:cancel_lc, "lc"})
258244

test/support/live_views/assign_async.ex

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
defmodule Phoenix.LiveViewTest.Support.AssignAsyncLive do
22
use Phoenix.LiveView
33

4+
import Phoenix.LiveViewTest.Support.AsyncSync
5+
46
on_mount({__MODULE__, :defaults})
57

68
def on_mount(:defaults, _params, _session, socket) do
@@ -66,18 +68,14 @@ defmodule Phoenix.LiveViewTest.Support.AssignAsyncLive do
6668
def mount(%{"test" => "lv_exit"}, _session, socket) do
6769
{:ok,
6870
assign_async(socket, :data, fn ->
69-
Process.register(self(), :lv_exit)
70-
send(:assign_async_test_process, :async_ready)
71-
Process.sleep(:infinity)
71+
register_and_sleep(:assign_async_test_process, :lv_exit)
7272
end)}
7373
end
7474

7575
def mount(%{"test" => "cancel"}, _session, socket) do
7676
{:ok,
7777
assign_async(socket, :data, fn ->
78-
Process.register(self(), :cancel)
79-
send(:assign_async_test_process, :async_ready)
80-
Process.sleep(:infinity)
78+
register_and_sleep(:assign_async_test_process, :cancel)
8179
end)}
8280
end
8381

@@ -126,6 +124,8 @@ end
126124
defmodule Phoenix.LiveViewTest.Support.AssignAsyncLive.LC do
127125
use Phoenix.LiveComponent
128126

127+
import Phoenix.LiveViewTest.Support.AsyncSync
128+
129129
def render(assigns) do
130130
~H"""
131131
<div>
@@ -168,18 +168,14 @@ defmodule Phoenix.LiveViewTest.Support.AssignAsyncLive.LC do
168168
def update(%{test: "lv_exit"}, socket) do
169169
{:ok,
170170
assign_async(socket, [:lc_data, :other_data], fn ->
171-
Process.register(self(), :lc_exit)
172-
send(:assign_async_test_process, :async_ready)
173-
Process.sleep(:infinity)
171+
register_and_sleep(:assign_async_test_process, :lc_exit)
174172
end)}
175173
end
176174

177175
def update(%{test: "cancel"}, socket) do
178176
{:ok,
179177
assign_async(socket, [:lc_data, :other_data], fn ->
180-
Process.register(self(), :lc_cancel)
181-
send(:assign_async_test_process, :async_ready)
182-
Process.sleep(:infinity)
178+
register_and_sleep(:assign_async_test_process, :lc_cancel)
183179
end)}
184180
end
185181

test/support/live_views/start_async.ex

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
defmodule Phoenix.LiveViewTest.Support.StartAsyncLive do
22
use Phoenix.LiveView
33

4+
import Phoenix.LiveViewTest.Support.AsyncSync
5+
46
on_mount({__MODULE__, :defaults})
57

68
def on_mount(:defaults, _params, _session, socket) do
@@ -51,9 +53,7 @@ defmodule Phoenix.LiveViewTest.Support.StartAsyncLive do
5153
socket
5254
|> assign(result: :loading)
5355
|> start_async(:result_task, fn ->
54-
Process.register(self(), :start_async_exit)
55-
send(:start_async_test_process, :async_ready)
56-
Process.sleep(:infinity)
56+
register_and_sleep(:start_async_test_process, :start_async_exit)
5757
end)}
5858
end
5959

@@ -62,9 +62,7 @@ defmodule Phoenix.LiveViewTest.Support.StartAsyncLive do
6262
socket
6363
|> assign(result: :loading)
6464
|> start_async(:result_task, fn ->
65-
Process.register(self(), :start_async_cancel)
66-
send(:start_async_test_process, :async_ready)
67-
Process.sleep(:infinity)
65+
register_and_sleep(:start_async_test_process, :start_async_cancel)
6866
end)}
6967
end
7068

@@ -191,6 +189,8 @@ end
191189
defmodule Phoenix.LiveViewTest.Support.StartAsyncLive.LC do
192190
use Phoenix.LiveComponent
193191

192+
import Phoenix.LiveViewTest.Support.AsyncSync
193+
194194
def render(assigns) do
195195
~H"""
196196
<div>
@@ -225,9 +225,7 @@ defmodule Phoenix.LiveViewTest.Support.StartAsyncLive.LC do
225225
socket
226226
|> assign(result: :loading)
227227
|> start_async(:result_task, fn ->
228-
Process.register(self(), :start_async_exit)
229-
send(:start_async_test_process, :async_ready)
230-
Process.sleep(:infinity)
228+
register_and_sleep(:start_async_test_process, :start_async_exit)
231229
end)}
232230
end
233231

@@ -236,9 +234,7 @@ defmodule Phoenix.LiveViewTest.Support.StartAsyncLive.LC do
236234
socket
237235
|> assign(result: :loading)
238236
|> start_async(:result_task, fn ->
239-
Process.register(self(), :start_async_cancel)
240-
send(:start_async_test_process, :async_ready)
241-
Process.sleep(:infinity)
237+
register_and_sleep(:start_async_test_process, :start_async_cancel)
242238
end)}
243239
end
244240

test/support/live_views/stream_async.ex

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
defmodule Phoenix.LiveViewTest.Support.StreamAsyncLive do
22
use Phoenix.LiveView
33

4+
import Phoenix.LiveViewTest.Support.AsyncSync
5+
46
on_mount({__MODULE__, :defaults})
57

68
def on_mount(:defaults, params, _session, socket) do
@@ -86,18 +88,14 @@ defmodule Phoenix.LiveViewTest.Support.StreamAsyncLive do
8688
def mount(%{"test" => "lv_exit"}, _session, socket) do
8789
{:ok,
8890
stream_async(socket, :my_stream, fn ->
89-
Process.register(self(), :stream_async_exit)
90-
send(:stream_async_test_process, :async_ready)
91-
Process.sleep(:infinity)
91+
register_and_sleep(:stream_async_test_process, :stream_async_exit)
9292
end)}
9393
end
9494

9595
def mount(%{"test" => "cancel"}, _session, socket) do
9696
{:ok,
9797
stream_async(socket, :my_stream, fn ->
98-
Process.register(self(), :cancel_stream)
99-
send(:stream_async_test_process, :async_ready)
100-
Process.sleep(:infinity)
98+
register_and_sleep(:stream_async_test_process, :cancel_stream)
10199
end)}
102100
end
103101

@@ -158,6 +156,8 @@ end
158156
defmodule Phoenix.LiveViewTest.Support.StreamAsyncLive.LC do
159157
use Phoenix.LiveComponent
160158

159+
import Phoenix.LiveViewTest.Support.AsyncSync
160+
161161
def render(assigns) do
162162
~H"""
163163
<div>
@@ -199,18 +199,14 @@ defmodule Phoenix.LiveViewTest.Support.StreamAsyncLive.LC do
199199
def update(%{test: "lv_exit"}, socket) do
200200
{:ok,
201201
stream_async(socket, :lc_stream, fn ->
202-
Process.register(self(), :lc_stream_exit)
203-
send(:stream_async_test_process, :async_ready)
204-
Process.sleep(:infinity)
202+
register_and_sleep(:stream_async_test_process, :lc_stream_exit)
205203
end)}
206204
end
207205

208206
def update(%{test: "cancel"}, socket) do
209207
{:ok,
210208
stream_async(socket, :lc_stream, fn ->
211-
Process.register(self(), :lc_stream_cancel)
212-
send(:stream_async_test_process, :async_ready)
213-
Process.sleep(:infinity)
209+
register_and_sleep(:stream_async_test_process, :lc_stream_cancel)
214210
end)}
215211
end
216212

0 commit comments

Comments
 (0)