aboutsummaryrefslogtreecommitdiffstats
path: root/lib/mnesia/test/mnesia_majority_test.erl
blob: 41ba0fd6011c6de487b32ce4e5938bce901de66c (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2010-2011. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%

%%
-module(mnesia_majority_test).
-author('ulf.wiger@erlang-solutions.com').
-compile(export_all).
-include("mnesia_test_lib.hrl").

init_per_testcase(Func, Conf) ->
    mnesia_test_lib:init_per_testcase(Func, Conf).

end_per_testcase(Func, Conf) ->
    mnesia_test_lib:end_per_testcase(Func, Conf).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
all() ->
    [
     write
     , wread
     , delete
     , clear_table
     , frag
     , change_majority
     , frag_change_majority
    ].

write(suite) -> [];
write(Config) when is_list(Config) ->
    [N1, N2, N3] = ?acquire_nodes(3, Config),
    Tab = t,
    Schema = [{name, Tab}, {ram_copies, [N1,N2,N3]}, {majority,true}],
    ?match({atomic, ok}, mnesia:create_table(Schema)),
    ?match({[ok,ok,ok],[]},
	   rpc:multicall([N1,N2,N3], mnesia, wait_for_tables, [[Tab], 3000])),
    ?match({atomic,ok},
	   mnesia:transaction(fun() -> mnesia:write({t,1,a}) end)),
    mnesia_test_lib:kill_mnesia([N3]),
    ?match({atomic,ok},
	   mnesia:transaction(fun() -> mnesia:write({t,1,a}) end)),
    mnesia_test_lib:kill_mnesia([N2]),
    ?match({aborted,{no_majority,Tab}},
	   mnesia:transaction(fun() -> mnesia:write({t,1,a}) end)).

wread(suite) -> [];
wread(Config) when is_list(Config) ->
    [N1, N2] = ?acquire_nodes(2, Config),
    Tab = t,
    Schema = [{name, Tab}, {ram_copies, [N1,N2]}, {majority,true}],
    ?match({atomic, ok}, mnesia:create_table(Schema)),
    ?match({[ok,ok],[]},
	   rpc:multicall([N1,N2], mnesia, wait_for_tables, [[Tab], 3000])),
    ?match({atomic,[]},
	   mnesia:transaction(fun() -> mnesia:read(t,1,write) end)),
    mnesia_test_lib:kill_mnesia([N2]),
    ?match({aborted,{no_majority,Tab}},
	   mnesia:transaction(fun() -> mnesia:read(t,1,write) end)).

delete(suite) -> [];
delete(Config) when is_list(Config) ->
    [N1, N2] = ?acquire_nodes(2, Config),
    Tab = t,
    Schema = [{name, Tab}, {ram_copies, [N1,N2]}, {majority,true}],
    ?match({atomic, ok}, mnesia:create_table(Schema)),
    ?match({[ok,ok],[]},
	   rpc:multicall([N1,N2], mnesia, wait_for_tables, [[Tab], 3000])),
    %% works as expected with majority of nodes present
    ?match({atomic,ok},
	   mnesia:transaction(fun() -> mnesia:write({t,1,a}) end)),
    ?match({atomic,ok},
	   mnesia:transaction(fun() -> mnesia:delete({t,1}) end)),
    ?match({atomic,[]},
	   mnesia:transaction(fun() -> mnesia:read({t,1}) end)),
    %% put the record back
    ?match({atomic,ok},
	   mnesia:transaction(fun() -> mnesia:write({t,1,a}) end)),
    ?match({atomic,[{t,1,a}]},
	   mnesia:transaction(fun() -> mnesia:read({t,1}) end)),
    mnesia_test_lib:kill_mnesia([N2]),
    ?match({aborted,{no_majority,Tab}},
	   mnesia:transaction(fun() -> mnesia:delete({t,1}) end)).

clear_table(suite) -> [];
clear_table(Config) when is_list(Config) ->
    [N1, N2] = ?acquire_nodes(2, Config),
    Tab = t,
    Schema = [{name, Tab}, {ram_copies, [N1,N2]}, {majority,true}],
    ?match({atomic, ok}, mnesia:create_table(Schema)),
    ?match({[ok,ok],[]},
	   rpc:multicall([N1,N2], mnesia, wait_for_tables, [[Tab], 3000])),
    %% works as expected with majority of nodes present
    ?match({atomic,ok},
	   mnesia:transaction(fun() -> mnesia:write({t,1,a}) end)),
    ?match({atomic,ok}, mnesia:clear_table(t)),
    ?match({atomic,[]},
	   mnesia:transaction(fun() -> mnesia:read({t,1}) end)),
    %% put the record back
    ?match({atomic,ok},
	   mnesia:transaction(fun() -> mnesia:write({t,1,a}) end)),
    ?match({atomic,[{t,1,a}]},
	   mnesia:transaction(fun() -> mnesia:read({t,1}) end)),
    mnesia_test_lib:kill_mnesia([N2]),
    ?match({aborted,{no_majority,Tab}}, mnesia:clear_table(t)).

frag(suite) -> [];
frag(Config) when is_list(Config) ->
    [N1] = ?acquire_nodes(1, Config),
    Tab = t,
    Schema = [
	      {name, Tab}, {ram_copies, [N1]},
	      {majority,true},
	      {frag_properties, [{n_fragments, 2}]}
	     ],
    ?match({atomic, ok}, mnesia:create_table(Schema)),
    ?match(true, mnesia:table_info(t, majority)),
    ?match(true, mnesia:table_info(t_frag2, majority)).

change_majority(suite) -> [];
change_majority(Config) when is_list(Config) ->
    [N1,N2] = ?acquire_nodes(2, Config),
    Tab = t,
    Schema = [
	      {name, Tab}, {ram_copies, [N1,N2]},
	      {majority,false}
	     ],
    ?match({atomic, ok}, mnesia:create_table(Schema)),
    ?match(false, mnesia:table_info(t, majority)),
    ?match({atomic, ok},
	   mnesia:change_table_majority(t, true)),
    ?match(true, mnesia:table_info(t, majority)),
    ?match(ok,
	   mnesia:activity(transaction, fun() ->
						mnesia:write({t,1,a})
					end)),
    mnesia_test_lib:kill_mnesia([N2]),
    ?match({'EXIT',{aborted,{no_majority,_}}},
	   mnesia:activity(transaction, fun() ->
						mnesia:write({t,1,a})
					end)).

frag_change_majority(suite) -> [];
frag_change_majority(Config) when is_list(Config) ->
    [N1,N2] = ?acquire_nodes(2, Config),
    Tab = t,
    Schema = [
	      {name, Tab}, {ram_copies, [N1,N2]},
	      {majority,false},
	      {frag_properties,
	       [{n_fragments, 2},
		{n_ram_copies, 2},
		{node_pool, [N1,N2]}]}
	     ],
    ?match({atomic, ok}, mnesia:create_table(Schema)),
    ?match(false, mnesia:table_info(t, majority)),
    ?match(false, mnesia:table_info(t_frag2, majority)),
    ?match({aborted,{bad_type,t_frag2}},
	   mnesia:change_table_majority(t_frag2, true)),
    ?match({atomic, ok},
	   mnesia:change_table_majority(t, true)),
    ?match(true, mnesia:table_info(t, majority)),
    ?match(true, mnesia:table_info(t_frag2, majority)),
    ?match(ok,
	   mnesia:activity(transaction, fun() ->
						mnesia:write({t,1,a})
					end, mnesia_frag)),
    mnesia_test_lib:kill_mnesia([N2]),
    ?match({'EXIT',{aborted,{no_majority,_}}},
	   mnesia:activity(transaction, fun() ->
						mnesia:write({t,1,a})
					end, mnesia_frag)).