1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2009-2018. 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-module(ac_SUITE).
22
23-compile(export_all).
24
25-include_lib("common_test/include/ct.hrl").
26
27%%--------------------------------------------------------------------
28%% @spec suite() -> Info
29%% Info = [tuple()]
30%% @end
31%%--------------------------------------------------------------------
32suite() ->
33    [{timetrap,{seconds,30}}].
34
35%%--------------------------------------------------------------------
36%% @spec init_per_suite(Config0) ->
37%%     Config1 | {skip,Reason} | {skip_and_save,Reason,Config1}
38%% Config0 = Config1 = [tuple()]
39%% Reason = term()
40%% @end
41%%--------------------------------------------------------------------
42init_per_suite(Config) ->
43    start_processes(),
44    Config.
45
46%%--------------------------------------------------------------------
47%% @spec end_per_suite(Config0) -> term() | {save_config,Config1}
48%% Config0 = Config1 = [tuple()]
49%% @end
50%%--------------------------------------------------------------------
51end_per_suite(_Config) ->
52    start_processes(),
53    ok.
54
55%%--------------------------------------------------------------------
56%% @spec init_per_group(GroupName, Config0) ->
57%%               Config1 | {skip,Reason} | {skip_and_save,Reason,Config1}
58%% GroupName = atom()
59%% Config0 = Config1 = [tuple()]
60%% Reason = term()
61%% @end
62%%--------------------------------------------------------------------
63init_per_group(_GroupName, Config) ->
64    start_processes(),
65    Config.
66
67%%--------------------------------------------------------------------
68%% @spec end_per_group(GroupName, Config0) ->
69%%               term() | {save_config,Config1}
70%% GroupName = atom()
71%% Config0 = Config1 = [tuple()]
72%% @end
73%%--------------------------------------------------------------------
74end_per_group(_GroupName, _Config) ->
75    start_processes(),
76    ok.
77
78%%--------------------------------------------------------------------
79%% @spec init_per_testcase(TestCase, Config0) ->
80%%               Config1 | {skip,Reason} | {skip_and_save,Reason,Config1}
81%% TestCase = atom()
82%% Config0 = Config1 = [tuple()]
83%% Reason = term()
84%% @end
85%%--------------------------------------------------------------------
86init_per_testcase(_TestCase, Config) ->
87    start_processes(),
88    Config.
89
90%%--------------------------------------------------------------------
91%% @spec end_per_testcase(TestCase, Config0) ->
92%%               term() | {save_config,Config1} | {fail,Reason}
93%% TestCase = atom()
94%% Config0 = Config1 = [tuple()]
95%% Reason = term()
96%% @end
97%%--------------------------------------------------------------------
98end_per_testcase(_TestCase, _Config) ->
99    start_processes(),
100    ok.
101
102%%--------------------------------------------------------------------
103%% @spec groups() -> [Group]
104%% Group = {GroupName,Properties,GroupsAndTestCases}
105%% GroupName = atom()
106%% Properties = [parallel | sequence | Shuffle | {RepeatType,N}]
107%% GroupsAndTestCases = [Group | {group,GroupName} | TestCase]
108%% TestCase = atom()
109%% Shuffle = shuffle | {shuffle,{integer(),integer(),integer()}}
110%% RepeatType = repeat | repeat_until_all_ok | repeat_until_all_fail |
111%%              repeat_until_any_ok | repeat_until_any_fail
112%% N = integer() | forever
113%% @end
114%%--------------------------------------------------------------------
115groups() ->
116    [{s1,[],[stc1,stc2]},
117     {p1,[parallel],[ptc1,ptc2]},
118     {s2,[],[stc1,stc2]}].
119
120%%! What about nested groups??
121
122%%--------------------------------------------------------------------
123%% @spec all() -> GroupsAndTestCases | {skip,Reason}
124%% GroupsAndTestCases = [{group,GroupName} | TestCase]
125%% GroupName = atom()
126%% TestCase = atom()
127%% Reason = term()
128%% @end
129%%--------------------------------------------------------------------
130all() ->
131    [
132     [tc1,tc2],
133     {group,s1},
134     {group,p1},
135     {group,s2},
136     tc1
137    ].
138
139tc1(_Config) ->
140    start_processes(),
141    ok.
142
143tc2(_Config) ->
144    start_processes(),
145    ok.
146
147stc1(_Config) ->
148    start_processes(),
149    ok.
150
151stc2(_Config) ->
152    start_processes(),
153    ok.
154
155ptc1(_Config) ->
156    start_processes(),
157    ok.
158
159ptc2(_Config) ->
160    start_processes(),
161    ok.
162
163
164%%%-----------------------------------------------------------------
165%%%
166
167start_processes() ->
168    Init = fun() ->
169                   process_flag(trap_exit, true),
170                   do_spawn(fun() -> receive _ -> ok end end),
171                   receive _ ->
172                           ok
173                   end
174           end,
175    do_spawn(Init).
176
177do_spawn(Fun) ->
178    Pid = spawn(Fun),
179    ct:log("Process ~w started with group leader ~w",
180           [Pid,element(2, process_info(Pid, group_leader))]),
181    Pid.
182