|
| 1 | +defmodule Realtime.Integration.ReplicationsTest do |
| 2 | + use Realtime.DataCase, async: false |
| 3 | + |
| 4 | + alias Extensions.PostgresCdcRls.Replications |
| 5 | + alias Extensions.PostgresCdcRls.Subscriptions |
| 6 | + alias Realtime.Database |
| 7 | + |
| 8 | + @publication "supabase_realtime_test" |
| 9 | + @poll_interval 100 |
| 10 | + |
| 11 | + setup do |
| 12 | + tenant = Containers.checkout_tenant(run_migrations: true) |
| 13 | + |
| 14 | + {:ok, conn} = |
| 15 | + tenant |
| 16 | + |> Database.from_tenant("realtime_rls") |
| 17 | + |> Map.from_struct() |
| 18 | + |> Keyword.new() |
| 19 | + |> Postgrex.start_link() |
| 20 | + |
| 21 | + slot_name = "supabase_realtime_test_slot_#{System.unique_integer([:positive])}" |
| 22 | + |
| 23 | + on_exit(fn -> |
| 24 | + try do |
| 25 | + Postgrex.query(conn, "select pg_drop_replication_slot($1)", [slot_name]) |
| 26 | + catch |
| 27 | + _, _ -> :ok |
| 28 | + end |
| 29 | + end) |
| 30 | + |
| 31 | + {:ok, subscription_params} = Subscriptions.parse_subscription_params(%{"event" => "*", "schema" => "public"}) |
| 32 | + params_list = [%{claims: %{"role" => "anon"}, id: UUID.uuid1(), subscription_params: subscription_params}] |
| 33 | + {:ok, _} = Subscriptions.create(conn, @publication, params_list, self(), self()) |
| 34 | + {:ok, _} = Replications.prepare_replication(conn, slot_name) |
| 35 | + |
| 36 | + # Drain any setup changes |
| 37 | + Replications.list_changes(conn, slot_name, @publication, 100, 1_048_576) |
| 38 | + |
| 39 | + %{conn: conn, slot_name: slot_name} |
| 40 | + end |
| 41 | + |
| 42 | + describe "replication polling lifecycle" do |
| 43 | + test "prepare, poll, consume full cycle", %{conn: conn, slot_name: slot_name} do |
| 44 | + # Empty slot short-circuits via peek |
| 45 | + {time, result} = |
| 46 | + :timer.tc(fn -> |
| 47 | + Replications.list_changes(conn, slot_name, @publication, 100, 1_048_576) |
| 48 | + end) |
| 49 | + |
| 50 | + assert {:ok, %Postgrex.Result{num_rows: 0}} = result |
| 51 | + assert time < 50_000, "Expected peek short-circuit under 50ms, took #{div(time, 1000)}ms" |
| 52 | + |
| 53 | + Process.sleep(@poll_interval) |
| 54 | + |
| 55 | + Postgrex.query!(conn, "INSERT INTO public.test (details) VALUES ('row_1')", []) |
| 56 | + Postgrex.query!(conn, "INSERT INTO public.test (details) VALUES ('row_2')", []) |
| 57 | + Postgrex.query!(conn, "INSERT INTO public.test (details) VALUES ('row_3')", []) |
| 58 | + |
| 59 | + Process.sleep(@poll_interval) |
| 60 | + |
| 61 | + {:ok, %Postgrex.Result{num_rows: 3, rows: rows}} = |
| 62 | + Replications.list_changes(conn, slot_name, @publication, 100, 1_048_576) |
| 63 | + |
| 64 | + [row | _] = rows |
| 65 | + assert Enum.at(row, 0) == "INSERT" |
| 66 | + assert Enum.at(row, 1) == "public" |
| 67 | + assert Enum.at(row, 2) == "test" |
| 68 | + |
| 69 | + Process.sleep(@poll_interval) |
| 70 | + |
| 71 | + {:ok, %Postgrex.Result{num_rows: 0}} = |
| 72 | + Replications.list_changes(conn, slot_name, @publication, 100, 1_048_576) |
| 73 | + end |
| 74 | + |
| 75 | + test "polls empty multiple times then captures a change when it arrives", %{conn: conn, slot_name: slot_name} do |
| 76 | + for _ <- 1..5 do |
| 77 | + {:ok, %Postgrex.Result{num_rows: 0}} = |
| 78 | + Replications.list_changes(conn, slot_name, @publication, 100, 1_048_576) |
| 79 | + |
| 80 | + Process.sleep(@poll_interval) |
| 81 | + end |
| 82 | + |
| 83 | + Postgrex.query!(conn, "INSERT INTO public.test (details) VALUES ('delayed_arrival')", []) |
| 84 | + Process.sleep(@poll_interval) |
| 85 | + |
| 86 | + {:ok, %Postgrex.Result{num_rows: 1, rows: [row]}} = |
| 87 | + Replications.list_changes(conn, slot_name, @publication, 100, 1_048_576) |
| 88 | + |
| 89 | + assert Enum.at(row, 0) == "INSERT" |
| 90 | + assert Enum.at(row, 1) == "public" |
| 91 | + assert Enum.at(row, 2) == "test" |
| 92 | + |
| 93 | + Process.sleep(@poll_interval) |
| 94 | + |
| 95 | + {:ok, %Postgrex.Result{num_rows: 0}} = |
| 96 | + Replications.list_changes(conn, slot_name, @publication, 100, 1_048_576) |
| 97 | + end |
| 98 | + |
| 99 | + test "prepare_replication is idempotent", %{conn: conn, slot_name: slot_name} do |
| 100 | + {:ok, _} = Replications.prepare_replication(conn, slot_name) |
| 101 | + Process.sleep(@poll_interval) |
| 102 | + {:ok, _} = Replications.prepare_replication(conn, slot_name) |
| 103 | + end |
| 104 | + |
| 105 | + test "terminate_backend returns slot_not_found for unknown slots", %{conn: conn} do |
| 106 | + assert {:error, :slot_not_found} = |
| 107 | + Replications.terminate_backend(conn, "nonexistent_slot_#{System.unique_integer([:positive])}") |
| 108 | + end |
| 109 | + |
| 110 | + test "get_pg_stat_activity_diff returns elapsed seconds for active connection", %{conn: conn} do |
| 111 | + {:ok, %Postgrex.Result{rows: [[pid]]}} = Postgrex.query(conn, "SELECT pg_backend_pid()", []) |
| 112 | + Postgrex.query!(conn, "SET application_name = 'realtime_rls'", []) |
| 113 | + Process.sleep(@poll_interval) |
| 114 | + |
| 115 | + assert {:ok, diff} = Replications.get_pg_stat_activity_diff(conn, pid) |
| 116 | + assert is_integer(diff) |
| 117 | + end |
| 118 | + end |
| 119 | +end |
0 commit comments