aboutsummaryrefslogtreecommitdiffstats
path: root/lib/test_server/src/things/mnesia_power_SUITE.erl
blob: 84a41d6594300c48f41b98c789a50fa35ca7acb6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 1996-2009. All Rights Reserved.
%% 
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%% 
%% %CopyrightEnd%
%%
-module(mnesia_power_SUITE).
-compile([export_all]).
%%-define(line_trace,1).
-include_lib("common_test/include/ct.hrl").

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

all(suite) -> [run].

-define(iterations,3). %% nof power-off cycles to do before acceptance
-define(rows,8). %% nof database rows to use (not too big, please)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

-record(sum_table_1,{row,a,b,c,s}).

run(suite) -> [];
run(Config) ->
    ?line mnesia:create_schema([node()]),
    ?line mnesia:start(),
    ?line mnesia:create_table([{name, sum_table_1}, {disc_copies,[node()]},
			       {attributes,record_info(fields,sum_table_1)}]),
    ?line run_test(Config,?iterations).

run(Config,N) ->
    ?line mnesia:start(),
    ?line check_consistency(sum_table_1),
    case N of
	0 -> ?line ok;
	N -> ?line run_test(Config,N)
    end.
    
run_test(Config,N) ->
    ?line Pid1a = start_manipulator(sum_table_1),
    ?line Pid1b = start_manipulator(sum_table_1),
    ?line Pid1c = start_manipulator(sum_table_1),
    ?line test_server:resume_point(?MODULE,run,[Config,N-1]),
    ?line test_server:format(1,"Manipulating data like crazy now, "
		       "power off any time..."),
    ?line test_server:sleep(infinity).

start_manipulator(Table) ->
    ?line spawn_link(?MODULE,manipulator_init,[Table]).

manipulator_init(Table) ->
    random:seed(4711,0,0),
    manipulator(0,Table).

manipulator(N,Table) ->
    ?line Fun = 
	fun() ->
		?line Row = random:uniform(?rows),
		?line A = random:uniform(100000),
		?line B = random:uniform(100000),
		?line C = random:uniform(100000),
		?line Sum = A+B+C,
		?line case mnesia:write(#sum_table_1
					{row=Row,a=A,b=B,c=C,s=Sum}) of
			  ok -> ok;
			  Other ->
			      ?line io:format("Trans failed: ~p\n",[Other])
		      end
	end,
    ?line mnesia:transaction(Fun),
    case mnesia:table_info(sum_table_1,size) of
	0 -> exit(still_empty);
	_ -> ok
    end,
    case N rem 2000 of
	0 -> io:format("~p did ~p operations",[self(),N]),
	     check_consistency(sum_table_1);
	_ -> ok
    end,
    ?line manipulator(N+1,Table).

check_consistency(Table) ->
    io:format("Checking consistency of table ~p\n",[Table]),
    All = mnesia:table_info(Table,wild_pattern),
    ?line Fun =
        fun() ->
		mnesia:match_object(All)
        end,
  ?line case mnesia:transaction(Fun) of
	    {atomic,Val} ->
		check_consistency_rows(Val,0);
	    Other ->
		io:format("Trans failed: ~p\n",[Other]),
		exit(failed),
		check_consistency(Table)
	  end.

check_consistency_rows([#sum_table_1{a=A,b=B,c=C,s=Sum}|Rows],N) ->
    ?line Sum=A+B+C,
    ?line check_consistency_rows(Rows,N+1);
check_consistency_rows([],N) ->
    io:format("All ~p rows were consistent\n",[N]),
    {ok,N};
check_consistency_rows(Thing,N) ->
    io:format("Mnesia transaction returned:\n~p\n",[Thing]),
    exit({bad_format,Thing}).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%