|
1 | 1 | defmodule PostgresReplicationTest do
|
2 | 2 | use ExUnit.Case
|
| 3 | + alias PostgresReplication.Plugin.Pgoutput.Decoder |
3 | 4 |
|
4 | 5 | test "handles database connection and receives WAL changes" do
|
5 | 6 | opts = %PostgresReplication{
|
@@ -27,14 +28,14 @@ defmodule PostgresReplicationTest do
|
27 | 28 | []
|
28 | 29 | )
|
29 | 30 |
|
30 |
| - assert_receive %PostgresReplication.Decoder.Messages.Begin{} |
31 |
| - assert_receive %PostgresReplication.Decoder.Messages.Relation{} |
| 31 | + assert_receive %Decoder.Messages.Begin{} |
| 32 | + assert_receive %Decoder.Messages.Relation{} |
32 | 33 |
|
33 |
| - assert_receive %PostgresReplication.Decoder.Messages.Insert{ |
| 34 | + assert_receive %Decoder.Messages.Insert{ |
34 | 35 | tuple_data: {_, "Random Text 1"}
|
35 | 36 | }
|
36 | 37 |
|
37 |
| - assert_receive %PostgresReplication.Decoder.Messages.Commit{} |
| 38 | + assert_receive %Decoder.Messages.Commit{} |
38 | 39 | end
|
39 | 40 |
|
40 | 41 | test "handles database connection, receives WAL changes and you can set plugin options" do
|
@@ -69,22 +70,80 @@ defmodule PostgresReplicationTest do
|
69 | 70 | []
|
70 | 71 | )
|
71 | 72 |
|
72 |
| - assert_receive %PostgresReplication.Decoder.Messages.Begin{} |
73 |
| - assert_receive %PostgresReplication.Decoder.Messages.Relation{} |
| 73 | + assert_receive %Decoder.Messages.Begin{} |
| 74 | + assert_receive %Decoder.Messages.Relation{} |
74 | 75 |
|
75 |
| - assert_receive %PostgresReplication.Decoder.Messages.Insert{ |
| 76 | + assert_receive %Decoder.Messages.Insert{ |
76 | 77 | tuple_data: {_, "Random Text 1"}
|
77 | 78 | }
|
78 | 79 |
|
79 |
| - assert_receive %PostgresReplication.Decoder.Messages.Commit{} |
| 80 | + assert_receive %Decoder.Messages.Commit{} |
| 81 | + end |
| 82 | + |
| 83 | + test "handles database connection, receives WAL changes and can use plugins without options" do |
| 84 | + opts = %PostgresReplication{ |
| 85 | + connection_opts: [ |
| 86 | + hostname: "localhost", |
| 87 | + username: "postgres", |
| 88 | + password: "postgres", |
| 89 | + database: "postgres", |
| 90 | + parameters: [ |
| 91 | + application_name: "PostgresReplication" |
| 92 | + ] |
| 93 | + ], |
| 94 | + table: :all, |
| 95 | + opts: [name: __MODULE__], |
| 96 | + publication_name: "test_publication", |
| 97 | + output_plugin: "test_decoding", |
| 98 | + output_plugin_options: [], |
| 99 | + handler_module: PostgresReplicationTest.TestDecodingHandler, |
| 100 | + metadata: %{pid: self()} |
| 101 | + } |
| 102 | + |
| 103 | + {:ok, conn} = Postgrex.start_link(opts.connection_opts) |
| 104 | + PostgresReplication.start_link(opts) |
| 105 | + |
| 106 | + Postgrex.query!( |
| 107 | + conn, |
| 108 | + "INSERT INTO random_values (value) VALUES ('Random Text 1')", |
| 109 | + [] |
| 110 | + ) |
| 111 | + |
| 112 | + assert_receive :ok |
| 113 | + assert_receive :ok |
| 114 | + assert_receive :ok |
| 115 | + end |
| 116 | + |
| 117 | + defmodule TestDecodingHandler do |
| 118 | + @behaviour PostgresReplication.Handler |
| 119 | + |
| 120 | + @impl true |
| 121 | + def call(<<?w, _header::192, message::binary>>, %{metadata: %{pid: pid}}) do |
| 122 | + send(pid, :ok) |
| 123 | + :noreply |
| 124 | + end |
| 125 | + |
| 126 | + # Handles keep alive messages |
| 127 | + def call(<<?k, wal_end::64, _clock::64, reply>>, _) do |
| 128 | + messages = |
| 129 | + case reply do |
| 130 | + 1 -> [<<?r, wal_end + 1::64, wal_end + 1::64, wal_end + 1::64, current_time()::64, 0>>] |
| 131 | + 0 -> [] |
| 132 | + end |
| 133 | + |
| 134 | + {:reply, messages} |
| 135 | + end |
| 136 | + |
| 137 | + @epoch DateTime.to_unix(~U[2000-01-01 00:00:00Z], :microsecond) |
| 138 | + defp current_time, do: System.os_time(:microsecond) - @epoch |
80 | 139 | end
|
81 | 140 |
|
82 | 141 | defmodule PgoutputHandler do
|
83 | 142 | @behaviour PostgresReplication.Handler
|
84 | 143 |
|
85 | 144 | @impl true
|
86 | 145 | def call(<<?w, _header::192, message::binary>>, %{metadata: %{pid: pid}}) do
|
87 |
| - message |> PostgresReplication.Decoder.decode_message() |> then(&send(pid, &1)) |
| 146 | + message |> Decoder.decode_message() |> then(&send(pid, &1)) |
88 | 147 | :noreply
|
89 | 148 | end
|
90 | 149 |
|
|
0 commit comments