epgsql_pool_SUITE.erl 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. -module(epgsql_pool_SUITE).
  2. %% test needs connection to database
  3. %% and database should be inited with ./testdb_schema.sql
  4. -include("epgsql_pool.hrl").
  5. -include_lib("epgsql/include/epgsql.hrl").
  6. -include_lib("common_test/include/ct.hrl").
  7. -include_lib("eunit/include/eunit.hrl").
  8. -export([all/0,
  9. init_per_suite/1, end_per_suite/1, init_per_testcase/2, end_per_testcase/2,
  10. query_test/1, transaction_test/1, reconnect_test/1, timeout_test/1
  11. ]).
  12. -define(SELECT_ITEMS_QUERY, "SELECT id, category_id, title, num FROM item ORDER by id ASC").
  13. all() ->
  14. [query_test,
  15. transaction_test,
  16. reconnect_test,
  17. timeout_test
  18. ].
  19. init_per_suite(Config) ->
  20. application:ensure_all_started(epgsql_pool),
  21. Config.
  22. end_per_suite(Config) ->
  23. application:stop(epgsql_pool),
  24. Config.
  25. init_per_testcase(_, Config) ->
  26. Params = #epgsql_connection_params{host = "localhost", port = 5432, username = "test", password = "test", database = "testdb"},
  27. epgsql_pool_settings:set_connection_params(my_pool, Params),
  28. {ok, _} = epgsql_pool:start(my_pool, 5, 10),
  29. epgsql_pool:query(my_pool, "TRUNCATE TABLE item"),
  30. epgsql_pool:query(my_pool, "TRUNCATE TABLE category CASCADE"),
  31. Config.
  32. end_per_testcase(_, Config) ->
  33. ok = epgsql_pool:stop(my_pool),
  34. Config.
  35. query_test(Config) ->
  36. {ok, 3, _, Ids} = epgsql_pool:query(my_pool,
  37. "INSERT INTO category (title) "
  38. "VALUES ('cat 1'), ('cat 2'), ('cat 3') "
  39. "RETURNING id"),
  40. WaitForRows = lists:map(fun({{Id}, Title}) -> {Id, Title} end,
  41. lists:zip(Ids, [<<"cat 1">>, <<"cat 2">>, <<"cat 3">>])),
  42. {ok, _, Rows} = epgsql_pool:query(my_pool, "SELECT id, title FROM category ORDER by id ASC"),
  43. ct:pal("Rows ~p", [Rows]),
  44. ?assertEqual(WaitForRows, Rows),
  45. [{Id1}, {Id2} | _] = Ids,
  46. {ok, _, Rows2} = epgsql_pool:query(my_pool, "SELECT id, title FROM category WHERE id = $1 OR id = $2 ORDER BY id ASC", [Id1, Id2]),
  47. ct:pal("Rows2 ~p", [Rows2]),
  48. ?assertEqual([{Id1, <<"cat 1">>}, {Id2, <<"cat 2">>}], Rows2),
  49. {error, Error} = epgsql_pool:query(my_pool, "SELECT id, title FROM some_table"),
  50. ct:pal("Error:~p", [Error]),
  51. ?assertMatch(#error{severity = error, message = <<"relation \"some_table\" does not exist">>}, Error),
  52. ok.
  53. transaction_test(Config) ->
  54. {FirstCatId, CatIds2, ItemIds2} =
  55. epgsql_pool:transaction(my_pool,
  56. fun(Worker) ->
  57. ct:pal("worker:~p", [Worker]),
  58. {ok, 3, _, CatIds0} =
  59. epgsql_pool:query(Worker,
  60. "INSERT INTO category (title) "
  61. "VALUES ('cat 4'), ('cat 5'), ('cat 6') "
  62. "RETURNING id"),
  63. CatIds1 = lists:map(fun({Cid}) -> Cid end, CatIds0),
  64. CatId = hd(CatIds1),
  65. {ok, 2, _, ItemIds0} =
  66. epgsql_pool:query(Worker,
  67. "INSERT INTO item (category_id, title, num) "
  68. "VALUES ($1, 'item 1', 5), ($1, 'item 2', 7) "
  69. "RETURNING id", [CatId]),
  70. ItemIds1 = lists:map(fun({Iid}) -> Iid end, ItemIds0),
  71. {CatId, CatIds1, ItemIds1}
  72. end),
  73. WaitForCats = lists:zip(CatIds2, [<<"cat 4">>, <<"cat 5">>, <<"cat 6">>]),
  74. {ok, _, CatRows} = epgsql_pool:query(my_pool, "SELECT id, title FROM category ORDER by id ASC"),
  75. ct:pal("CatRows ~p", [CatRows]),
  76. ?assertEqual(WaitForCats, CatRows),
  77. WaitForItems = lists:map(fun({ItemId, {Title, Num}}) -> {ItemId, FirstCatId, Title, Num} end,
  78. lists:zip(ItemIds2, [{<<"item 1">>, 5}, {<<"item 2">>, 7}])),
  79. {ok, _, ItemRows} = epgsql_pool:query(my_pool, ?SELECT_ITEMS_QUERY),
  80. ct:pal("ItemRows ~p", [ItemRows]),
  81. ?assertEqual(WaitForItems, ItemRows),
  82. try
  83. epgsql_pool:transaction(my_pool,
  84. fun(Worker) ->
  85. ct:pal("worker:~p", [Worker]),
  86. {ok, 2} =
  87. epgsql_pool:query(Worker,
  88. "INSERT INTO item (category_id, title, num) "
  89. "VALUES ($1, 'item 3', 55), ($1, 'item 4', 77) ",
  90. [FirstCatId]),
  91. {ok, _, ItemRows2} = epgsql_pool:query(Worker, ?SELECT_ITEMS_QUERY),
  92. ct:pal("ItemRows2 ~p", [ItemRows2]),
  93. ?assertMatch([{_, FirstCatId, <<"item 1">>, 5},
  94. {_, FirstCatId, <<"item 2">>, 7},
  95. {_, FirstCatId, <<"item 3">>, 55},
  96. {_, FirstCatId, <<"item 4">>, 77}],
  97. ItemRows2),
  98. throw(cancel_transaction)
  99. end)
  100. catch
  101. throw:cancel_transaction -> ok
  102. end,
  103. %% items not changes after calcelled transaction
  104. {ok, _, ItemRows} = epgsql_pool:query(my_pool, ?SELECT_ITEMS_QUERY),
  105. ok.
  106. reconnect_test(Config) ->
  107. Worker = pooler:take_member(my_pool, 1000) ,
  108. [state, my_pool, #epgsql_connection{sock = Sock1} | _]= tuple_to_list(sys:get_state(Worker)),
  109. ct:pal("Worker: ~p, sock: ~p", [Worker, Sock1]),
  110. R1 = epgsql_pool:query(Worker, ?SELECT_ITEMS_QUERY),
  111. ct:pal("first query ~p", [R1]),
  112. {ok, _, []} = R1,
  113. ct:pal("~p close_connection", [Sock1]),
  114. exit(Sock1, close_connection),
  115. R2 = epgsql_pool:query(Worker, "select * from item"),
  116. ct:pal("second query goes immediatelly ~p", [R2]),
  117. {error, reconnecting} = R2,
  118. timer:sleep(50),
  119. R3 = epgsql_pool:query(Worker, "select * from item"),
  120. ct:pal("third query goes after 50 ms ~p", [R3]),
  121. {error, reconnecting} = R3,
  122. timer:sleep(150),
  123. R4 = epgsql_pool:query(Worker, "select * from item"),
  124. ct:pal("fouth query goes after 200 ms ~p", [R4]),
  125. {ok, _, []} = R4,
  126. [state, my_pool, #epgsql_connection{sock = Sock2} | _]= tuple_to_list(sys:get_state(Worker)),
  127. ct:pal("Worker: ~p, sock: ~p", [Worker, Sock2]),
  128. ?assertNotEqual(Sock1, Sock2),
  129. ok.
  130. timeout_test(_Config) ->
  131. ok.