epgsql_pool_SUITE.erl 11 KB

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