1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 1997-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%%
22-module(mnesia_examples_test).
23-author('hakan@erix.ericsson.se').
24-export([init_per_testcase/2, end_per_testcase/2,
25         init_per_group/2, end_per_group/2,
26         all/0, groups/0]).
27-export([bup/1, company/1, meter/1,
28         replica_test/1, sticky_replica_test/1, dist_test/1,
29         conflict_test/1, frag_test/1, frag2_test/1, remote_test/1,
30         remote_frag2_test/1, opt_load/1]).
31
32-include("mnesia_test_lib.hrl").
33
34init_per_testcase(Func, Conf) ->
35    mnesia_test_lib:init_per_testcase(Func, Conf).
36
37end_per_testcase(Func, Conf) ->
38    mnesia_test_lib:end_per_testcase(Func, Conf).
39
40-define(init(N, Config),
41	mnesia_test_lib:prepare_test_case([{init_test_case, [mnesia]},
42					   delete_schema],
43					  N, Config, ?FILE, ?LINE)).
44
45%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
46all() ->
47    [bup, company, meter, {group, tpcb}].
48
49groups() ->
50    [{tpcb, [],
51      [replica_test, sticky_replica_test, dist_test,
52       conflict_test, frag_test, frag2_test, remote_test,
53       remote_frag2_test]}].
54
55init_per_group(_GroupName, Config) ->
56    Config.
57
58end_per_group(_GroupName, Config) ->
59    Config.
60
61
62%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63bup(doc) -> ["Run the backup examples in bup.erl"];
64bup(suite) -> [];
65bup(Config) when is_list(Config) ->
66    Nodes = ?init(3, Config),
67    opt_net_load(bup),
68    ?match(ok, bup:test(Nodes)).
69
70%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71company(doc) ->
72    ["Run the company examples in company.erl and company_o.erl"].
73
74%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75
76replica_test(suite) -> [];
77replica_test(Config) when is_list(Config) ->
78    ?init(3, Config),
79    opt_net_load(mnesia_tpcb),
80    ?match({ok, _}, mnesia_tpcb:start(config(replica_test, ram_copies))).
81
82sticky_replica_test(suite) -> [];
83sticky_replica_test(Config) when is_list(Config) ->
84    ?init(3, Config),
85    opt_net_load(mnesia_tpcb),
86    ?match({ok, _}, mnesia_tpcb:start(config(sticky_replica_test, ram_copies))).
87
88dist_test(suite) -> [];
89dist_test(Config) when is_list(Config) ->
90    ?init(3, [{tc_timeout, timer:minutes(10)} | Config]),
91    opt_net_load(mnesia_tpcb),
92    ?match({ok, _}, mnesia_tpcb:start(config(dist_test, ram_copies))).
93
94conflict_test(suite) -> [];
95conflict_test(Config) when is_list(Config) ->
96    ?init(3, Config),
97    opt_net_load(mnesia_tpcb),
98    ?match({ok, _}, mnesia_tpcb:start(config(conflict_test, ram_copies))).
99
100frag_test(suite) -> [];
101frag_test(Config) when is_list(Config) ->
102    ?init(3, Config),
103    opt_net_load(mnesia_tpcb),
104    ?match({ok, _}, mnesia_tpcb:start(config(frag_test, ram_copies))).
105
106frag2_test(suite) -> [];
107frag2_test(Config) when is_list(Config) ->
108    ?init(3, Config),
109    opt_net_load(mnesia_tpcb),
110    ?match({ok, _}, mnesia_tpcb:start(config(frag2_test, ram_copies))).
111
112remote_test(suite) -> [];
113remote_test(Config) when is_list(Config) ->
114    ?init(3, Config),
115    opt_net_load(mnesia_tpcb),
116    ?match({ok, _}, mnesia_tpcb:start(config(remote_test, ram_copies))).
117
118remote_frag2_test(suite) -> [];
119remote_frag2_test(Config) when is_list(Config) ->
120    ?init(3, Config),
121    opt_net_load(mnesia_tpcb),
122    ?match({ok, _}, mnesia_tpcb:start(config(remote_frag2_test, ram_copies))).
123
124%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125meter(doc) ->
126    ["Run the meter example in mnesia_meter.erl"];
127meter(suite) ->
128    [];
129meter(Config) when is_list(Config) ->
130    [N | _] = ?init(3, Config),
131    opt_net_load(mnesia_meter),
132    ?match(ok, mnesia_meter:go(ram_copies, [N])).
133
134%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
135
136config(Test, Type) ->
137    Config0 = mnesia_tpcb:config(Test, Type),
138    %% Cut the times, the idea is to test the example and configuration
139    %% not running the test a long time
140    Config1 = lists:keyreplace(stop_after, 1, Config0, {stop_after, 6000}),
141    lists:keyreplace(report_interval, 1, Config1, {report_interval, 1000}).
142
143opt_net_load(ExampleMod) ->
144    opt_net_load([node() | nodes()], ExampleMod, ok).
145
146opt_net_load([Node | Nodes], ExampleMod, Res) ->
147    case rpc:call(Node, ?MODULE, opt_load, [ExampleMod]) of
148	{module, ExampleMod} ->
149	    opt_net_load(Nodes, ExampleMod, Res);
150	{error, Reason} ->
151	    Error = {opt_net_load, ExampleMod, Node, Reason},
152	    opt_net_load(Nodes, ExampleMod, {error, Error});
153	{badrpc, Reason} ->
154	    Error = {opt_net_load, ExampleMod, Node, Reason},
155	    opt_net_load(Nodes, ExampleMod, {error, Error})
156    end;
157opt_net_load([], _ExampleMod, Res) ->
158    Res.
159
160opt_load(Mod) ->
161    case code:is_loaded(Mod) of
162	{file, _} ->
163	    {module, Mod};
164	false ->
165	    Abs = filename:join([code:lib_dir(mnesia), examples, Mod]),
166	    code:load_abs(Abs)
167    end.
168