Skip to content

Commit e6d06bd

Browse files
committed
Update the Phonenic pubsub tests to check if pubsub is subscribed before some operations, rather than sleeping. Also add a helper to assert with retries.
1 parent 5f0dab1 commit e6d06bd

File tree

2 files changed

+78
-22
lines changed

2 files changed

+78
-22
lines changed

test/fun_with_flags/notifications/phoenix_pubsub_test.exs

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ defmodule FunWithFlags.Notifications.PhoenixPubSubTest do
4444

4545
describe "publish_change(flag_name)" do
4646
setup do
47+
wait_until_pubsub_is_ready!()
48+
4749
{:ok, name: unique_atom()}
4850
end
4951

@@ -59,15 +61,16 @@ defmodule FunWithFlags.Notifications.PhoenixPubSubTest do
5961
{Phoenix.PubSub, [:passthrough], []}
6062
]) do
6163
assert {:ok, _pid} = PubSub.publish_change(name)
62-
:timer.sleep(10)
6364

64-
assert called(
65-
Phoenix.PubSub.broadcast!(
66-
:fwf_test,
67-
"fun_with_flags_changes",
68-
{:fwf_changes, {:updated, name, u_id}}
65+
assert_with_retries(fn ->
66+
assert called(
67+
Phoenix.PubSub.broadcast!(
68+
:fwf_test,
69+
"fun_with_flags_changes",
70+
{:fwf_changes, {:updated, name, u_id}}
71+
)
6972
)
70-
)
73+
end)
7174
end
7275
end
7376

@@ -100,14 +103,16 @@ defmodule FunWithFlags.Notifications.PhoenixPubSubTest do
100103
channel = "fun_with_flags_changes"
101104
message = {:fwf_changes, {:updated, :foobar, u_id}}
102105

106+
wait_until_pubsub_is_ready!()
107+
103108
with_mock(PubSub, [:passthrough], []) do
104109
Phoenix.PubSub.broadcast!(client, channel, message)
105110

106-
:timer.sleep(1)
107-
108-
assert called(
109-
PubSub.handle_info(message, u_id)
110-
)
111+
assert_with_retries(fn ->
112+
assert called(
113+
PubSub.handle_info(message, {u_id, :subscribed})
114+
)
115+
end)
111116
end
112117
end
113118

@@ -122,10 +127,14 @@ defmodule FunWithFlags.Notifications.PhoenixPubSubTest do
122127
channel = "fun_with_flags_changes"
123128
message = {:fwf_changes, {:updated, :a_flag_name, u_id}}
124129

130+
wait_until_pubsub_is_ready!()
131+
125132
with_mock(Store, [:passthrough], []) do
126133
Phoenix.PubSub.broadcast!(client, channel, message)
127-
:timer.sleep(30)
128-
refute called(Store.reload(:a_flag_name))
134+
135+
assert_with_retries(fn ->
136+
refute called(Store.reload(:a_flag_name))
137+
end)
129138
end
130139
end
131140

@@ -138,10 +147,14 @@ defmodule FunWithFlags.Notifications.PhoenixPubSubTest do
138147
channel = "fun_with_flags_changes"
139148
message = {:fwf_changes, {:updated, :a_flag_name, another_u_id}}
140149

150+
wait_until_pubsub_is_ready!()
151+
141152
with_mock(Store, [:passthrough], []) do
142153
Phoenix.PubSub.broadcast!(client, channel, message)
143-
:timer.sleep(30)
144-
assert called(Store.reload(:a_flag_name))
154+
155+
assert_with_retries(fn ->
156+
assert called(Store.reload(:a_flag_name))
157+
end)
145158
end
146159
end
147160
end
@@ -160,12 +173,15 @@ defmodule FunWithFlags.Notifications.PhoenixPubSubTest do
160173
cached_flag = %Flag{name: name, gates: [gate2]}
161174

162175
{:ok, ^stored_flag} = Config.persistence_adapter.put(name, gate)
163-
:timer.sleep(10)
164-
{:ok, ^cached_flag} = Cache.put(cached_flag)
176+
assert_with_retries(fn ->
177+
{:ok, ^cached_flag} = Cache.put(cached_flag)
178+
end)
165179

166180
assert {:ok, ^stored_flag} = Config.persistence_adapter.get(name)
167181
assert {:ok, ^cached_flag} = Cache.get(name)
168182

183+
wait_until_pubsub_is_ready!()
184+
169185
{:ok, name: name, stored_flag: stored_flag, cached_flag: cached_flag}
170186
end
171187

@@ -183,8 +199,10 @@ defmodule FunWithFlags.Notifications.PhoenixPubSubTest do
183199
message = {:fwf_changes, {:updated, name, u_id}}
184200

185201
Phoenix.PubSub.broadcast!(client, channel, message)
186-
:timer.sleep(30)
187-
assert {:ok, ^cached_flag} = Cache.get(name)
202+
203+
assert_with_retries(fn ->
204+
assert {:ok, ^cached_flag} = Cache.get(name)
205+
end)
188206
end
189207

190208

@@ -198,8 +216,10 @@ defmodule FunWithFlags.Notifications.PhoenixPubSubTest do
198216

199217
assert {:ok, ^cached_flag} = Cache.get(name)
200218
Phoenix.PubSub.broadcast!(client, channel, message)
201-
:timer.sleep(30)
202-
assert {:ok, ^stored_flag} = Cache.get(name)
219+
220+
assert_with_retries(fn ->
221+
assert {:ok, ^stored_flag} = Cache.get(name)
222+
end)
203223
end
204224
end
205225
end

test/support/test_utils.ex

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,40 @@ defmodule FunWithFlags.TestUtils do
8787
def on_elixir_15? do
8888
Version.match?(System.version, ">= 1.15.0")
8989
end
90+
91+
def phx_pubsub_ready? do
92+
Process.whereis(FunWithFlags.Notifications.PhoenixPubSub) &&
93+
FunWithFlags.Notifications.PhoenixPubSub.subscribed?
94+
end
95+
96+
def wait_until_pubsub_is_ready!(attempts \\ 20, wait_time_ms \\ 25)
97+
98+
def wait_until_pubsub_is_ready!(attempts, wait_time_ms) when attempts > 0 do
99+
case phx_pubsub_ready?() do
100+
true ->
101+
:ok
102+
_ ->
103+
:timer.sleep(wait_time_ms)
104+
wait_until_pubsub_is_ready!(attempts - 1, wait_time_ms)
105+
end
106+
end
107+
108+
def wait_until_pubsub_is_ready!(_, _) do
109+
raise "Phoenix PubSub is never ready, giving up"
110+
end
111+
112+
def assert_with_retries(attempts \\ 30, wait_time_ms \\ 25, test_fn) do
113+
try do
114+
test_fn.()
115+
rescue
116+
e ->
117+
if attempts == 1 do
118+
reraise e, __STACKTRACE__
119+
else
120+
IO.write("|")
121+
:timer.sleep(wait_time_ms)
122+
assert_with_retries(attempts - 1, wait_time_ms, test_fn)
123+
end
124+
end
125+
end
90126
end

0 commit comments

Comments
 (0)