Skip to content

Commit 1c6e51f

Browse files
committed
Switch testsuite to common_test, part #2
The migrated tests are those from `rabbit_tests.erl`. References #725. [#116526487]
1 parent 1cb21c4 commit 1c6e51f

File tree

6 files changed

+4306
-228
lines changed

6 files changed

+4306
-228
lines changed

test/dummy_event_receiver.erl

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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(dummy_event_receiver).
18+
19+
-export([start/3, stop/0]).
20+
21+
-export([init/1, handle_call/2, handle_event/2, handle_info/2,
22+
terminate/2, code_change/3]).
23+
24+
-include("rabbit.hrl").
25+
26+
start(Pid, Nodes, Types) ->
27+
Oks = [ok || _ <- Nodes],
28+
{Oks, _} = rpc:multicall(Nodes, gen_event, add_handler,
29+
[rabbit_event, ?MODULE, [Pid, Types]]).
30+
31+
stop() ->
32+
gen_event:delete_handler(rabbit_event, ?MODULE, []).
33+
34+
%%----------------------------------------------------------------------------
35+
36+
init([Pid, Types]) ->
37+
{ok, {Pid, Types}}.
38+
39+
handle_call(_Request, State) ->
40+
{ok, not_understood, State}.
41+
42+
handle_event(Event = #event{type = Type}, State = {Pid, Types}) ->
43+
case lists:member(Type, Types) of
44+
true -> Pid ! Event;
45+
false -> ok
46+
end,
47+
{ok, State}.
48+
49+
handle_info(_Info, State) ->
50+
{ok, State}.
51+
52+
terminate(_Arg, _State) ->
53+
ok.
54+
55+
code_change(_OldVsn, State, _Extra) ->
56+
{ok, State}.
57+
58+
%%----------------------------------------------------------------------------

test/dummy_runtime_parameters.erl

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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(dummy_runtime_parameters).
18+
-behaviour(rabbit_runtime_parameter).
19+
-behaviour(rabbit_policy_validator).
20+
21+
-include("rabbit.hrl").
22+
23+
-export([validate/5, notify/4, notify_clear/3]).
24+
-export([register/0, unregister/0]).
25+
-export([validate_policy/1]).
26+
-export([register_policy_validator/0, unregister_policy_validator/0]).
27+
28+
%----------------------------------------------------------------------------
29+
30+
register() ->
31+
rabbit_registry:register(runtime_parameter, <<"test">>, ?MODULE).
32+
33+
unregister() ->
34+
rabbit_registry:unregister(runtime_parameter, <<"test">>).
35+
36+
validate(_, <<"test">>, <<"good">>, _Term, _User) -> ok;
37+
validate(_, <<"test">>, <<"maybe">>, <<"good">>, _User) -> ok;
38+
validate(_, <<"test">>, <<"admin">>, _Term, none) -> ok;
39+
validate(_, <<"test">>, <<"admin">>, _Term, User) ->
40+
case lists:member(administrator, User#user.tags) of
41+
true -> ok;
42+
false -> {error, "meh", []}
43+
end;
44+
validate(_, <<"test">>, _, _, _) -> {error, "meh", []}.
45+
46+
notify(_, _, _, _) -> ok.
47+
notify_clear(_, _, _) -> ok.
48+
49+
%----------------------------------------------------------------------------
50+
51+
register_policy_validator() ->
52+
rabbit_registry:register(policy_validator, <<"testeven">>, ?MODULE),
53+
rabbit_registry:register(policy_validator, <<"testpos">>, ?MODULE).
54+
55+
unregister_policy_validator() ->
56+
rabbit_registry:unregister(policy_validator, <<"testeven">>),
57+
rabbit_registry:unregister(policy_validator, <<"testpos">>).
58+
59+
validate_policy([{<<"testeven">>, Terms}]) when is_list(Terms) ->
60+
case length(Terms) rem 2 =:= 0 of
61+
true -> ok;
62+
false -> {error, "meh", []}
63+
end;
64+
65+
validate_policy([{<<"testpos">>, Terms}]) when is_list(Terms) ->
66+
case lists:all(fun (N) -> is_integer(N) andalso N > 0 end, Terms) of
67+
true -> ok;
68+
false -> {error, "meh", []}
69+
end;
70+
71+
validate_policy(_) ->
72+
{error, "meh", []}.

test/rabbit_ct_broker_helpers.erl

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919
-include_lib("rabbit_common/include/rabbit.hrl").
2020

2121
-export([
22+
add_test_path_to_broker/2,
2223
run_on_broker/4,
23-
find_listener/0,
24+
get_connection_pids/1,
25+
get_queue_sup_pid/1,
2426
test_channel/0
2527
]).
2628

27-
run_on_broker(Node, Module, Function, Args) ->
28-
%% We add some directories to the broker node search path.
29+
add_test_path_to_broker(Node, Module) ->
2930
Path1 = filename:dirname(code:which(Module)),
3031
Path2 = filename:dirname(code:which(?MODULE)),
3132
Paths = lists:usort([Path1, Path2]),
@@ -36,7 +37,11 @@ run_on_broker(Node, Module, Function, Args) ->
3637
true -> ok;
3738
false -> true = rpc:call(Node, code, add_pathz, [P])
3839
end
39-
end, Paths),
40+
end, Paths).
41+
42+
run_on_broker(Node, Module, Function, Args) ->
43+
%% We add some directories to the broker node search path.
44+
add_test_path_to_broker(Node, Module),
4045
%% If there is an exception, rpc:call/4 returns the exception as
4146
%% a "normal" return value. If there is an exit signal, we raise
4247
%% it again. In both cases, we have no idea of the module and line
@@ -47,17 +52,34 @@ run_on_broker(Node, Module, Function, Args) ->
4752
Ret -> Ret
4853
end.
4954

50-
find_listener() ->
51-
[#listener{host = H, port = P} | _] =
52-
[L || L = #listener{node = N, protocol = amqp}
53-
<- rabbit_networking:active_listeners(),
54-
N =:= node()],
55-
{H, P}.
55+
%% From a given list of gen_tcp client connections, return the list of
56+
%% connection handler PID in RabbitMQ.
57+
get_connection_pids(Connections) ->
58+
ConnInfos = [
59+
begin
60+
{ok, {Addr, Port}} = inet:sockname(Connection),
61+
[{peer_host, Addr}, {peer_port, Port}]
62+
end || Connection <- Connections],
63+
lists:filter(
64+
fun(Conn) ->
65+
ConnInfo = rabbit_networking:connection_info(Conn,
66+
[peer_host, peer_port]),
67+
lists:member(ConnInfo, ConnInfos)
68+
end, rabbit_networking:connections()).
5669

57-
user(Username) ->
58-
#user{username = Username,
59-
tags = [administrator],
60-
authz_backends = [{rabbit_auth_backend_internal, none}]}.
70+
%% Return the PID of the given queue's supervisor.
71+
get_queue_sup_pid(QueuePid) ->
72+
Sups = supervisor:which_children(rabbit_amqqueue_sup_sup),
73+
get_queue_sup_pid(Sups, QueuePid).
74+
75+
get_queue_sup_pid([{_, SupPid, _, _} | Rest], QueuePid) ->
76+
WorkerPids = [Pid || {_, Pid, _, _} <- supervisor:which_children(SupPid)],
77+
case lists:member(QueuePid, WorkerPids) of
78+
true -> SupPid;
79+
false -> get_queue_sup_pid(Rest, QueuePid)
80+
end;
81+
get_queue_sup_pid([], _QueuePid) ->
82+
undefined.
6183

6284
test_channel() ->
6385
Me = self(),
@@ -76,3 +98,8 @@ test_writer(Pid) ->
7698
test_writer(Pid);
7799
shutdown -> ok
78100
end.
101+
102+
user(Username) ->
103+
#user{username = Username,
104+
tags = [administrator],
105+
authz_backends = [{rabbit_auth_backend_internal, none}]}.

test/sup_delayed_restart_SUITE.erl

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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

Comments
 (0)