summaryrefslogtreecommitdiff
path: root/src/supervisor2_tests.erl
diff options
context:
space:
mode:
authorTim Watson <tim@rabbitmq.com>2012-09-04 10:56:32 +0100
committerTim Watson <tim@rabbitmq.com>2012-09-04 10:56:32 +0100
commit24a1161a52e32f170c9fdf549e152a14771be41e (patch)
treee78e5710afcdc2e60caba6b439c9d88827b532a6 /src/supervisor2_tests.erl
parentb9100c6a425f65f2f5a338b5ad65dbfce5ac0ce3 (diff)
downloadrabbitmq-server-24a1161a52e32f170c9fdf549e152a14771be41e.tar.gz
rework tests for most simple case of child termination
increase test coverage for whole of terminate_simple_children fix bug in timeout handling clause of terminate_simple_children cosmetic (trailing whitespace)
Diffstat (limited to 'src/supervisor2_tests.erl')
-rw-r--r--src/supervisor2_tests.erl113
1 files changed, 45 insertions, 68 deletions
diff --git a/src/supervisor2_tests.erl b/src/supervisor2_tests.erl
index 6898cfa6..599dfac9 100644
--- a/src/supervisor2_tests.erl
+++ b/src/supervisor2_tests.erl
@@ -17,89 +17,66 @@
-module(supervisor2_tests).
-behaviour(supervisor2).
--export([test_all/0, start_sup/0, start_link/0, start_child/0]).
+-export([test_all/0, start_link/0]).
-export([init/1]).
--define(NUM_CHILDREN, 1000).
-
--export([test_all/0]).
-
-include_lib("eunit/include/eunit.hrl").
+-define(TEST_RUNS, 2000).
+-define(SLOW_TEST_RUNS, 45).
+-define(CHILDREN, 100).
+
test_all() ->
- eunit:test(supervisor2, [verbose]).
+ eunit:test(?MODULE, [verbose]).
-terminate_simple_children_without_deadlock_test_() ->
- lists:flatten(
- lists:duplicate(
- 100, [{setup, fun init_supervisor/0,
- {with, [fun ensure_children_are_alive/1,
- fun shutdown_and_verify_all_children_died/1]}},
- {setup, fun init_supervisor/0,
- {with, [fun shutdown_whilst_interleaving_exits_occur/1]}}])).
+simple_child_shutdown_without_deadlock_test_() ->
+ lists:duplicate(?TEST_RUNS,
+ [{timeout, 60, fun test_clean_stop/0}]).
-%%
-%% Public (test facing) API
-%%
+simple_child_shutdown_with_timeout_test_() ->
+ lists:duplicate(?SLOW_TEST_RUNS,
+ [{timeout, 120, fun test_timeout/0}]).
-start_sup() ->
- supervisor2:start_link({local, ?MODULE}, ?MODULE, []).
+test_clean_stop() ->
+ test_it(stop).
-start_link() ->
- Pid = spawn_link(fun loop_infinity/0),
- {ok, Pid}.
+test_timeout() ->
+ test_it(ignored).
-start_child() ->
- {ok, Pid} = supervisor2:start_child(?MODULE, []),
- Pid.
+test_it(SigStop) ->
+ {ok, Pid} = supervisor2:start_link(?MODULE, [?CHILDREN]),
+ start_and_terminate_children(SigStop, Pid, ?CHILDREN),
+ unlink(Pid),
+ exit(Pid, shutdown).
-%%
-%% supervisor2 callbacks
-%%
+start_link() ->
+ Pid = spawn_link(fun () ->
+ process_flag(trap_exit, true),
+ receive stop -> ok end
+ end),
+ {ok, Pid}.
-init([parent]) ->
+init([N]) ->
{ok, {{one_for_one, 0, 1},
- [{test_sup, {?MODULE, start_sup, []},
- transient, 5000, supervisor, [?MODULE]}]}};
+ [{test_sup, {supervisor2, start_link,
+ [{local, ?MODULE}, ?MODULE, []]},
+ transient, N * 100, supervisor, [?MODULE]}]}};
init([]) ->
{ok, {{simple_one_for_one_terminate, 0, 1},
[{test_worker, {?MODULE, start_link, []},
- temporary, 1000, worker, []}]}}.
-
-%%
-%% Private API
-%%
-
-ensure_children_are_alive({_, ChildPids}) ->
- ?assertEqual(true,
- lists:all(fun erlang:is_process_alive/1, ChildPids)).
-
-shutdown_and_verify_all_children_died({Parent, ChildPids} = State) ->
- ensure_children_are_alive(State),
- TestSup = erlang:whereis(?MODULE),
- ?assertEqual(true, erlang:is_process_alive(TestSup)),
- ?assertMatch(ok, supervisor2:terminate_child(Parent, test_sup)),
- ?assertMatch([], [P || P <- ChildPids,
- erlang:is_process_alive(P)]),
- ?assertEqual(false, erlang:is_process_alive(TestSup)).
-
-shutdown_whilst_interleaving_exits_occur({Parent, ChildPids} = State) ->
- ensure_children_are_alive(State),
- TestPid = self(),
- Ref = erlang:make_ref(),
- spawn(fun() ->
- TestPid ! {Ref, supervisor2:terminate_child(Parent, test_sup)}
- end),
- [P ! stop || P <- ChildPids],
- receive {Ref, Res} ->
- ?assertEqual(ok, Res)
- end.
-
-init_supervisor() ->
- {ok, Pid} = supervisor2:start_link(?MODULE, [parent]),
- {Pid, [start_child() || _ <- lists:seq(1, ?NUM_CHILDREN)]}.
-
-loop_infinity() ->
+ temporary, 1000, worker, [?MODULE]}]}}.
+
+start_and_terminate_children(SigStop, Sup, N) ->
+ TestSupPid = whereis(?MODULE),
+ ChildPids = [begin
+ {ok, ChildPid} = supervisor2:start_child(TestSupPid, []),
+ ChildPid
+ end || _ <- lists:seq(1, N)],
+ erlang:monitor(process, TestSupPid),
+ [P ! SigStop || P <- ChildPids],
+ ?assertEqual(ok, supervisor2:terminate_child(Sup, test_sup)),
+ ?assertMatch({ok,_}, supervisor2:restart_child(Sup, test_sup)),
receive
- stop -> ok
+ {'DOWN', _MRef, process, TestSupPid, Reason} ->
+ ?assertMatch(shutdown, Reason)
end.