epgsql_pool_SUITE.erl 9.5 KB

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