|
@@ -59,16 +59,16 @@ query_test(Config) ->
|
|
WaitForRows = lists:map(fun({{Id}, Title}) -> {Id, Title} end,
|
|
WaitForRows = lists:map(fun({{Id}, Title}) -> {Id, Title} end,
|
|
lists:zip(Ids, [<<"cat 1">>, <<"cat 2">>, <<"cat 3">>])),
|
|
lists:zip(Ids, [<<"cat 1">>, <<"cat 2">>, <<"cat 3">>])),
|
|
{ok, _, Rows} = epgsql_pool:query(my_pool, "SELECT id, title FROM category ORDER by id ASC"),
|
|
{ok, _, Rows} = epgsql_pool:query(my_pool, "SELECT id, title FROM category ORDER by id ASC"),
|
|
- ct:pal("Rows ~p", [Rows]),
|
|
|
|
|
|
+ ct:log("Rows ~p", [Rows]),
|
|
?assertEqual(WaitForRows, Rows),
|
|
?assertEqual(WaitForRows, Rows),
|
|
|
|
|
|
[{Id1}, {Id2} | _] = Ids,
|
|
[{Id1}, {Id2} | _] = Ids,
|
|
{ok, _, Rows2} = epgsql_pool:query(my_pool, "SELECT id, title FROM category WHERE id = $1 OR id = $2 ORDER BY id ASC", [Id1, Id2]),
|
|
{ok, _, Rows2} = epgsql_pool:query(my_pool, "SELECT id, title FROM category WHERE id = $1 OR id = $2 ORDER BY id ASC", [Id1, Id2]),
|
|
- ct:pal("Rows2 ~p", [Rows2]),
|
|
|
|
|
|
+ ct:log("Rows2 ~p", [Rows2]),
|
|
?assertEqual([{Id1, <<"cat 1">>}, {Id2, <<"cat 2">>}], Rows2),
|
|
?assertEqual([{Id1, <<"cat 1">>}, {Id2, <<"cat 2">>}], Rows2),
|
|
|
|
|
|
{error, Error} = epgsql_pool:query(my_pool, "SELECT id, title FROM some_table"),
|
|
{error, Error} = epgsql_pool:query(my_pool, "SELECT id, title FROM some_table"),
|
|
- ct:pal("Error:~p", [Error]),
|
|
|
|
|
|
+ ct:log("Error:~p", [Error]),
|
|
?assertMatch(#error{severity = error, message = <<"relation \"some_table\" does not exist">>}, Error),
|
|
?assertMatch(#error{severity = error, message = <<"relation \"some_table\" does not exist">>}, Error),
|
|
ok.
|
|
ok.
|
|
|
|
|
|
@@ -80,7 +80,7 @@ squery_test(Config) ->
|
|
WaitForRows = lists:map(fun({{Id}, Title}) -> {Id, Title} end,
|
|
WaitForRows = lists:map(fun({{Id}, Title}) -> {Id, Title} end,
|
|
lists:zip(Ids, [<<"cat 1">>, <<"cat 2">>, <<"cat 3">>])),
|
|
lists:zip(Ids, [<<"cat 1">>, <<"cat 2">>, <<"cat 3">>])),
|
|
{ok, _, Rows} = epgsql_pool:squery(my_pool, "SELECT id, title FROM category ORDER by id ASC"),
|
|
{ok, _, Rows} = epgsql_pool:squery(my_pool, "SELECT id, title FROM category ORDER by id ASC"),
|
|
- ct:pal("Rows ~p", [Rows]),
|
|
|
|
|
|
+ ct:log("Rows ~p", [Rows]),
|
|
?assertEqual(WaitForRows, Rows),
|
|
?assertEqual(WaitForRows, Rows),
|
|
|
|
|
|
?assertMatch([{ok, _, [{<<"1">>}]}, {ok, _, [{<<"2">>}]}],
|
|
?assertMatch([{ok, _, [{<<"1">>}]}, {ok, _, [{<<"2">>}]}],
|
|
@@ -94,7 +94,7 @@ transaction_test(Config) ->
|
|
{FirstCatId, CatIds2, ItemIds2} =
|
|
{FirstCatId, CatIds2, ItemIds2} =
|
|
epgsql_pool:transaction(my_pool,
|
|
epgsql_pool:transaction(my_pool,
|
|
fun(Worker) ->
|
|
fun(Worker) ->
|
|
- ct:pal("worker:~p", [Worker]),
|
|
|
|
|
|
+ ct:log("worker:~p", [Worker]),
|
|
{ok, 3, _, CatIds0} =
|
|
{ok, 3, _, CatIds0} =
|
|
epgsql_pool:query(Worker,
|
|
epgsql_pool:query(Worker,
|
|
"INSERT INTO category (title) "
|
|
"INSERT INTO category (title) "
|
|
@@ -112,26 +112,26 @@ transaction_test(Config) ->
|
|
end),
|
|
end),
|
|
WaitForCats = lists:zip(CatIds2, [<<"cat 4">>, <<"cat 5">>, <<"cat 6">>]),
|
|
WaitForCats = lists:zip(CatIds2, [<<"cat 4">>, <<"cat 5">>, <<"cat 6">>]),
|
|
{ok, _, CatRows} = epgsql_pool:query(my_pool, "SELECT id, title FROM category ORDER by id ASC"),
|
|
{ok, _, CatRows} = epgsql_pool:query(my_pool, "SELECT id, title FROM category ORDER by id ASC"),
|
|
- ct:pal("CatRows ~p", [CatRows]),
|
|
|
|
|
|
+ ct:log("CatRows ~p", [CatRows]),
|
|
?assertEqual(WaitForCats, CatRows),
|
|
?assertEqual(WaitForCats, CatRows),
|
|
|
|
|
|
WaitForItems = lists:map(fun({ItemId, {Title, Num}}) -> {ItemId, FirstCatId, Title, Num} end,
|
|
WaitForItems = lists:map(fun({ItemId, {Title, Num}}) -> {ItemId, FirstCatId, Title, Num} end,
|
|
lists:zip(ItemIds2, [{<<"item 1">>, 5}, {<<"item 2">>, 7}])),
|
|
lists:zip(ItemIds2, [{<<"item 1">>, 5}, {<<"item 2">>, 7}])),
|
|
{ok, _, ItemRows} = epgsql_pool:query(my_pool, ?SELECT_ITEMS_QUERY),
|
|
{ok, _, ItemRows} = epgsql_pool:query(my_pool, ?SELECT_ITEMS_QUERY),
|
|
- ct:pal("ItemRows ~p", [ItemRows]),
|
|
|
|
|
|
+ ct:log("ItemRows ~p", [ItemRows]),
|
|
?assertEqual(WaitForItems, ItemRows),
|
|
?assertEqual(WaitForItems, ItemRows),
|
|
|
|
|
|
try
|
|
try
|
|
epgsql_pool:transaction(my_pool,
|
|
epgsql_pool:transaction(my_pool,
|
|
fun(Worker) ->
|
|
fun(Worker) ->
|
|
- ct:pal("worker:~p", [Worker]),
|
|
|
|
|
|
+ ct:log("worker:~p", [Worker]),
|
|
{ok, 2} =
|
|
{ok, 2} =
|
|
epgsql_pool:query(Worker,
|
|
epgsql_pool:query(Worker,
|
|
"INSERT INTO item (category_id, title, num) "
|
|
"INSERT INTO item (category_id, title, num) "
|
|
"VALUES ($1, 'item 3', 55), ($1, 'item 4', 77) ",
|
|
"VALUES ($1, 'item 3', 55), ($1, 'item 4', 77) ",
|
|
[FirstCatId]),
|
|
[FirstCatId]),
|
|
{ok, _, ItemRows2} = epgsql_pool:query(Worker, ?SELECT_ITEMS_QUERY),
|
|
{ok, _, ItemRows2} = epgsql_pool:query(Worker, ?SELECT_ITEMS_QUERY),
|
|
- ct:pal("ItemRows2 ~p", [ItemRows2]),
|
|
|
|
|
|
+ ct:log("ItemRows2 ~p", [ItemRows2]),
|
|
?assertMatch([{_, FirstCatId, <<"item 1">>, 5},
|
|
?assertMatch([{_, FirstCatId, <<"item 1">>, 5},
|
|
{_, FirstCatId, <<"item 2">>, 7},
|
|
{_, FirstCatId, <<"item 2">>, 7},
|
|
{_, FirstCatId, <<"item 3">>, 55},
|
|
{_, FirstCatId, <<"item 3">>, 55},
|
|
@@ -151,33 +151,33 @@ transaction_test(Config) ->
|
|
reconnect_test(Config) ->
|
|
reconnect_test(Config) ->
|
|
Worker = pooler:take_member(my_pool, 1000),
|
|
Worker = pooler:take_member(my_pool, 1000),
|
|
[state, my_pool, #epgsql_connection{sock = Sock1} | _]= tuple_to_list(sys:get_state(Worker)),
|
|
[state, my_pool, #epgsql_connection{sock = Sock1} | _]= tuple_to_list(sys:get_state(Worker)),
|
|
- ct:pal("Worker: ~p, sock: ~p", [Worker, Sock1]),
|
|
|
|
|
|
+ ct:log("Worker: ~p, sock: ~p", [Worker, Sock1]),
|
|
|
|
|
|
R1 = epgsql_pool:query(Worker, ?SELECT_ITEMS_QUERY),
|
|
R1 = epgsql_pool:query(Worker, ?SELECT_ITEMS_QUERY),
|
|
- ct:pal("first query ~p", [R1]),
|
|
|
|
|
|
+ ct:log("first query ~p", [R1]),
|
|
{ok, _, []} = R1,
|
|
{ok, _, []} = R1,
|
|
|
|
|
|
- ct:pal("~p close_connection", [Sock1]),
|
|
|
|
|
|
+ ct:log("~p close_connection", [Sock1]),
|
|
exit(Sock1, close_connection),
|
|
exit(Sock1, close_connection),
|
|
|
|
|
|
R2 = epgsql_pool:query(Worker, "select * from item"),
|
|
R2 = epgsql_pool:query(Worker, "select * from item"),
|
|
- ct:pal("second query goes immediatelly ~p", [R2]),
|
|
|
|
|
|
+ ct:log("second query goes immediatelly ~p", [R2]),
|
|
{error, no_connection} = R2,
|
|
{error, no_connection} = R2,
|
|
|
|
|
|
timer:sleep(50),
|
|
timer:sleep(50),
|
|
|
|
|
|
R3 = epgsql_pool:query(Worker, "select * from item"),
|
|
R3 = epgsql_pool:query(Worker, "select * from item"),
|
|
- ct:pal("third query goes after 50 ms ~p", [R3]),
|
|
|
|
|
|
+ ct:log("third query goes after 50 ms ~p", [R3]),
|
|
{error, no_connection} = R3,
|
|
{error, no_connection} = R3,
|
|
|
|
|
|
timer:sleep(150),
|
|
timer:sleep(150),
|
|
|
|
|
|
R4 = epgsql_pool:query(Worker, "select * from item"),
|
|
R4 = epgsql_pool:query(Worker, "select * from item"),
|
|
- ct:pal("fouth query goes after 200 ms ~p", [R4]),
|
|
|
|
|
|
+ ct:log("fouth query goes after 200 ms ~p", [R4]),
|
|
{ok, _, []} = R4,
|
|
{ok, _, []} = R4,
|
|
|
|
|
|
[state, my_pool, #epgsql_connection{sock = Sock2} | _]= tuple_to_list(sys:get_state(Worker)),
|
|
[state, my_pool, #epgsql_connection{sock = Sock2} | _]= tuple_to_list(sys:get_state(Worker)),
|
|
- ct:pal("Worker: ~p, sock: ~p", [Worker, Sock2]),
|
|
|
|
|
|
+ ct:log("Worker: ~p, sock: ~p", [Worker, Sock2]),
|
|
|
|
|
|
?assertNotEqual(Sock1, Sock2),
|
|
?assertNotEqual(Sock1, Sock2),
|
|
ok.
|
|
ok.
|
|
@@ -185,22 +185,22 @@ reconnect_test(Config) ->
|
|
|
|
|
|
timeout_test(_Config) ->
|
|
timeout_test(_Config) ->
|
|
Res1 = epgsql_pool:query(my_pool, "SELECT pg_sleep(1)", [], [{timeout, 2000}]),
|
|
Res1 = epgsql_pool:query(my_pool, "SELECT pg_sleep(1)", [], [{timeout, 2000}]),
|
|
- ct:pal("Res1:~p", [Res1]),
|
|
|
|
|
|
+ ct:log("Res1:~p", [Res1]),
|
|
?assertMatch({ok, _, _}, Res1),
|
|
?assertMatch({ok, _, _}, Res1),
|
|
|
|
|
|
Res2 = epgsql_pool:query(my_pool, "SELECT pg_sleep(1)", [], [{timeout, 500}]),
|
|
Res2 = epgsql_pool:query(my_pool, "SELECT pg_sleep(1)", [], [{timeout, 500}]),
|
|
- ct:pal("Res2:~p", [Res2]),
|
|
|
|
|
|
+ ct:log("Res2:~p", [Res2]),
|
|
?assertEqual({error, timeout}, Res2),
|
|
?assertEqual({error, timeout}, Res2),
|
|
|
|
|
|
Worker = pooler:take_member(my_pool, 1000),
|
|
Worker = pooler:take_member(my_pool, 1000),
|
|
|
|
|
|
Res3 = epgsql_pool:query(Worker, "SELECT pg_sleep(100)", [], [{timeout, 500}]),
|
|
Res3 = epgsql_pool:query(Worker, "SELECT pg_sleep(100)", [], [{timeout, 500}]),
|
|
- ct:pal("Res3:~p", [Res3]),
|
|
|
|
|
|
+ ct:log("Res3:~p", [Res3]),
|
|
?assertEqual({error, timeout}, Res3),
|
|
?assertEqual({error, timeout}, Res3),
|
|
|
|
|
|
%% check worker and connection able to perform query
|
|
%% check worker and connection able to perform query
|
|
Res4 = epgsql_pool:query(Worker, "SELECT * FROM item", [], [{timeout, 500}]),
|
|
Res4 = epgsql_pool:query(Worker, "SELECT * FROM item", [], [{timeout, 500}]),
|
|
- ct:pal("Res4:~p", [Res4]),
|
|
|
|
|
|
+ ct:log("Res4:~p", [Res4]),
|
|
?assertMatch({ok, _, _}, Res4),
|
|
?assertMatch({ok, _, _}, Res4),
|
|
|
|
|
|
ok.
|
|
ok.
|
|
@@ -210,19 +210,19 @@ validate_connection_params_test(_Config) ->
|
|
Params1 = #epgsql_connection_params{host = "localhost", port = 5432,
|
|
Params1 = #epgsql_connection_params{host = "localhost", port = 5432,
|
|
username = "test", password = "test", database = "testdb"},
|
|
username = "test", password = "test", database = "testdb"},
|
|
Res1 = epgsql_pool:validate_connection_params(Params1),
|
|
Res1 = epgsql_pool:validate_connection_params(Params1),
|
|
- ct:pal("Res1: ~p", [Res1]),
|
|
|
|
|
|
+ ct:log("Res1: ~p", [Res1]),
|
|
?assertEqual(ok, Res1),
|
|
?assertEqual(ok, Res1),
|
|
|
|
|
|
Params2 = #epgsql_connection_params{host = "localhost", port = 5432,
|
|
Params2 = #epgsql_connection_params{host = "localhost", port = 5432,
|
|
username = "test", password = "some", database = "testdb"},
|
|
username = "test", password = "some", database = "testdb"},
|
|
Res2 = epgsql_pool:validate_connection_params(Params2),
|
|
Res2 = epgsql_pool:validate_connection_params(Params2),
|
|
- ct:pal("Res2: ~p", [Res2]),
|
|
|
|
|
|
+ ct:log("Res2: ~p", [Res2]),
|
|
?assertEqual({error,invalid_password}, Res2),
|
|
?assertEqual({error,invalid_password}, Res2),
|
|
|
|
|
|
Params3 = #epgsql_connection_params{host = "localhost", port = 5432,
|
|
Params3 = #epgsql_connection_params{host = "localhost", port = 5432,
|
|
username = "test", password = "test", database = "some"},
|
|
username = "test", password = "test", database = "some"},
|
|
{error, Res3} = epgsql_pool:validate_connection_params(Params3),
|
|
{error, Res3} = epgsql_pool:validate_connection_params(Params3),
|
|
- ct:pal("Res3: ~p", [Res3]),
|
|
|
|
|
|
+ ct:log("Res3: ~p", [Res3]),
|
|
?assertMatch(#error{
|
|
?assertMatch(#error{
|
|
severity = fatal,
|
|
severity = fatal,
|
|
code = <<"3D000">>,
|
|
code = <<"3D000">>,
|