epgsql_replication_SUITE.erl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. -module(epgsql_replication_SUITE).
  2. -include_lib("eunit/include/eunit.hrl").
  3. -include_lib("common_test/include/ct.hrl").
  4. -include("epgsql.hrl").
  5. -export([
  6. init_per_suite/1,
  7. all/0,
  8. end_per_suite/1,
  9. connect_in_repl_mode/1,
  10. create_drop_replication_slot/1,
  11. replication_sync/1,
  12. replication_async/1,
  13. %% Callbacks
  14. handle_x_log_data/4
  15. ]).
  16. init_per_suite(Config) ->
  17. [{module, epgsql}|Config].
  18. end_per_suite(_Config) ->
  19. ok.
  20. all() ->
  21. [
  22. connect_in_repl_mode,
  23. create_drop_replication_slot,
  24. replication_async,
  25. replication_sync
  26. ].
  27. connect_in_repl_mode(Config) ->
  28. epgsql_ct:connect_only(Config, ["epgsql_test_replication",
  29. "epgsql_test_replication",
  30. [{database, "epgsql_test_db1"}, {replication, "database"}]]).
  31. create_drop_replication_slot(Config) ->
  32. Module = ?config(module, Config),
  33. epgsql_ct:with_connection(
  34. Config,
  35. fun(C) ->
  36. {ok, Cols, Rows} = Module:squery(C, "CREATE_REPLICATION_SLOT ""epgsql_test"" LOGICAL ""test_decoding"""),
  37. [#column{name = <<"slot_name">>}, #column{name = <<"consistent_point">>},
  38. #column{name = <<"snapshot_name">>}, #column{name = <<"output_plugin">>}] = Cols,
  39. [{<<"epgsql_test">>, _, _, <<"test_decoding">>}] = Rows,
  40. [{ok, _, _}, {ok, _, _}] = Module:squery(C, "DROP_REPLICATION_SLOT ""epgsql_test""")
  41. end,
  42. "epgsql_test_replication",
  43. [{replication, "database"}]).
  44. replication_async(Config) ->
  45. replication_test_run(Config, self()).
  46. replication_sync(Config) ->
  47. replication_test_run(Config, ?MODULE).
  48. replication_test_run(Config, Callback) ->
  49. Module = ?config(module, Config),
  50. epgsql_ct:with_connection(
  51. Config,
  52. fun(C) ->
  53. {ok, _, _} = Module:squery(C, "CREATE_REPLICATION_SLOT ""epgsql_test"" LOGICAL ""test_decoding"""),
  54. %% new connection because main id in a replication mode
  55. epgsql_ct:with_connection(
  56. Config,
  57. fun(C2) ->
  58. [{ok, 1},{ok, 1}] = Module:squery(C2,
  59. "insert into test_table1 (id, value) values (5, 'five');delete from test_table1 where id = 5;")
  60. end),
  61. Module:start_replication(C, "epgsql_test", Callback, {C, self()}, "0/0"),
  62. ok = receive_replication_msgs(
  63. [<<"table public.test_table1: INSERT: id[integer]:5 value[text]:'five'">>,
  64. <<"table public.test_table1: DELETE: id[integer]:5">>], C, [])
  65. end,
  66. "epgsql_test_replication",
  67. [{replication, "database"}]),
  68. %% cleanup
  69. epgsql_ct:with_connection(
  70. Config,
  71. fun(C) ->
  72. [{ok, _, _}, {ok, _, _}] = Module:squery(C, "DROP_REPLICATION_SLOT ""epgsql_test""")
  73. end,
  74. "epgsql_test_replication",
  75. [{replication, "database"}]).
  76. receive_replication_msgs(Pattern, Pid, ReceivedMsgs) ->
  77. receive
  78. {epgsql, Pid, {x_log_data, _StartLSN, _EndLSN, <<"BEGIN", _/binary>>}} ->
  79. receive_replication_msgs(Pattern, Pid, [begin_msg | ReceivedMsgs]);
  80. {epgsql, Pid, {x_log_data, _StartLSN, _EndLSN, <<"COMMIT", _/binary>>}} ->
  81. case lists:reverse(ReceivedMsgs) of
  82. [begin_msg, row_msg | _] -> ok;
  83. _ -> error_replication_messages_not_received
  84. end;
  85. {epgsql, Pid, {x_log_data, _StartLSN, _EndLSN, Msg}} ->
  86. [Msg | T] = Pattern,
  87. receive_replication_msgs(T, Pid, [row_msg | ReceivedMsgs])
  88. after
  89. 60000 ->
  90. error_timeout
  91. end.
  92. handle_x_log_data(StartLSN, EndLSN, Data, CbState) ->
  93. {C, Pid} = CbState,
  94. Pid ! {epgsql, C, {x_log_data, StartLSN, EndLSN, Data}},
  95. {ok, EndLSN, EndLSN, CbState}.