|
@@ -1,16 +1,16 @@
|
|
|
--module(pidq_test).
|
|
|
+-module(pooler_test).
|
|
|
|
|
|
-include_lib("eunit/include/eunit.hrl").
|
|
|
|
|
|
-compile([export_all]).
|
|
|
|
|
|
-% The `user' processes represent users of the pidq library. A user
|
|
|
+% The `user' processes represent users of the pooler library. A user
|
|
|
% process will take a pid, report details on the pid it has, release
|
|
|
% and take a new pid, stop cleanly, and crash.
|
|
|
|
|
|
start_user() ->
|
|
|
spawn(fun() ->
|
|
|
- TC = pidq:take_pid(),
|
|
|
+ TC = pooler:take_pid(),
|
|
|
user_loop(TC)
|
|
|
end).
|
|
|
|
|
@@ -42,17 +42,17 @@ user_loop(MyTC) ->
|
|
|
From ! pooled_gs:ping_count(MyTC),
|
|
|
user_loop(MyTC);
|
|
|
new_tc ->
|
|
|
- pidq:return_pid(MyTC, ok),
|
|
|
- MyNewTC = pidq:take_pid(),
|
|
|
+ pooler:return_pid(MyTC, ok),
|
|
|
+ MyNewTC = pooler:take_pid(),
|
|
|
user_loop(MyNewTC);
|
|
|
stop ->
|
|
|
- pidq:return_pid(MyTC, ok),
|
|
|
+ pooler:return_pid(MyTC, ok),
|
|
|
stopped;
|
|
|
crash ->
|
|
|
erlang:error({user_loop, kaboom})
|
|
|
end.
|
|
|
|
|
|
-% The `tc' processes represent the pids tracked by pidq for testing.
|
|
|
+% The `tc' processes represent the pids tracked by pooler for testing.
|
|
|
% They have a type and an ID and can report their type and ID and
|
|
|
% stop.
|
|
|
|
|
@@ -102,7 +102,7 @@ assert_tc_valid(Pid) ->
|
|
|
% user_crash(User),
|
|
|
% stop_tc(Pid1).
|
|
|
|
|
|
-pidq_basics_test_() ->
|
|
|
+pooler_basics_test_() ->
|
|
|
{foreach,
|
|
|
% setup
|
|
|
fun() ->
|
|
@@ -111,25 +111,25 @@ pidq_basics_test_() ->
|
|
|
{init_count, 2},
|
|
|
{start_mfa,
|
|
|
{pooled_gs, start_link, [{"type-0"}]}}]],
|
|
|
- application:set_env(pidq, pools, Pools),
|
|
|
- application:start(pidq)
|
|
|
+ application:set_env(pooler, pools, Pools),
|
|
|
+ application:start(pooler)
|
|
|
end,
|
|
|
fun(_X) ->
|
|
|
- application:stop(pidq)
|
|
|
+ application:stop(pooler)
|
|
|
end,
|
|
|
[
|
|
|
{"take and return one",
|
|
|
fun() ->
|
|
|
- P = pidq:take_pid(),
|
|
|
+ P = pooler:take_pid(),
|
|
|
?assertMatch({"type-0", _Id}, pooled_gs:get_id(P)),
|
|
|
- ok = pidq:return_pid(P, ok)
|
|
|
+ ok = pooler:return_pid(P, ok)
|
|
|
end},
|
|
|
|
|
|
{"pids are created on demand until max",
|
|
|
fun() ->
|
|
|
- Pids = [pidq:take_pid(), pidq:take_pid(), pidq:take_pid()],
|
|
|
- ?assertMatch(error_no_pids, pidq:take_pid()),
|
|
|
- ?assertMatch(error_no_pids, pidq:take_pid()),
|
|
|
+ Pids = [pooler:take_pid(), pooler:take_pid(), pooler:take_pid()],
|
|
|
+ ?assertMatch(error_no_pids, pooler:take_pid()),
|
|
|
+ ?assertMatch(error_no_pids, pooler:take_pid()),
|
|
|
PRefs = [ R || {_T, R} <- [ pooled_gs:get_id(P) || P <- Pids ] ],
|
|
|
% no duplicates
|
|
|
?assertEqual(length(PRefs), length(lists:usort(PRefs)))
|
|
@@ -138,19 +138,19 @@ pidq_basics_test_() ->
|
|
|
|
|
|
{"pids are reused most recent return first",
|
|
|
fun() ->
|
|
|
- P1 = pidq:take_pid(),
|
|
|
- P2 = pidq:take_pid(),
|
|
|
+ P1 = pooler:take_pid(),
|
|
|
+ P2 = pooler:take_pid(),
|
|
|
?assertNot(P1 == P2),
|
|
|
- ok = pidq:return_pid(P1, ok),
|
|
|
- ok = pidq:return_pid(P2, ok),
|
|
|
+ ok = pooler:return_pid(P1, ok),
|
|
|
+ ok = pooler:return_pid(P2, ok),
|
|
|
% pids are reused most recent first
|
|
|
- ?assertEqual(P2, pidq:take_pid()),
|
|
|
- ?assertEqual(P1, pidq:take_pid())
|
|
|
+ ?assertEqual(P2, pooler:take_pid()),
|
|
|
+ ?assertEqual(P1, pooler:take_pid())
|
|
|
end},
|
|
|
|
|
|
{"if a pid crashes it is replaced",
|
|
|
fun() ->
|
|
|
- Pids0 = [pidq:take_pid(), pidq:take_pid(), pidq:take_pid()],
|
|
|
+ Pids0 = [pooler:take_pid(), pooler:take_pid(), pooler:take_pid()],
|
|
|
Ids0 = [ pooled_gs:get_id(P) || P <- Pids0 ],
|
|
|
% crash them all
|
|
|
[ pooled_gs:crash(P) || P <- Pids0 ],
|
|
@@ -162,10 +162,10 @@ pidq_basics_test_() ->
|
|
|
|
|
|
{"if a pid is returned with bad status it is replaced",
|
|
|
fun() ->
|
|
|
- Pids0 = [pidq:take_pid(), pidq:take_pid(), pidq:take_pid()],
|
|
|
+ Pids0 = [pooler:take_pid(), pooler:take_pid(), pooler:take_pid()],
|
|
|
Ids0 = [ pooled_gs:get_id(P) || P <- Pids0 ],
|
|
|
% return them all marking as bad
|
|
|
- [ pidq:return_pid(P, fail) || P <- Pids0 ],
|
|
|
+ [ pooler:return_pid(P, fail) || P <- Pids0 ],
|
|
|
Pids1 = get_n_pids(3, []),
|
|
|
Ids1 = [ pooled_gs:get_id(P) || P <- Pids1 ],
|
|
|
[ ?assertNot(lists:member(I, Ids0)) || I <- Ids1 ]
|
|
@@ -185,7 +185,7 @@ pidq_basics_test_() ->
|
|
|
]}.
|
|
|
|
|
|
|
|
|
-pidq_integration_test_() ->
|
|
|
+pooler_integration_test_() ->
|
|
|
{foreach,
|
|
|
% setup
|
|
|
fun() ->
|
|
@@ -194,15 +194,15 @@ pidq_integration_test_() ->
|
|
|
{init_count, 10},
|
|
|
{start_mfa,
|
|
|
{pooled_gs, start_link, [{"type-0"}]}}]],
|
|
|
- application:set_env(pidq, pools, Pools),
|
|
|
- application:start(pidq),
|
|
|
+ application:set_env(pooler, pools, Pools),
|
|
|
+ application:start(pooler),
|
|
|
Users = [ start_user() || _X <- lists:seq(1, 10) ],
|
|
|
Users
|
|
|
end,
|
|
|
% cleanup
|
|
|
fun(Users) ->
|
|
|
[ user_stop(U) || U <- Users ],
|
|
|
- application:stop(pidq)
|
|
|
+ application:stop(pooler)
|
|
|
end,
|
|
|
%
|
|
|
[
|
|
@@ -243,12 +243,12 @@ pidq_integration_test_() ->
|
|
|
}.
|
|
|
|
|
|
% testing crash recovery means race conditions when either pids
|
|
|
-% haven't yet crashed or pidq hasn't recovered. So this helper loops
|
|
|
+% haven't yet crashed or pooler hasn't recovered. So this helper loops
|
|
|
% forver until N pids are obtained, ignoring error_no_pids.
|
|
|
get_n_pids(0, Acc) ->
|
|
|
Acc;
|
|
|
get_n_pids(N, Acc) ->
|
|
|
- case pidq:take_pid() of
|
|
|
+ case pooler:take_pid() of
|
|
|
error_no_pids ->
|
|
|
get_n_pids(N, Acc);
|
|
|
Pid ->
|