1: %%
    2: %% %CopyrightBegin%
    3: %%
    4: %% Copyright Ericsson AB 1997-2013. All Rights Reserved.
    5: %%
    6: %% The contents of this file are subject to the Erlang Public License,
    7: %% Version 1.1, (the "License"); you may not use this file except in
    8: %% compliance with the License. You should have received a copy of the
    9: %% Erlang Public License along with this software. If not, it can be
   10: %% retrieved online at http://www.erlang.org/.
   11: %%
   12: %% Software distributed under the License is distributed on an "AS IS"
   13: %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
   14: %% the License for the specific language governing rights and limitations
   15: %% under the License.
   16: %%
   17: %% %CopyrightEnd%
   18: %%
   19: 
   20: %%
   21: -module(mnesia_examples_test).
   22: -author('hakan@erix.ericsson.se').
   23: -compile([export_all]).
   24: -include("mnesia_test_lib.hrl").
   25: 
   26: init_per_testcase(Func, Conf) ->
   27:     mnesia_test_lib:init_per_testcase(Func, Conf).
   28: 
   29: end_per_testcase(Func, Conf) ->
   30:     mnesia_test_lib:end_per_testcase(Func, Conf).
   31: 
   32: -define(init(N, Config),
   33: 	mnesia_test_lib:prepare_test_case([{init_test_case, [mnesia]},
   34: 					   delete_schema],
   35: 					  N, Config, ?FILE, ?LINE)).
   36: 
   37: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   38: all() -> 
   39:     [bup, company, meter, {group, tpcb}].
   40: 
   41: groups() -> 
   42:     [{tpcb, [],
   43:       [replica_test, sticky_replica_test, dist_test,
   44:        conflict_test, frag_test, frag2_test, remote_test,
   45:        remote_frag2_test]}].
   46: 
   47: init_per_group(_GroupName, Config) ->
   48:     Config.
   49: 
   50: end_per_group(_GroupName, Config) ->
   51:     Config.
   52: 
   53: 
   54: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   55: bup(doc) -> ["Run the backup examples in bup.erl"];
   56: bup(suite) -> [];
   57: bup(Config) when is_list(Config) ->
   58:     Nodes = ?init(3, Config),
   59:     opt_net_load(bup),
   60:     ?match(ok, bup:test(Nodes)).
   61: 
   62: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   63: company(doc) ->
   64:     ["Run the company examples in company.erl and company_o.erl"].
   65: 
   66: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   67: 
   68: replica_test(suite) -> [];
   69: replica_test(Config) when is_list(Config) ->
   70:     ?init(3, Config),
   71:     opt_net_load(mnesia_tpcb),
   72:     ?match({ok, _}, mnesia_tpcb:start(config(replica_test, ram_copies))).
   73: 
   74: sticky_replica_test(suite) -> [];
   75: sticky_replica_test(Config) when is_list(Config) ->
   76:     ?init(3, Config),
   77:     opt_net_load(mnesia_tpcb),
   78:     ?match({ok, _}, mnesia_tpcb:start(config(sticky_replica_test, ram_copies))).
   79: 
   80: dist_test(suite) -> [];
   81: dist_test(Config) when is_list(Config) ->
   82:     ?init(3, [{tc_timeout, timer:minutes(10)} | Config]),
   83:     opt_net_load(mnesia_tpcb),
   84:     ?match({ok, _}, mnesia_tpcb:start(config(dist_test, ram_copies))).
   85: 
   86: conflict_test(suite) -> [];
   87: conflict_test(Config) when is_list(Config) ->
   88:     ?init(3, Config),
   89:     opt_net_load(mnesia_tpcb),
   90:     ?match({ok, _}, mnesia_tpcb:start(config(conflict_test, ram_copies))).
   91: 
   92: frag_test(suite) -> [];
   93: frag_test(Config) when is_list(Config) ->
   94:     ?init(3, Config),
   95:     opt_net_load(mnesia_tpcb),
   96:     ?match({ok, _}, mnesia_tpcb:start(config(frag_test, ram_copies))).
   97: 
   98: frag2_test(suite) -> [];
   99: frag2_test(Config) when is_list(Config) ->
  100:     ?init(3, Config),
  101:     opt_net_load(mnesia_tpcb),
  102:     ?match({ok, _}, mnesia_tpcb:start(config(frag2_test, ram_copies))).
  103: 
  104: remote_test(suite) -> [];
  105: remote_test(Config) when is_list(Config) ->
  106:     ?init(3, Config),
  107:     opt_net_load(mnesia_tpcb),
  108:     ?match({ok, _}, mnesia_tpcb:start(config(remote_test, ram_copies))).
  109: 
  110: remote_frag2_test(suite) -> [];
  111: remote_frag2_test(Config) when is_list(Config) ->
  112:     ?init(3, Config),
  113:     opt_net_load(mnesia_tpcb),
  114:     ?match({ok, _}, mnesia_tpcb:start(config(remote_frag2_test, ram_copies))).
  115: 
  116: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  117: meter(doc) ->
  118:     ["Run the meter example in mnesia_meter.erl"];
  119: meter(suite) ->
  120:     [];
  121: meter(Config) when is_list(Config) ->
  122:     [N | _] = ?init(3, Config),
  123:     opt_net_load(mnesia_meter),
  124:     ?match(ok, mnesia_meter:go(ram_copies, [N])).
  125: 
  126: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  127: 
  128: config(Test, Type) ->
  129:     Config0 = mnesia_tpcb:config(Test, Type),
  130:     %% Cut the times, the idea is to test the example and configuration
  131:     %% not running the test a long time
  132:     Config1 = lists:keyreplace(stop_after, 1, Config0, {stop_after, 6000}),
  133:     lists:keyreplace(report_interval, 1, Config1, {report_interval, 1000}).
  134: 
  135: opt_net_load(ExampleMod) ->
  136:     opt_net_load([node() | nodes()], ExampleMod, ok).
  137: 
  138: opt_net_load([Node | Nodes], ExampleMod, Res) ->
  139:     case rpc:call(Node, ?MODULE, opt_load, [ExampleMod]) of
  140: 	{module, ExampleMod} ->
  141: 	    opt_net_load(Nodes, ExampleMod, Res);
  142: 	{error, Reason} ->
  143: 	    Error = {opt_net_load, ExampleMod, Node, Reason},
  144: 	    opt_net_load(Nodes, ExampleMod, {error, Error});
  145: 	{badrpc, Reason} ->
  146: 	    Error = {opt_net_load, ExampleMod, Node, Reason},
  147: 	    opt_net_load(Nodes, ExampleMod, {error, Error})
  148:     end;
  149: opt_net_load([], _ExampleMod, Res) ->
  150:     Res.
  151: 
  152: opt_load(Mod) ->
  153:     case code:is_loaded(Mod) of
  154: 	{file, _} ->
  155: 	    {module, Mod};
  156: 	false ->
  157: 	    Abs = filename:join([code:lib_dir(mnesia), examples, Mod]),
  158: 	    code:load_abs(Abs)
  159:     end.