epgsql_pool_SUITE.erl 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. validate_connection_params_test/1
  12. ]).
  13. -define(SELECT_ITEMS_QUERY, "SELECT id, category_id, title, num FROM item ORDER by id ASC").
  14. all() ->
  15. [query_test,
  16. transaction_test,
  17. reconnect_test,
  18. timeout_test,
  19. validate_connection_params_test
  20. ].
  21. init_per_suite(Config) ->
  22. application:ensure_all_started(epgsql_pool),
  23. Config.
  24. end_per_suite(Config) ->
  25. application:stop(epgsql_pool),
  26. Config.
  27. init_per_testcase(_, Config) ->
  28. Params = #epgsql_connection_params{host = "localhost", port = 5432, username = "test", password = "test", database = "testdb"},
  29. {ok, _} = epgsql_pool:start(my_pool, 5, 10, Params),
  30. epgsql_pool:query(my_pool, "TRUNCATE TABLE item"),
  31. epgsql_pool:query(my_pool, "TRUNCATE TABLE category CASCADE"),
  32. Config.
  33. end_per_testcase(_, Config) ->
  34. ok = epgsql_pool:stop(my_pool),
  35. Config.
  36. query_test(Config) ->
  37. {ok, 3, _, Ids} = epgsql_pool:query(my_pool,
  38. "INSERT INTO category (title) "
  39. "VALUES ('cat 1'), ('cat 2'), ('cat 3') "
  40. "RETURNING id"),
  41. WaitForRows = lists:map(fun({{Id}, Title}) -> {Id, Title} end,
  42. lists:zip(Ids, [<<"cat 1">>, <<"cat 2">>, <<"cat 3">>])),
  43. {ok, _, Rows} = epgsql_pool:query(my_pool, "SELECT id, title FROM category ORDER by id ASC"),
  44. ct:pal("Rows ~p", [Rows]),
  45. ?assertEqual(WaitForRows, Rows),
  46. [{Id1}, {Id2} | _] = Ids,
  47. {ok, _, Rows2} = epgsql_pool:query(my_pool, "SELECT id, title FROM category WHERE id = $1 OR id = $2 ORDER BY id ASC", [Id1, Id2]),
  48. ct:pal("Rows2 ~p", [Rows2]),
  49. ?assertEqual([{Id1, <<"cat 1">>}, {Id2, <<"cat 2">>}], Rows2),
  50. {error, Error} = epgsql_pool:query(my_pool, "SELECT id, title FROM some_table"),
  51. ct:pal("Error:~p", [Error]),
  52. ?assertMatch(#error{severity = error, message = <<"relation \"some_table\" does not exist">>}, Error),
  53. ok.
  54. transaction_test(Config) ->
  55. {FirstCatId, CatIds2, ItemIds2} =
  56. epgsql_pool:transaction(my_pool,
  57. fun(Worker) ->
  58. ct:pal("worker:~p", [Worker]),
  59. {ok, 3, _, CatIds0} =
  60. epgsql_pool:query(Worker,
  61. "INSERT INTO category (title) "
  62. "VALUES ('cat 4'), ('cat 5'), ('cat 6') "
  63. "RETURNING id"),
  64. CatIds1 = lists:map(fun({Cid}) -> Cid end, CatIds0),
  65. CatId = hd(CatIds1),
  66. {ok, 2, _, ItemIds0} =
  67. epgsql_pool:query(Worker,
  68. "INSERT INTO item (category_id, title, num) "
  69. "VALUES ($1, 'item 1', 5), ($1, 'item 2', 7) "
  70. "RETURNING id", [CatId]),
  71. ItemIds1 = lists:map(fun({Iid}) -> Iid end, ItemIds0),
  72. {CatId, CatIds1, ItemIds1}
  73. end),
  74. WaitForCats = lists:zip(CatIds2, [<<"cat 4">>, <<"cat 5">>, <<"cat 6">>]),
  75. {ok, _, CatRows} = epgsql_pool:query(my_pool, "SELECT id, title FROM category ORDER by id ASC"),
  76. ct:pal("CatRows ~p", [CatRows]),
  77. ?assertEqual(WaitForCats, CatRows),
  78. WaitForItems = lists:map(fun({ItemId, {Title, Num}}) -> {ItemId, FirstCatId, Title, Num} end,
  79. lists:zip(ItemIds2, [{<<"item 1">>, 5}, {<<"item 2">>, 7}])),
  80. {ok, _, ItemRows} = epgsql_pool:query(my_pool, ?SELECT_ITEMS_QUERY),
  81. ct:pal("ItemRows ~p", [ItemRows]),
  82. ?assertEqual(WaitForItems, ItemRows),
  83. try
  84. epgsql_pool:transaction(my_pool,
  85. fun(Worker) ->
  86. ct:pal("worker:~p", [Worker]),
  87. {ok, 2} =
  88. epgsql_pool:query(Worker,
  89. "INSERT INTO item (category_id, title, num) "
  90. "VALUES ($1, 'item 3', 55), ($1, 'item 4', 77) ",
  91. [FirstCatId]),
  92. {ok, _, ItemRows2} = epgsql_pool:query(Worker, ?SELECT_ITEMS_QUERY),
  93. ct:pal("ItemRows2 ~p", [ItemRows2]),
  94. ?assertMatch([{_, FirstCatId, <<"item 1">>, 5},
  95. {_, FirstCatId, <<"item 2">>, 7},
  96. {_, FirstCatId, <<"item 3">>, 55},
  97. {_, FirstCatId, <<"item 4">>, 77}],
  98. ItemRows2),
  99. throw(cancel_transaction)
  100. end)
  101. catch
  102. throw:cancel_transaction -> ok
  103. end,
  104. %% items not changes after calcelled transaction
  105. {ok, _, ItemRows} = epgsql_pool:query(my_pool, ?SELECT_ITEMS_QUERY),
  106. ok.
  107. reconnect_test(Config) ->
  108. Worker = pooler:take_member(my_pool, 1000),
  109. [state, my_pool, #epgsql_connection{sock = Sock1} | _]= tuple_to_list(sys:get_state(Worker)),
  110. ct:pal("Worker: ~p, sock: ~p", [Worker, Sock1]),
  111. R1 = epgsql_pool:query(Worker, ?SELECT_ITEMS_QUERY),
  112. ct:pal("first query ~p", [R1]),
  113. {ok, _, []} = R1,
  114. ct:pal("~p close_connection", [Sock1]),
  115. exit(Sock1, close_connection),
  116. R2 = epgsql_pool:query(Worker, "select * from item"),
  117. ct:pal("second query goes immediatelly ~p", [R2]),
  118. {error, no_connection} = R2,
  119. timer:sleep(50),
  120. R3 = epgsql_pool:query(Worker, "select * from item"),
  121. ct:pal("third query goes after 50 ms ~p", [R3]),
  122. {error, no_connection} = R3,
  123. timer:sleep(150),
  124. R4 = epgsql_pool:query(Worker, "select * from item"),
  125. ct:pal("fouth query goes after 200 ms ~p", [R4]),
  126. {ok, _, []} = R4,
  127. [state, my_pool, #epgsql_connection{sock = Sock2} | _]= tuple_to_list(sys:get_state(Worker)),
  128. ct:pal("Worker: ~p, sock: ~p", [Worker, Sock2]),
  129. ?assertNotEqual(Sock1, Sock2),
  130. ok.
  131. timeout_test(_Config) ->
  132. Res1 = epgsql_pool:query(my_pool, "SELECT pg_sleep(1)", [], [{timeout, 2000}]),
  133. ct:pal("Res1:~p", [Res1]),
  134. ?assertMatch({ok, _, _}, Res1),
  135. Res2 = epgsql_pool:query(my_pool, "SELECT pg_sleep(1)", [], [{timeout, 500}]),
  136. ct:pal("Res2:~p", [Res2]),
  137. ?assertEqual({error, timeout}, Res2),
  138. Worker = pooler:take_member(my_pool, 1000),
  139. Res3 = epgsql_pool:query(Worker, "SELECT pg_sleep(100)", [], [{timeout, 500}]),
  140. ct:pal("Res3:~p", [Res3]),
  141. ?assertEqual({error, timeout}, Res3),
  142. %% check worker and connection able to perform query
  143. Res4 = epgsql_pool:query(Worker, "SELECT * FROM item", [], [{timeout, 500}]),
  144. ct:pal("Res4:~p", [Res4]),
  145. ?assertMatch({ok, _, _}, Res4),
  146. ok.
  147. validate_connection_params_test(_Config) ->
  148. Params1 = #epgsql_connection_params{host = "localhost", port = 5432,
  149. username = "test", password = "test", database = "testdb"},
  150. Res1 = epgsql_pool:validate_connection_params(Params1),
  151. ct:pal("Res1: ~p", [Res1]),
  152. ?assertEqual(ok, Res1),
  153. Params2 = #epgsql_connection_params{host = "localhost", port = 5432,
  154. username = "test", password = "some", database = "testdb"},
  155. Res2 = epgsql_pool:validate_connection_params(Params2),
  156. ct:pal("Res2: ~p", [Res2]),
  157. ?assertEqual({error,invalid_password}, Res2),
  158. Params3 = #epgsql_connection_params{host = "localhost", port = 5432,
  159. username = "test", password = "test", database = "some"},
  160. {error, Res3} = epgsql_pool:validate_connection_params(Params3),
  161. ct:pal("Res3: ~p", [Res3]),
  162. ?assertMatch(#error{
  163. severity = fatal,
  164. code = <<"3D000">>,
  165. codename = invalid_catalog_name,
  166. message = <<"database \"some\" does not exist">>
  167. }, Res3),
  168. ok.