@@ -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
205225end
0 commit comments