|
| 1 | +%% The contents of this file are subject to the Mozilla Public License |
| 2 | +%% Version 1.1 (the "License"); you may not use this file except in |
| 3 | +%% compliance with the License. You may obtain a copy of the License |
| 4 | +%% at http://www.mozilla.org/MPL/ |
| 5 | +%% |
| 6 | +%% Software distributed under the License is distributed on an "AS IS" |
| 7 | +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See |
| 8 | +%% the License for the specific language governing rights and |
| 9 | +%% limitations under the License. |
| 10 | +%% |
| 11 | +%% The Original Code is RabbitMQ. |
| 12 | +%% |
| 13 | +%% The Initial Developer of the Original Code is GoPivotal, Inc. |
| 14 | +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. |
| 15 | +%% |
| 16 | + |
| 17 | +-module(sup_delayed_restart_SUITE). |
| 18 | + |
| 19 | +-behaviour(supervisor2). |
| 20 | + |
| 21 | +-include_lib("common_test/include/ct.hrl"). |
| 22 | + |
| 23 | +-compile(export_all). |
| 24 | + |
| 25 | +all() -> |
| 26 | + [ |
| 27 | + delayed_restart |
| 28 | + ]. |
| 29 | + |
| 30 | +%%---------------------------------------------------------------------------- |
| 31 | +%% Public API |
| 32 | +%%---------------------------------------------------------------------------- |
| 33 | + |
| 34 | +delayed_restart(_Config) -> |
| 35 | + passed = with_sup(simple_one_for_one, |
| 36 | + fun (SupPid) -> |
| 37 | + {ok, _ChildPid} = |
| 38 | + supervisor2:start_child(SupPid, []), |
| 39 | + test_supervisor_delayed_restart(SupPid) |
| 40 | + end), |
| 41 | + passed = with_sup(one_for_one, fun test_supervisor_delayed_restart/1). |
| 42 | + |
| 43 | +test_supervisor_delayed_restart(SupPid) -> |
| 44 | + ok = ping_child(SupPid), |
| 45 | + ok = exit_child(SupPid), |
| 46 | + timer:sleep(100), |
| 47 | + ok = ping_child(SupPid), |
| 48 | + ok = exit_child(SupPid), |
| 49 | + timer:sleep(100), |
| 50 | + timeout = ping_child(SupPid), |
| 51 | + timer:sleep(1010), |
| 52 | + ok = ping_child(SupPid), |
| 53 | + passed. |
| 54 | + |
| 55 | +with_sup(RestartStrategy, Fun) -> |
| 56 | + {ok, SupPid} = supervisor2:start_link(?MODULE, [RestartStrategy]), |
| 57 | + Res = Fun(SupPid), |
| 58 | + unlink(SupPid), |
| 59 | + exit(SupPid, shutdown), |
| 60 | + Res. |
| 61 | + |
| 62 | +init([RestartStrategy]) -> |
| 63 | + {ok, {{RestartStrategy, 1, 1}, |
| 64 | + [{test, {?MODULE, start_child, []}, {permanent, 1}, |
| 65 | + 16#ffffffff, worker, [?MODULE]}]}}. |
| 66 | + |
| 67 | +start_child() -> |
| 68 | + {ok, proc_lib:spawn_link(fun run_child/0)}. |
| 69 | + |
| 70 | +ping_child(SupPid) -> |
| 71 | + Ref = make_ref(), |
| 72 | + with_child_pid(SupPid, fun(ChildPid) -> ChildPid ! {ping, Ref, self()} end), |
| 73 | + receive {pong, Ref} -> ok |
| 74 | + after 1000 -> timeout |
| 75 | + end. |
| 76 | + |
| 77 | +exit_child(SupPid) -> |
| 78 | + with_child_pid(SupPid, fun(ChildPid) -> exit(ChildPid, abnormal) end), |
| 79 | + ok. |
| 80 | + |
| 81 | +with_child_pid(SupPid, Fun) -> |
| 82 | + case supervisor2:which_children(SupPid) of |
| 83 | + [{_Id, undefined, worker, [?MODULE]}] -> ok; |
| 84 | + [{_Id, ChildPid, worker, [?MODULE]}] -> Fun(ChildPid); |
| 85 | + [] -> ok |
| 86 | + end. |
| 87 | + |
| 88 | +run_child() -> |
| 89 | + receive {ping, Ref, Pid} -> Pid ! {pong, Ref}, |
| 90 | + run_child() |
| 91 | + end. |
0 commit comments