Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Tests

  • Loading branch information...
commit f7eb467861fc98b7d511a823a9f3d614bc08f4fa 1 parent 0038e8c
Joseph Wecker authored
Showing with 96 additions and 0 deletions.
  1. 0  test_ebin/.empty
  2. +96 −0 test_src/pgsql_pool_tests.erl
View
0  test_ebin/.empty
No changes.
View
96 test_src/pgsql_pool_tests.erl
@@ -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.
Please sign in to comment.
Something went wrong with that request. Please try again.