Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph Wecker committed Apr 23, 2010
1 parent 0038e8c commit f7eb467
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
Empty file added test_ebin/.empty
Empty file.
96 changes: 96 additions & 0 deletions test_src/pgsql_pool_tests.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
-module(pgsql_pool_tests).

-include_lib("eunit/include/eunit.hrl").

-define(host, "localhost").

get_connections_test() ->
with_pool_2(
fun(P) ->
{ok, C1} = get_connection(P),
{ok, C2} = get_connection(P),
?assert(C1 =/= C2),
ok = pgsql_pool:return_connection(P, C1),
ok = pgsql_pool:return_connection(P, C2)
end).

get_connection_timeout_test() ->
with_pool_0(
fun(P) ->
{error, timeout} = pgsql_pool:get_connection(P, 100)
end).

get_returned_connection_test() ->
with_pool_1(
fun(P) ->
{ok, C} = get_connection(P),
{error, timeout} = get_connection(P),
ok = pgsql_pool:return_connection(P, C),
{ok, C} = get_connection(P),
ok = pgsql_pool:return_connection(P, C)
end).

return_twice_test() ->
with_pool_1(
fun(P) ->
{ok, C} = get_connection(P),
ok = pgsql_pool:return_connection(P, C),
ok = pgsql_pool:return_connection(P, C)
end).

connection_dies_test() ->
with_pool_1(
fun(P) ->
{ok, C1} = get_connection(P),
exit(C1, kill),
{ok, C2} = get_connection(P),
?assert(C1 =/= C2)
end).

connection_owner_dies_test() ->
with_pool_1(
fun(P) ->
Self = self(),
spawn(fun() ->
{ok, C} = get_connection(P),
Self ! {connection, C}
end),
receive
{connection, C} ->
{ok, C} = get_connection(P),
ok = pgsql_pool:return_connection(P, C)
end
end).

named_pool_test() ->
Name = test_pool,
{ok, _P} = pgsql_pool:start_link(Name, 1, [{host, ?host}]),
{ok, C} = get_connection(Name),
ok = pgsql_pool:return_connection(Name, C),
pgsql_pool:stop(Name).

%% -- internal functions --

with_pool(Size, F) ->
{ok, P} = pgsql_pool:start_link(Size, [{host, ?host}]),
try F(P)
after
pgsql_pool:stop(P)
end.

with_pool_0(F) -> with_pool(0, F).
with_pool_1(F) -> with_pool(1, F).
with_pool_2(F) -> with_pool(2, F).

get_connection(P) ->
case pgsql_pool:get_connection(P, 1000) of
{ok, C} ->
ok = test_connection(C),
{ok, C};
Error ->
Error
end.

test_connection(C) ->
{ok, [_Col], [{<<"1">>}]} = pgsql:squery(C, "select 1"),
ok.

0 comments on commit f7eb467

Please sign in to comment.