@@ -22,9 +22,8 @@ defmodule Realtime.Tenants.ReplicationConnectionTest do
2222 { :ok , db_conn } = Database . connect ( tenant , "realtime_test" , :stop )
2323 name = "supabase_realtime_messages_replication_slot_test"
2424 Postgrex . query ( db_conn , "SELECT pg_drop_replication_slot($1)" , [ name ] )
25- Process . exit ( db_conn , :normal )
2625
27- % { tenant: tenant }
26+ % { tenant: tenant , db_conn: db_conn }
2827 end
2928
3029 describe "temporary process" do
@@ -70,7 +69,7 @@ defmodule Realtime.Tenants.ReplicationConnectionTest do
7069 assert { :error , _ } = ReplicationConnection . start ( tenant , self ( ) )
7170 end
7271
73- test "starts a handler for the tenant and broadcasts" , % { tenant: tenant } do
72+ test "starts a handler for the tenant and broadcasts" , % { tenant: tenant , db_conn: db_conn } do
7473 start_link_supervised! (
7574 { ReplicationConnection , % ReplicationConnection { tenant_id: tenant . external_id , monitored_pid: self ( ) } } ,
7675 restart: :transient
@@ -100,7 +99,6 @@ defmodule Realtime.Tenants.ReplicationConnectionTest do
10099 "event" => "INSERT" ,
101100 "meta" => % { "id" => row . id } ,
102101 "payload" => % {
103- "id" => row . id ,
104102 "value" => value
105103 } ,
106104 "type" => "broadcast"
@@ -122,7 +120,6 @@ defmodule Realtime.Tenants.ReplicationConnectionTest do
122120 } )
123121 end
124122
125- { :ok , db_conn } = Database . connect ( tenant , "realtime_test" , :stop )
126123 { :ok , _ } = Realtime.Repo . insert_all_entries ( db_conn , messages , Message )
127124
128125 messages_received =
@@ -140,9 +137,8 @@ defmodule Realtime.Tenants.ReplicationConnectionTest do
140137 "event" => "broadcast" ,
141138 "payload" => % {
142139 "event" => "INSERT" ,
143- "meta" => % { "id" => id } ,
140+ "meta" => % { "id" => _id } ,
144141 "payload" => % {
145- "id" => id ,
146142 "value" => ^ value
147143 }
148144 } ,
@@ -231,7 +227,7 @@ defmodule Realtime.Tenants.ReplicationConnectionTest do
231227 assert logs =~ "UnableToBroadcastChanges: %{messages: [%{payload: [\" Payload size exceeds tenant limit\" ]}]}"
232228 end
233229
234- test "payload without id" , % { tenant: tenant } do
230+ test "payload without id" , % { tenant: tenant , db_conn: db_conn } do
235231 start_link_supervised! (
236232 { ReplicationConnection , % ReplicationConnection { tenant_id: tenant . external_id , monitored_pid: self ( ) } } ,
237233 restart: :transient
@@ -241,15 +237,16 @@ defmodule Realtime.Tenants.ReplicationConnectionTest do
241237 tenant_topic = Tenants . tenant_topic ( tenant . external_id , topic , false )
242238 subscribe ( tenant_topic , topic )
243239
244- fixture =
245- message_fixture ( tenant , % {
246- "topic" => topic ,
247- "private" => true ,
248- "event" => "INSERT" ,
249- "payload" => % { "value" => "something" }
250- } )
240+ value = "something"
241+ event = "INSERT"
242+
243+ Postgrex . query! (
244+ db_conn ,
245+ "SELECT realtime.send (json_build_object ('value', $1 :: text)::jsonb, $2 :: text, $3 :: text, TRUE::bool);" ,
246+ [ value , event , topic ]
247+ )
251248
252- fixture_id = fixture . id
249+ { :ok , [ % { id: id } ] } = Repo . all ( db_conn , from ( m in Message ) , Message )
253250
254251 assert_receive { :socket_push , :text , data } , 500
255252 message = data |> IO . iodata_to_binary ( ) |> Jason . decode! ( )
@@ -258,7 +255,7 @@ defmodule Realtime.Tenants.ReplicationConnectionTest do
258255 "event" => "broadcast" ,
259256 "payload" => % {
260257 "event" => "INSERT" ,
261- "meta" => % { "id" => ^ fixture_id } ,
258+ "meta" => % { "id" => ^ id } ,
262259 "payload" => payload ,
263260 "type" => "broadcast"
264261 } ,
@@ -268,11 +265,11 @@ defmodule Realtime.Tenants.ReplicationConnectionTest do
268265
269266 assert payload == % {
270267 "value" => "something" ,
271- "id" => fixture_id
268+ "id" => id
272269 }
273270 end
274271
275- test "payload including id" , % { tenant: tenant } do
272+ test "payload including id" , % { tenant: tenant , db_conn: db_conn } do
276273 start_link_supervised! (
277274 { ReplicationConnection , % ReplicationConnection { tenant_id: tenant . external_id , monitored_pid: self ( ) } } ,
278275 restart: :transient
@@ -282,25 +279,27 @@ defmodule Realtime.Tenants.ReplicationConnectionTest do
282279 tenant_topic = Tenants . tenant_topic ( tenant . external_id , topic , false )
283280 subscribe ( tenant_topic , topic )
284281
285- payload = % { "value" => "something" , "id" => "123456" }
282+ id = "123456"
283+ value = "something"
284+ event = "INSERT"
286285
287- % { id: fixture_id } =
288- message_fixture ( tenant , % {
289- "topic" => topic ,
290- "private" => true ,
291- "event" => "INSERT" ,
292- "payload" => payload
293- } )
286+ Postgrex . query! (
287+ db_conn ,
288+ "SELECT realtime.send (json_build_object ('value', $1 :: text, 'id', $2 :: text)::jsonb, $3 :: text, $4 :: text, TRUE::bool);" ,
289+ [ value , id , event , topic ]
290+ )
291+
292+ { :ok , [ % { id: message_id } ] } = Repo . all ( db_conn , from ( m in Message ) , Message )
294293
295294 assert_receive { :socket_push , :text , data } , 500
296295 message = data |> IO . iodata_to_binary ( ) |> Jason . decode! ( )
297296
298297 assert % {
299298 "event" => "broadcast" ,
300299 "payload" => % {
301- "meta" => % { "id" => ^ fixture_id } ,
300+ "meta" => % { "id" => ^ message_id } ,
302301 "event" => "INSERT" ,
303- "payload" => ^ payload ,
302+ "payload" => % { "value" => "something" , "id" => ^ id } ,
304303 "type" => "broadcast"
305304 } ,
306305 "ref" => nil ,
0 commit comments