@@ -23,57 +23,69 @@ defmodule RealtimeWeb.RealtimeChannelReplicationReadyTest do
2323 assert_receive % Socket.Message { event: "system" , payload: % { message: "Replication connection established" } } , 500
2424 end
2525
26- test "pushes the system message when the syn ready broadcast arrives after join" , % { tenant: tenant } do
27- stub ( Connect , :lookup_or_start_connection , fn _ -> { :ok , self ( ) } end )
28- stub ( Connect , :replication_status , fn _ -> { :error , :not_connected } end )
26+ test "pushes the system message once replication becomes ready while polling" , % { tenant: tenant } do
27+ { :ok , counter } = Agent . start_link ( fn -> 0 end )
2928
30- assert { :ok , _ , _ } = join ( tenant )
29+ stub ( Connect , :lookup_or_start_connection , fn _ -> { :ok , self ( ) } end )
3130
32- refute_receive % Socket.Message { event: "system" , payload: % { message: "Replication connection established" } } , 200
31+ stub ( Connect , :replication_status , fn _ ->
32+ case Agent . get_and_update ( counter , fn n -> { n , n + 1 } end ) do
33+ n when n < 3 -> { :error , :not_connected }
34+ _ -> { :ok , self ( ) }
35+ end
36+ end )
3337
34- signal_ready ( tenant , self ( ) )
38+ assert { :ok , _ , _ } = join ( tenant )
3539
3640 assert_receive % Socket.Message { event: "system" , payload: % { message: "Replication connection established" } } , 500
3741 end
3842
39- test "ignores syn ready broadcasts without a replication connection " , % { tenant: tenant } do
43+ test "does not push while replication is unavailable " , % { tenant: tenant } do
4044 stub ( Connect , :lookup_or_start_connection , fn _ -> { :ok , self ( ) } end )
4145 stub ( Connect , :replication_status , fn _ -> { :error , :not_connected } end )
4246
4347 assert { :ok , _ , _ } = join ( tenant )
4448
45- signal_ready ( tenant , nil )
46-
4749 refute_receive % Socket.Message { event: "system" , payload: % { message: "Replication connection established" } } , 300
4850 end
4951
50- test "notifies at most once and stops listening after the first signal " , % { tenant: tenant } do
52+ test "notifies at most once" , % { tenant: tenant } do
5153 stub ( Connect , :lookup_or_start_connection , fn _ -> { :ok , self ( ) } end )
5254 stub ( Connect , :replication_status , fn _ -> { :ok , self ( ) } end )
5355
5456 assert { :ok , _ , _ } = join ( tenant )
5557
5658 assert_receive % Socket.Message { event: "system" , payload: % { message: "Replication connection established" } } , 500
5759
58- signal_ready ( tenant , self ( ) )
59-
6060 refute_receive % Socket.Message { event: "system" , payload: % { message: "Replication connection established" } } , 300
6161 end
6262
63+ test "stops the channel when replication is not established before the timeout" , % { tenant: tenant } do
64+ previous = Application . get_env ( :realtime , :replication_ready_timeout )
65+ Application . put_env ( :realtime , :replication_ready_timeout , 50 )
66+ on_exit ( fn -> Application . put_env ( :realtime , :replication_ready_timeout , previous ) end )
67+
68+ stub ( Connect , :lookup_or_start_connection , fn _ -> { :ok , self ( ) } end )
69+ stub ( Connect , :replication_status , fn _ -> { :error , :not_connected } end )
70+
71+ assert { :ok , _ , socket } = join ( tenant )
72+ ref = Process . monitor ( socket . channel_pid )
73+
74+ assert_receive % Socket.Message {
75+ event: "system" ,
76+ payload: % { status: "error" , message: "Replication connection was not established in time" }
77+ } ,
78+ 500
79+
80+ assert_receive { :DOWN , ^ ref , :process , _ , _ } , 500
81+ end
82+
6383 defp join ( tenant ) do
6484 jwt = generate_jwt_token ( tenant )
6585 { :ok , socket } = connect ( UserSocket , % { } , conn_opts ( tenant , jwt ) )
6686 subscribe_and_join ( socket , "realtime:test" , % { "config" => % { } } )
6787 end
6888
69- defp signal_ready ( tenant , replication_conn ) do
70- RealtimeWeb.Endpoint . local_broadcast (
71- Connect . syn_topic ( tenant . external_id ) ,
72- "ready" ,
73- % { pid: self ( ) , conn: self ( ) , replication_conn: replication_conn }
74- )
75- end
76-
7789 defp conn_opts ( tenant , token ) do
7890 [
7991 connect_info: % {
0 commit comments