Skip to content

Commit

Permalink
Added a test suite for a simple split case
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnthink committed Apr 29, 2011
1 parent 99eb704 commit 2361b77
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 0 deletions.
2 changes: 2 additions & 0 deletions test/config/cover.conf
@@ -0,0 +1,2 @@
{level, details}.
{incl_mods, [unsplit, unsplit_lib, unsplit_reporter, unsplit_server, unsplit_vclock]}.
6 changes: 6 additions & 0 deletions test/run.sh
@@ -0,0 +1,6 @@
!# /bin/sh
cd ../src; make; cd -;
run_test -pa ../ebin -include ../src/ -dir $PWD -cover config/cover.conf -logdir $PWD/log;
rm *.beam;
cd ../src; make clean

2 changes: 2 additions & 0 deletions test/test/config/cover.conf
@@ -0,0 +1,2 @@
{level, details}.
{incl_mods, [unsplit, unsplit_lib, unsplit_reporter, unsplit_server, unsplit_vclock]}.
6 changes: 6 additions & 0 deletions test/test/run.sh
@@ -0,0 +1,6 @@
!# /bin/sh
cd ../src; make; cd -;
run_test -pa ../ebin -include ../src/ -dir $PWD -cover config/cover.conf -logdir $PWD/log;
rm *.beam;
cd ../src; make clean

76 changes: 76 additions & 0 deletions test/test/unsplit_lib_SUITE.erl
@@ -0,0 +1,76 @@
%%% @doc
%%% Test suite for unsplit
%%% @copyright Nimbuzz B.V.
%%% @author Ahmed Omar <omar@nimbuzz.nl>
-module(unsplit_lib_SUITE).
%%% @end
-include_lib("eunit/include/eunit.hrl").
%%% Include files
-include_lib("common_test/include/ct.hrl").

%%% External exports
-compile(export_all).
-define(TABLE, test1).
-define(SNODE, mn7).
-record(?TABLE,{key,modified=erlang:now(),value}).
%%% Macros

all() ->
[split].


init_per_suite(Conf) ->
{ok, Host} = inet:gethostname(),
ct_slave:start(list_to_atom(Host), ?SNODE, [{erl_flags, "-pa ../../../ebin/"}]),
Conf.

end_per_suite(_Conf) ->
{ok, Host} = inet:gethostname(),
ct_slave:stop(list_to_atom(Host), ?SNODE),
ok.

init_per_testcase(Case, Conf) ->
ct:print("Test case ~p started", [Case]),
Conf.

end_per_testcase(Case, Conf) ->
ct:print("Test case ~p finished", [Case]),
Conf.

split()->
[{userdata, [{doc, "Tests split network"}]}].
split(_Conf)->
RemoteNode = hd(nodes()),
mnesia:delete_schema([node()|nodes()]),
ok = mnesia:start(),
ok = application:start(unsplit),
ok = rpc:call(RemoteNode, mnesia, start,[]),
ok = rpc:call(RemoteNode, application, start, [unsplit]),
mnesia:create_schema([node()|nodes()]),
mnesia:change_config(extra_db_nodes, nodes()),
mnesia:delete_table(?TABLE),
mnesia:create_table(?TABLE ,[{ram_copies,[node()|nodes()]},
{attributes,[key,modified,value]},
{user_properties,[{unsplit_method,{unsplit_lib,last_modified,[]}}]}]),
ct:print("inserting records~n"),
mnesia:transaction(fun() -> mnesia:write(#?TABLE{key=1,value=a}) end),
mnesia:transaction(fun() -> mnesia:write(#?TABLE{key=2,value=a}) end),
ct:print("disconnecting nodes~n"),
disconnect_node(RemoteNode),
mnesia:transaction(fun() -> mnesia:write(#?TABLE{key=4,value=b}) end),
mnesia:transaction(fun() -> mnesia:write(#?TABLE{key=5,value=b}) end),
ct:print("inserting records on one node, while the other one is disconnected~n"),
timer:sleep(4000),
ct:print("reconnecting nodes~n"),
net_kernel:connect_node(RemoteNode),
timer:sleep(20000),
true = compare_table_size(RemoteNode, test),
mnesia:delete_table(?TABLE).


compare_table_size(RemoteNode, Table)->
rpc:call(RemoteNode, mnesia, table_info,[Table, size])==
mnesia:table_info(Table, size).
print_table_size(RemoteNode, Table)->
ct:print("~p, ~p~n",[rpc:call(RemoteNode, mnesia, table_info,[Table, size]),
mnesia:table_info(Table, size)]).
78 changes: 78 additions & 0 deletions test/unsplit_lib_SUITE.erl
@@ -0,0 +1,78 @@
%%% @doc
%%% Unit test for sleep mode
%%% @copyright
%%% @author Ahmed Omar <omar@nimbuzz.nl>
-module(unsplit_lib_SUITE).
%%% @end
-include_lib("eunit/include/eunit.hrl").
%%% Include files
-include_lib("common_test/include/ct.hrl").

%%% External exports
-compile(export_all).
-define(TABLE, test1).
-define(SNODE, mn7).
-record(?TABLE,{key,modified=erlang:now(),value}).
%%% Macros

all() ->
[split].


init_per_suite(Conf) ->
{ok, Host} = inet:gethostname(),
ct_slave:start(list_to_atom(Host), ?SNODE, [{erl_flags, "-pa ../../../ebin/"}]),
Conf.

end_per_suite(_Conf) ->
application:stop(unsplit),
application:stop(mnesia),
{ok, Host} = inet:gethostname(),
ct_slave:stop(list_to_atom(Host), ?SNODE),
ok.

init_per_testcase(Case, Conf) ->
ct:print("Test case ~p started", [Case]),
Conf.

end_per_testcase(Case, Conf) ->
ct:print("Test case ~p finished", [Case]),
Conf.

split()->
[{userdata, [{doc, "Tests split network"}]}].
split(_Conf)->
RemoteNode = hd(nodes()),
mnesia:delete_schema([node()|nodes()]),
ok = mnesia:start(),
ok = application:start(unsplit),
ok = rpc:call(RemoteNode, mnesia, start,[]),
ok = rpc:call(RemoteNode, application, start, [unsplit]),
mnesia:create_schema([node()|nodes()]),
mnesia:change_config(extra_db_nodes, nodes()),
mnesia:delete_table(?TABLE),
mnesia:create_table(?TABLE ,[{ram_copies,[node()|nodes()]},
{attributes,[key,modified,value]},
{user_properties,[{unsplit_method,{unsplit_lib,last_modified,[]}}]}]),
ct:print("inserting records~n"),
mnesia:transaction(fun() -> mnesia:write(#?TABLE{key=1,value=a}) end),
mnesia:transaction(fun() -> mnesia:write(#?TABLE{key=2,value=a}) end),
ct:print("disconnecting nodes~n"),
disconnect_node(RemoteNode),
mnesia:transaction(fun() -> mnesia:write(#?TABLE{key=4,value=b}) end),
mnesia:transaction(fun() -> mnesia:write(#?TABLE{key=5,value=b}) end),
ct:print("inserting records on one node, while the other one is disconnected~n"),
timer:sleep(4000),
ct:print("reconnecting nodes~n"),
net_kernel:connect_node(RemoteNode),
timer:sleep(5000),
true = compare_table_size(RemoteNode, test),
mnesia:delete_table(?TABLE).


compare_table_size(RemoteNode, Table)->
rpc:call(RemoteNode, mnesia, table_info,[Table, size])==
mnesia:table_info(Table, size).
print_table_size(RemoteNode, Table)->
ct:print("~p, ~p~n",[rpc:call(RemoteNode, mnesia, table_info,[Table, size]),
mnesia:table_info(Table, size)]).

0 comments on commit 2361b77

Please sign in to comment.