epgsql_pool_SUITE.erl 9.7 KB

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