Skip to content

Commit 34707c8

Browse files
committed
Make CI: Add forks of ct_master_event and ct_master_logs
1 parent 320d8ae commit 34707c8

File tree

3 files changed

+765
-10
lines changed

3 files changed

+765
-10
lines changed
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
%%
2+
%% %CopyrightBegin%
3+
%%
4+
%% Copyright Ericsson AB 2006-2024. All Rights Reserved.
5+
%%
6+
%% Licensed under the Apache License, Version 2.0 (the "License");
7+
%% you may not use this file except in compliance with the License.
8+
%% You may obtain a copy of the License at
9+
%%
10+
%% http://www.apache.org/licenses/LICENSE-2.0
11+
%%
12+
%% Unless required by applicable law or agreed to in writing, software
13+
%% distributed under the License is distributed on an "AS IS" BASIS,
14+
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
%% See the License for the specific language governing permissions and
16+
%% limitations under the License.
17+
%%
18+
%% %CopyrightEnd%
19+
%%
20+
21+
%%% Common Test Framework Event Handler
22+
%%%
23+
%%% This module implements an event handler that the CT Master
24+
%%% uses to handle status and progress notifications sent to the
25+
%%% master node during test runs. This module may be used as a
26+
%%% template for other event handlers that can be plugged in to
27+
%%% handle logging and reporting on the master node.
28+
-module(ct_master_event_fork).
29+
-moduledoc false.
30+
31+
-behaviour(gen_event).
32+
33+
%% API
34+
-export([start_link/0, add_handler/0, add_handler/1, stop/0]).
35+
-export([notify/1, sync_notify/1]).
36+
37+
%% gen_event callbacks
38+
-export([init/1, handle_event/2, handle_call/2,
39+
handle_info/2, terminate/2, code_change/3]).
40+
41+
-include_lib("common_test/include/ct_event.hrl").
42+
-include_lib("common_test/src/ct_util.hrl").
43+
44+
45+
-record(state, {}).
46+
47+
%%====================================================================
48+
%% gen_event callbacks
49+
%%====================================================================
50+
%%--------------------------------------------------------------------
51+
%% Function: start_link() -> {ok,Pid} | {error,Error}
52+
%% Description: Creates an event manager.
53+
%%--------------------------------------------------------------------
54+
start_link() ->
55+
gen_event:start_link({local,?CT_MEVMGR}).
56+
57+
%%--------------------------------------------------------------------
58+
%% Function: add_handler() -> ok | {'EXIT',Reason} | term()
59+
%% Description: Adds an event handler
60+
%%--------------------------------------------------------------------
61+
add_handler() ->
62+
gen_event:add_handler(?CT_MEVMGR_REF,?MODULE,[]).
63+
add_handler(Args) ->
64+
gen_event:add_handler(?CT_MEVMGR_REF,?MODULE,Args).
65+
66+
%%--------------------------------------------------------------------
67+
%% Function: stop() -> ok
68+
%% Description: Stops the event manager
69+
%%--------------------------------------------------------------------
70+
stop() ->
71+
case flush() of
72+
{error,Reason} ->
73+
ct_master_logs_fork:log("Error",
74+
"No response from CT Master Event.\n"
75+
"Reason = ~tp\n"
76+
"Terminating now!\n",[Reason]),
77+
%% communication with event manager fails, kill it
78+
catch exit(whereis(?CT_MEVMGR_REF), kill);
79+
_ ->
80+
gen_event:stop(?CT_MEVMGR_REF)
81+
end.
82+
83+
flush() ->
84+
try gen_event:call(?CT_MEVMGR_REF,?MODULE,flush,1800000) of
85+
flushing ->
86+
timer:sleep(1),
87+
flush();
88+
done ->
89+
ok;
90+
Error = {error,_} ->
91+
Error
92+
catch
93+
_:Reason ->
94+
{error,Reason}
95+
end.
96+
97+
%%--------------------------------------------------------------------
98+
%% Function: notify(Event) -> ok
99+
%% Description: Asynchronous notification to event manager.
100+
%%--------------------------------------------------------------------
101+
notify(Event) ->
102+
gen_event:notify(?CT_MEVMGR_REF,Event).
103+
104+
%%--------------------------------------------------------------------
105+
%% Function: sync_notify(Event) -> ok
106+
%% Description: Synchronous notification to event manager.
107+
%%--------------------------------------------------------------------
108+
sync_notify(Event) ->
109+
gen_event:sync_notify(?CT_MEVMGR_REF,Event).
110+
111+
%%====================================================================
112+
%% gen_event callbacks
113+
%%====================================================================
114+
%%--------------------------------------------------------------------
115+
%% Function: init(Args) -> {ok, State}
116+
%% Description: Whenever a new event handler is added to an event manager,
117+
%% this function is called to initialize the event handler.
118+
%%--------------------------------------------------------------------
119+
init(_) ->
120+
ct_util:mark_process(),
121+
ct_master_logs_fork:log("CT Master Event Handler started","",[]),
122+
{ok,#state{}}.
123+
124+
%%--------------------------------------------------------------------
125+
%% Function:
126+
%% handle_event(Event, State) -> {ok, State} |
127+
%% {swap_handler, Args1, State1, Mod2, Args2} |
128+
%% remove_handler
129+
%% Description:Whenever an event manager receives an event sent using
130+
%% gen_event:notify/2 or gen_event:sync_notify/2, this function is called for
131+
%% each installed event handler to handle the event.
132+
%%--------------------------------------------------------------------
133+
handle_event(#event{name=start_logging,node=Node,data=RunDir},State) ->
134+
ct_master_logs_fork:log("CT Master Event Handler","Got ~ts from ~w",[RunDir,Node]),
135+
ct_master_logs_fork:nodedir(Node,RunDir),
136+
{ok,State};
137+
138+
handle_event(#event{name=Name,node=Node,data=Data},State) ->
139+
print("~n=== ~w ===~n", [?MODULE]),
140+
print("~tw on ~w: ~tp~n", [Name,Node,Data]),
141+
{ok,State}.
142+
143+
%%--------------------------------------------------------------------
144+
%% Function:
145+
%% handle_call(Request, State) -> {ok, Reply, State} |
146+
%% {swap_handler, Reply, Args1, State1,
147+
%% Mod2, Args2} |
148+
%% {remove_handler, Reply}
149+
%% Description: Whenever an event manager receives a request sent using
150+
%% gen_event:call/3,4, this function is called for the specified event
151+
%% handler to handle the request.
152+
%%--------------------------------------------------------------------
153+
handle_call(flush,State) ->
154+
case process_info(self(),message_queue_len) of
155+
{message_queue_len,0} ->
156+
{ok,done,State};
157+
_ ->
158+
{ok,flushing,State}
159+
end.
160+
161+
%%--------------------------------------------------------------------
162+
%% Function:
163+
%% handle_info(Info, State) -> {ok, State} |
164+
%% {swap_handler, Args1, State1, Mod2, Args2} |
165+
%% remove_handler
166+
%% Description: This function is called for each installed event handler when
167+
%% an event manager receives any other message than an event or a synchronous
168+
%% request (or a system message).
169+
%%--------------------------------------------------------------------
170+
handle_info(_Info,State) ->
171+
{ok,State}.
172+
173+
%%--------------------------------------------------------------------
174+
%% Function: terminate(Reason, State) -> ok
175+
%% Description:Whenever an event handler is deleted from an event manager,
176+
%% this function is called. It should be the opposite of Module:init/1 and
177+
%% do any necessary cleaning up.
178+
%%--------------------------------------------------------------------
179+
terminate(_Reason,_State) ->
180+
ct_master_logs_fork:log("CT Master Event Handler stopping","",[]),
181+
ok.
182+
183+
%%--------------------------------------------------------------------
184+
%% Function: code_change(OldVsn, State, Extra) -> {ok, NewState}
185+
%% Description: Convert process state when code is changed
186+
%%--------------------------------------------------------------------
187+
code_change(_OldVsn,State,_Extra) ->
188+
{ok,State}.
189+
190+
%%--------------------------------------------------------------------
191+
%%% Internal functions
192+
%%--------------------------------------------------------------------
193+
194+
print(_Str,_Args) ->
195+
% io:format(_Str,_Args),
196+
ok.

deps/rabbitmq_ct_helpers/src/ct_master_fork.erl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ init_master(Parent,NodeOptsList,EvHandlers,MasterLogDir,LogDirs,
446446
end,
447447

448448
%% start master logger
449-
{MLPid,_} = ct_master_logs:start(MasterLogDir,
449+
{MLPid,_} = ct_master_logs_fork:start(MasterLogDir,
450450
[N || {N,_} <- NodeOptsList]),
451451
log(all,"Master Logger process started","~w",[MLPid]),
452452

@@ -456,13 +456,13 @@ init_master(Parent,NodeOptsList,EvHandlers,MasterLogDir,LogDirs,
456456
SpecsStr = lists:map(fun(Name) ->
457457
Name ++ " "
458458
end,Specs),
459-
ct_master_logs:log("Test Specification file(s)","~ts",
459+
ct_master_logs_fork:log("Test Specification file(s)","~ts",
460460
[lists:flatten(SpecsStr)])
461461
end,
462462

463463
%% start master event manager and add default handler
464464
{ok, _} = start_ct_master_event(),
465-
ct_master_event:add_handler(),
465+
ct_master_event_fork:add_handler(),
466466
%% add user handlers for master event manager
467467
Add = fun({H,Args}) ->
468468
log(all,"Adding Event Handler","~w",[H]),
@@ -484,7 +484,7 @@ init_master(Parent,NodeOptsList,EvHandlers,MasterLogDir,LogDirs,
484484
init_master1(Parent,NodeOptsList,InitOptions,LogDirs).
485485

486486
start_ct_master_event() ->
487-
case ct_master_event:start_link() of
487+
case ct_master_event_fork:start_link() of
488488
{error, {already_started, Pid}} ->
489489
{ok, Pid};
490490
Else ->
@@ -510,8 +510,8 @@ init_master1(Parent,NodeOptsList,InitOptions,LogDirs) ->
510510
init_master1(Parent,NodeOptsList,InitOptions1,LogDirs);
511511
_ ->
512512
log(html,"Aborting Tests","",[]),
513-
ct_master_event:stop(),
514-
ct_master_logs:stop(),
513+
ct_master_event_fork:stop(),
514+
ct_master_logs_fork:stop(),
515515
exit(aborted)
516516
end
517517
end.
@@ -546,8 +546,8 @@ master_loop(#state{node_ctrl_pids=[],
546546
log(all,"Info","Updating log files",[]),
547547
refresh_logs(LogDirs,[]),
548548

549-
ct_master_event:stop(),
550-
ct_master_logs:stop(),
549+
ct_master_event_fork:stop(),
550+
ct_master_logs_fork:stop(),
551551
ok;
552552

553553
master_loop(State=#state{node_ctrl_pids=NodeCtrlPids,
@@ -658,7 +658,7 @@ master_loop(State=#state{node_ctrl_pids=NodeCtrlPids,
658658
blocked=Blocked1});
659659

660660
{cast,Event} when is_record(Event,event) ->
661-
ct_master_event:notify(Event),
661+
ct_master_event_fork:notify(Event),
662662
master_loop(State)
663663

664664
end.
@@ -851,7 +851,7 @@ log(To,Heading,Str,Args) ->
851851
ok
852852
end,
853853
if To == all ; To == html ->
854-
ct_master_logs:log(Heading,Str,Args);
854+
ct_master_logs_fork:log(Heading,Str,Args);
855855
true ->
856856
ok
857857
end.

0 commit comments

Comments
 (0)