Browse Source

Refactor tests for new supervisor based setup

Seth Falcon 14 years ago
parent
commit
8f09ea04f1
5 changed files with 138 additions and 57 deletions
  1. 6 9
      src/pidq.erl
  2. 0 1
      src/pidq_pool_sup.erl
  3. 1 2
      src/pidq_pooled_worker_sup.erl
  4. 46 45
      test/pidq_test.erl
  5. 85 0
      test/pooled_gs.erl

+ 6 - 9
src/pidq.erl

@@ -87,16 +87,14 @@ status() ->
 %% ------------------------------------------------------------------
 
 init(Config) ->
-    ?debugMsg("in pidq init"),
     PoolRecs = [ props_to_pool(P) || P <- ?gv(pools, Config) ],
-    ?debugVal(hd(PoolRecs)),
     Pools = [ {Pool#pool.name, Pool} || Pool <-  PoolRecs ],
-    ?debugMsg("have list"),
-    PoolSups = [ {P#pool.name,
-                  element(2, supervisor:start_child(pidq_pool_sup,
-                                                    [P#pool.start_mfa]))}
-                 || P <- PoolRecs ],
-    ?debugMsg("called start_child"),
+    PoolSups =
+        lists:map(
+          fun(#pool{name = Name, start_mfa = MFA}) ->
+                  {ok, SupPid} = supervisor:start_child(pidq_pool_sup, [MFA]),
+                  {Name, SupPid}
+          end, PoolRecs),
     % TODO: create initial workers here
     State = #state{npools = length(Pools),
                    pools = dict:from_list(Pools),
@@ -138,7 +136,6 @@ handle_info({'EXIT', Pid, Reason}, State) ->
                      case dict:find(Pid, CPMap) of
 
                          {ok, Pids} ->
-                             error_logger:info_report({{consumer, Pid, Reason}, Pids}),
                              IsOk = case Reason of
                                         normal -> ok;
                                         _Crash -> fail

+ 0 - 1
src/pidq_pool_sup.erl

@@ -8,7 +8,6 @@ start_link() ->
     supervisor:start_link({local, ?MODULE}, ?MODULE, []).
 
 init([]) ->
-    error_logger:info_report(in_pidq_pool_sup),
     Worker = {pidq_pooled_worker_sup,
               {pidq_pooled_worker_sup, start_link, []},
               temporary, 5000, supervisor, [pidq_pooled_worker_sup]},

+ 1 - 2
src/pidq_pooled_worker_sup.erl

@@ -5,10 +5,9 @@
 -export([start_link/1, init/1]).
 
 start_link(Config) ->
-    supervisor:start_link({local, ?MODULE}, ?MODULE, Config).
+    supervisor:start_link(?MODULE, Config).
 
 init({Mod, Fun, Args}) ->
-    error_logger:info_report(in_pidq_pooled_worker_sup),
     Worker = {Mod, {Mod, Fun, Args}, temporary, brutal_kill, worker, [Mod]},
     Specs = [Worker],
     Restart = {simple_one_for_one, 1, 1},

+ 46 - 45
test/pidq_test.erl

@@ -33,7 +33,13 @@ user_crash(Pid) ->
 user_loop(MyTC) ->
     receive
         {get_tc_id, From} ->
-            From ! get_tc_id(MyTC),
+            From ! pooled_gs:get_id(MyTC),
+            user_loop(MyTC);
+        {ping_tc, From} ->
+            From ! pooled_gs:ping(MyTC),
+            user_loop(MyTC);
+        {ping_count, From} ->
+            From ! pooled_gs:ping_count(MyTC),
             user_loop(MyTC);
         new_tc ->
             pidq:return_pid(MyTC, ok),
@@ -80,43 +86,42 @@ assert_tc_valid(Pid) ->
     ?assertMatch({_Type, _Ref}, get_tc_id(Pid)),
     ok.
 
-tc_sanity_test() ->
-    Pid1 = tc_starter("1"),
-    {"1", Id1} = get_tc_id(Pid1),
-    Pid2 = tc_starter("1"),
-    {"1", Id2} = get_tc_id(Pid2),
-    ?assertNot(Id1 == Id2),
-    stop_tc(Pid1),
-    stop_tc(Pid2).
-
-user_sanity_test() ->
-    Pid1 = tc_starter("1"),
-    User = spawn(fun() -> user_loop(Pid1) end),
-    ?assertMatch({"1", _Ref}, user_id(User)),
-    user_crash(User),
-    stop_tc(Pid1).
+% tc_sanity_test() ->
+%     Pid1 = tc_starter("1"),
+%     {"1", Id1} = get_tc_id(Pid1),
+%     Pid2 = tc_starter("1"),
+%     {"1", Id2} = get_tc_id(Pid2),
+%     ?assertNot(Id1 == Id2),
+%     stop_tc(Pid1),
+%     stop_tc(Pid2).
+
+% user_sanity_test() ->
+%     Pid1 = tc_starter("1"),
+%     User = spawn(fun() -> user_loop(Pid1) end),
+%     ?assertMatch({"1", _Ref}, user_id(User)),
+%     user_crash(User),
+%     stop_tc(Pid1).
 
 pidq_basics_test_() ->
     {foreach,
      % setup
      fun() ->
              Pools = [[{name, "p1"},
-                       {max_pids, 3}, {min_free, 1},
-                       {init_size, 2}, {pid_starter_args, ["type-0"]}]],
-
-             Config = [{pid_starter, {?MODULE, tc_starter}},
-                       {pid_stopper, {?MODULE, stop_tc}},
-                       {pools, Pools}],
-             pidq:start(Config)
+                       {max_count, 3},
+                       {init_count, 2},
+                       {start_mfa,
+                        {pooled_gs, start_link, [{"type-0"}]}}]],
+             application:set_env(pidq, pools, Pools),
+             application:start(pidq)
      end,
      fun(_X) ->
-             pidq:stop()
+             application:stop(pidq)
      end,
      [
       {"take and return one",
        fun() ->
                P = pidq:take_pid(),
-               ?assertMatch({"type-0", _Id}, get_tc_id(P)),
+               ?assertMatch({"type-0", _Id}, pooled_gs:get_id(P)),
                ok = pidq:return_pid(P, ok)
        end},
 
@@ -125,7 +130,7 @@ pidq_basics_test_() ->
                Pids = [pidq:take_pid(), pidq:take_pid(), pidq:take_pid()],
                ?assertMatch(error_no_pids, pidq:take_pid()),
                ?assertMatch(error_no_pids, pidq:take_pid()),
-               PRefs = [ R || {_T, R} <- [ get_tc_id(P) || P <- Pids ] ],
+               PRefs = [ R || {_T, R} <- [ pooled_gs:get_id(P) || P <- Pids ] ],
                % no duplicates
                ?assertEqual(length(PRefs), length(lists:usort(PRefs)))
        end
@@ -146,11 +151,11 @@ pidq_basics_test_() ->
       {"if a pid crashes it is replaced",
        fun() ->
                Pids0 = [pidq:take_pid(), pidq:take_pid(), pidq:take_pid()],
-               Ids0 = [ get_tc_id(P) || P <- Pids0 ],
+               Ids0 = [ pooled_gs:get_id(P) || P <- Pids0 ],
                % crash them all
-               [ P ! crash || P <- Pids0 ],
+               [ pooled_gs:crash(P) || P <- Pids0 ],
                Pids1 = get_n_pids(3, []),
-               Ids1 = [ get_tc_id(P) || P <- Pids1 ],
+               Ids1 = [ pooled_gs:get_id(P) || P <- Pids1 ],
                [ ?assertNot(lists:member(I, Ids0)) || I <- Ids1 ]
        end
        },
@@ -158,11 +163,11 @@ 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()],
-               Ids0 = [ get_tc_id(P) || P <- Pids0 ],
+               Ids0 = [ pooled_gs:get_id(P) || P <- Pids0 ],
                % return them all marking as bad
                [ pidq:return_pid(P, fail) || P <- Pids0 ],
                Pids1 = get_n_pids(3, []),
-               Ids1 = [ get_tc_id(P) || P <- Pids1 ],
+               Ids1 = [ pooled_gs:get_id(P) || P <- Pids1 ],
                [ ?assertNot(lists:member(I, Ids0)) || I <- Ids1 ]
        end
       },
@@ -171,11 +176,9 @@ pidq_basics_test_() ->
        fun() ->
                Consumer = start_user(),
                StartId = user_id(Consumer),
-               ?debugVal(pidq:pool_stats("p1")),
                user_crash(Consumer),
                NewPid = hd(get_n_pids(1, [])),
-               NewId = get_tc_id(NewPid),
-               ?debugVal(pidq:pool_stats("p1")),
+               NewId = pooled_gs:get_id(NewPid),
                ?assertNot(NewId == StartId)
        end
       }
@@ -187,22 +190,19 @@ pidq_integration_test_() ->
      % setup
      fun() ->
              Pools = [[{name, "p1"},
-                      {max_pids, 10},
-                      {min_free, 3},
-                      {init_size, 10},
-                      {pid_starter_args, ["type-0"]}]],
-
-             Config = [{pid_starter, {?MODULE, tc_starter}},
-                       {pid_stopper, {?MODULE, stop_tc}},
-                       {pools, Pools}],
-             pidq:start(Config),
+                       {max_count, 10},
+                       {init_count, 10},
+                       {start_mfa,
+                        {pooled_gs, start_link, [{"type-0"}]}}]],
+             application:set_env(pidq, pools, Pools),
+             application:start(pidq),
              Users = [ start_user() || _X <- lists:seq(1, 10) ],
              Users
      end,
      % cleanup
      fun(Users) ->
              [ user_stop(U) || U <- Users ],
-             pidq:stop()
+             application:stop(pidq)
      end,
      %
      [
@@ -212,7 +212,8 @@ pidq_integration_test_() ->
                      TcIds = lists:sort([ user_id(UPid) || UPid <- Users ]),
                      ?assertEqual(lists:usort(TcIds), TcIds)
              end
-      end,
+      end
+      ,
 
       fun(Users) ->
               fun() ->

+ 85 - 0
test/pooled_gs.erl

@@ -0,0 +1,85 @@
+-module(pooled_gs).
+-behaviour(gen_server).
+-define(SERVER, ?MODULE).
+
+%% ------------------------------------------------------------------
+%% API Function Exports
+%% ------------------------------------------------------------------
+
+-export([start_link/1,
+         get_id/1,
+         ping/1,
+         ping_count/1,
+         crash/1,
+         stop/1
+        ]).
+
+%% ------------------------------------------------------------------
+%% gen_server Function Exports
+%% ------------------------------------------------------------------
+
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
+         terminate/2, code_change/3]).
+
+%% ------------------------------------------------------------------
+%% API Function Definitions
+%% ------------------------------------------------------------------
+
+start_link(Args ={_Type}) ->
+    % not registered
+    gen_server:start_link(?MODULE, Args, []).
+
+get_id(S) ->
+    gen_server:call(S, get_id).
+
+ping(S) ->
+    gen_server:call(S, ping).
+
+ping_count(S) ->
+    gen_server:call(S, ping_count).
+
+crash(S) ->
+    gen_server:cast(S, crash),
+    sent_crash_request.
+
+stop(S) ->
+    gen_server:call(S, stop).
+
+%% ------------------------------------------------------------------
+%% gen_server Function Definitions
+%% ------------------------------------------------------------------
+-record(state, {
+          type = "",
+          id,
+          ping_count = 0
+         }).
+
+init({Type}) ->
+    {ok, #state{type = Type, id = make_ref()}}.
+
+handle_call(get_id, _From, State) ->
+    {reply, {State#state.type, State#state.id}, State};
+handle_call(ping, _From, #state{ping_count = C } = State) ->
+    State1 = State#state{ping_count = C + 1},
+    {reply, pong, State1};
+handle_call(ping_count, _From, #state{ping_count = C } = State) ->
+    {reply, C, State};
+handle_call(stop, _From, State) ->
+    {stop, normal, stop_ok, State};
+handle_call(_Request, _From, State) ->
+    {noreply, ok, State}.
+
+handle_cast(crash, State) ->
+    erlang:error({pooled_gs, requested_crash});
+handle_cast(_Msg, State) ->
+    {noreply, State}.
+
+handle_info(_Info, State) ->
+    {noreply, State}.
+
+terminate(_Reason, _State) ->
+    ok.
+
+code_change(_OldVsn, State, _Extra) ->
+    {ok, State}.
+