From e660dd48e78f1a368010bbcf4455b576dd59d7c1 Mon Sep 17 00:00:00 2001 From: Brad Anderson Date: Thu, 5 Aug 2010 13:41:53 -0400 Subject: n_val tests for mem3_util --- test/01-config-default.ini | 2 + test/mem3_test.erl | 164 --------------------------------------------- test/mem3_util_test.erl | 140 ++++++++++++++++++++++++++++++++++++++ test/partitions_test.erl | 53 --------------- test/test_suite.erl | 9 --- 5 files changed, 142 insertions(+), 226 deletions(-) create mode 100644 test/01-config-default.ini delete mode 100644 test/mem3_test.erl create mode 100644 test/mem3_util_test.erl delete mode 100644 test/partitions_test.erl delete mode 100644 test/test_suite.erl (limited to 'test') diff --git a/test/01-config-default.ini b/test/01-config-default.ini new file mode 100644 index 00000000..757f7830 --- /dev/null +++ b/test/01-config-default.ini @@ -0,0 +1,2 @@ +[cluster] +n=3 diff --git a/test/mem3_test.erl b/test/mem3_test.erl deleted file mode 100644 index 01d80eb0..00000000 --- a/test/mem3_test.erl +++ /dev/null @@ -1,164 +0,0 @@ --module(mem3_test). - --include("../include/common.hrl"). --include("../include/config.hrl"). --include_lib("eunit/include/eunit.hrl"). - -%% version 3 of membership state --record(mem, {header=3, - nodes=[], - clock=[], - args - }). - --define(TEST_NODE_NAME, a). --define(HINT_C1, 365375409332725729550921208179070754913983135744). --define(HINT_C2, 1096126227998177188652763624537212264741949407232). --define(PARTS_FOR_D1, [365375409332725729550921208179070754913983135744, - 548063113999088594326381812268606132370974703616, - 730750818665451459101842416358141509827966271488, - 913438523331814323877303020447676887284957839360, - 1096126227998177188652763624537212264741949407232, - 1278813932664540053428224228626747642198940975104]). --define(x40, 365375409332725729550921208179070754913983135744). --define(x60, 548063113999088594326381812268606132370974703616). - -%% TEST SETUP - -all_tests_test_() -> - {"membership3 tests", - [ - {setup, - fun test_setup/0, - fun test_teardown/1, - fun(Pid) -> - {with, Pid, - [ - fun init/1, - fun clock/1, - fun join_init/1, - fun join_init_with_hints/1, - fun join_new_node/1, - fun join_two_new_nodes/1, - fun join_with_wrong_order/1 - ]} - end} - ] - }. - - -test_setup() -> - % Config = #config{n=3,r=2,w=2,q=3,directory="/srv/db", - % storage_mod="dynomite_couch_storage"}, - {ok, Pid} = mem3:start_link([{test,?TEST_NODE_NAME}]), - Pid. - - -test_teardown(Pid) -> - exit(Pid, shutdown). - - -%% TESTS - -init(_Pid) -> - {ok, #mem{args=Args}} = mem3:state(), - Test = proplists:get_value(test, Args), - ?assertEqual(?TEST_NODE_NAME, Test). - - -clock(_Pid) -> - {ok, Clock} = mem3:clock(), - ?assertMatch([], Clock). - - -join_init(_Pid) -> - mem3:reset(), - mem3:join(init, [{1, a, []}, {2, b, []}], nil), - {ok, Nodes} = mem3:nodes(), - ?assertEqual(2, length(Nodes)), - ok. - - -join_init_with_hints(_Pid) -> - mem3:reset(), - mem3:join(init, [{1, a, []}, - {2, b, []}, - {3, c, [{hints, [?HINT_C1, ?HINT_C2]}]}, - {4, d, []}, - {5, e, []}], - nil), - {ok, Nodes} = mem3:nodes(), - ?assertEqual(5, length(Nodes)), - %?debugFmt("~nFullmap: ~p~n", [Fullmap]), -% ?assertEqual([c,d,e], mem3:nodes_for_part(?HINT_C1)), -% ?assertEqual([c,d,e], mem3:nodes_for_part(?HINT_C2)), - ok. - - -join_new_node(_Pid) -> - mem3:reset(), - mem3:join(init, [{1, a, []}, {2, b, []}, {3, c, []}], nil), - {ok, Nodes1} = mem3:nodes(), - ?assertEqual(3, length(Nodes1)), - mem3:join(join, [{4, d, []}], a), - {ok, Nodes2} = mem3:nodes(), - ?assertEqual(4, length(Nodes2)), - ok. - - -join_two_new_nodes(_Pid) -> - mem3:reset(), - mem3:join(init, [{1, a, []}, {2, b, []}, {3, c, []}], nil), - {ok, Nodes1} = mem3:nodes(), - ?assertEqual(3, length(Nodes1)), - Res = mem3:join(join, [{4, d, []}, {5, e, []}], b), - ?assertEqual(ok, Res), - {ok, Nodes2} = mem3:nodes(), - ?assertEqual(5, length(Nodes2)), - %?debugFmt("~nFullmap: ~p~n", [mem3:fullmap()]), - ok. - - -join_with_wrong_order(_Pid) -> - mem3:reset(), - mem3:join(init, [{1, a, []}, {2, b, []}, {3, c, []}], nil), -% ?assertEqual([], mem3:parts_for_node(d)), - %?debugFmt("~nFullmap: ~p~n", [mem3:fullmap()]), - Res = mem3:join(join, [{3, d, []}], c), - ?assertEqual({error, <<"position_exists_3">>}, Res), - %?debugFmt("~nFullmap: ~p~n", [mem3:fullmap()]), - ok. - - -%% -%% tests without running gen_server -%% -merge_nodes_test() -> - A = [{1,a1,[]},{2,a2,[]},{3,a3,[]}], - B = [{1,a1,[]},{2,a2,[]},{3,b3,[]}], - ?assertEqual(A, mem3:merge_nodes(A,B)), - ?assertEqual(mem3:merge_nodes(A,B), mem3:merge_nodes(B,A)), - C = [{1,c1,[]},{2,c2,[]},{3,c3,[]}], - ?assertEqual(A, mem3:merge_nodes(A,C)), - ?assertEqual(A, mem3:merge_nodes(C,A)), - ok. - - -merge_nodes_with_init_nodelist_test() -> - A = [{1,a1,[]},{2,a2,[]},{3,a3,[]}], - B = [{0, b, []}], - ?assertEqual(A, mem3:merge_nodes(A,B)), - ?assertEqual(mem3:merge_nodes(A,B), mem3:merge_nodes(B,A)), - ok. - - -next_up_nodes_test() -> - Nodes = [a,b,c,d], - UpNodes = [a,b,d], - ?assertEqual(b, mem3:next_up_node(a,Nodes,UpNodes)), - ?assertEqual(d, mem3:next_up_node(b,Nodes,UpNodes)), - ?assertEqual(a, mem3:next_up_node(d,Nodes,UpNodes)), - ?assertThrow({error, no_gossip_targets_available}, - mem3:next_up_node(a,[a,b,c],[])), - ?assertEqual(b, mem3:next_up_node(a,[a,b],[a,b])), - ok. diff --git a/test/mem3_util_test.erl b/test/mem3_util_test.erl new file mode 100644 index 00000000..0f6d24be --- /dev/null +++ b/test/mem3_util_test.erl @@ -0,0 +1,140 @@ +-module(mem3_util_test). + +-include("mem3.hrl"). +-include_lib("eunit/include/eunit.hrl"). + +hash_test() -> + ?assertEqual(1624516141,mem3_util:hash(0)), + ?assertEqual(3816901808,mem3_util:hash("0")), + ?assertEqual(3523407757,mem3_util:hash(<<0>>)), + ?assertEqual(4108050209,mem3_util:hash(<<"0">>)), + ?assertEqual(3094724072,mem3_util:hash(zero)), + ok. + +name_shard_test() -> + Shard1 = #shard{}, + ?assertError(function_clause, mem3_util:name_shard(Shard1)), + + Shard2 = #shard{dbname = <<"testdb">>, range = [0,100]}, + #shard{name=Name2} = mem3_util:name_shard(Shard2), + ?assertEqual(<<"shards/00000000-00000064/testdb">>, Name2), + + ok. + +create_partition_map_test() -> + {DbName1, N1, Q1, Nodes1} = {<<"testdb1">>, 3, 4, [a,b,c,d]}, + Map1 = mem3_util:create_partition_map(DbName1, N1, Q1, Nodes1), + ?assertEqual(12, length(Map1)), + + {DbName2, N2, Q2, Nodes2} = {<<"testdb2">>, 1, 1, [a,b,c,d]}, + [#shard{name=Name2,node=Node2}] = Map2 = + mem3_util:create_partition_map(DbName2, N2, Q2, Nodes2), + ?assertEqual(1, length(Map2)), + ?assertEqual(<<"shards/00000000-ffffffff/testdb2">>, Name2), + ?assertEqual(a, Node2), + ok. + +build_shards_test() -> + DocProps1 = + [{<<"changelog">>, + [[<<"add">>,<<"00000000-1fffffff">>, + <<"dbcore@node.local">>], + [<<"add">>,<<"20000000-3fffffff">>, + <<"dbcore@node.local">>], + [<<"add">>,<<"40000000-5fffffff">>, + <<"dbcore@node.local">>], + [<<"add">>,<<"60000000-7fffffff">>, + <<"dbcore@node.local">>], + [<<"add">>,<<"80000000-9fffffff">>, + <<"dbcore@node.local">>], + [<<"add">>,<<"a0000000-bfffffff">>, + <<"dbcore@node.local">>], + [<<"add">>,<<"c0000000-dfffffff">>, + <<"dbcore@node.local">>], + [<<"add">>,<<"e0000000-ffffffff">>, + <<"dbcore@node.local">>]]}, + {<<"by_node">>, + {[{<<"dbcore@node.local">>, + [<<"00000000-1fffffff">>,<<"20000000-3fffffff">>, + <<"40000000-5fffffff">>,<<"60000000-7fffffff">>, + <<"80000000-9fffffff">>,<<"a0000000-bfffffff">>, + <<"c0000000-dfffffff">>,<<"e0000000-ffffffff">>]}]}}, + {<<"by_range">>, + {[{<<"00000000-1fffffff">>,[<<"dbcore@node.local">>]}, + {<<"20000000-3fffffff">>,[<<"dbcore@node.local">>]}, + {<<"40000000-5fffffff">>,[<<"dbcore@node.local">>]}, + {<<"60000000-7fffffff">>,[<<"dbcore@node.local">>]}, + {<<"80000000-9fffffff">>,[<<"dbcore@node.local">>]}, + {<<"a0000000-bfffffff">>,[<<"dbcore@node.local">>]}, + {<<"c0000000-dfffffff">>,[<<"dbcore@node.local">>]}, + {<<"e0000000-ffffffff">>,[<<"dbcore@node.local">>]}]}}], + Shards1 = mem3_util:build_shards(<<"testdb1">>, DocProps1), + ExpectedShards1 = + [{shard,<<"shards/00000000-1fffffff/testdb1">>, + 'dbcore@node.local',<<"testdb1">>, + [0,536870911], + undefined}, + {shard,<<"shards/20000000-3fffffff/testdb1">>, + 'dbcore@node.local',<<"testdb1">>, + [536870912,1073741823], + undefined}, + {shard,<<"shards/40000000-5fffffff/testdb1">>, + 'dbcore@node.local',<<"testdb1">>, + [1073741824,1610612735], + undefined}, + {shard,<<"shards/60000000-7fffffff/testdb1">>, + 'dbcore@node.local',<<"testdb1">>, + [1610612736,2147483647], + undefined}, + {shard,<<"shards/80000000-9fffffff/testdb1">>, + 'dbcore@node.local',<<"testdb1">>, + [2147483648,2684354559], + undefined}, + {shard,<<"shards/a0000000-bfffffff/testdb1">>, + 'dbcore@node.local',<<"testdb1">>, + [2684354560,3221225471], + undefined}, + {shard,<<"shards/c0000000-dfffffff/testdb1">>, + 'dbcore@node.local',<<"testdb1">>, + [3221225472,3758096383], + undefined}, + {shard,<<"shards/e0000000-ffffffff/testdb1">>, + 'dbcore@node.local',<<"testdb1">>, + [3758096384,4294967295], + undefined}], + ?assertEqual(ExpectedShards1, Shards1), + ok. + + +%% n_val tests + +nval_test() -> + ?assertEqual(2, mem3_util:n_val(2,4)), + ?assertEqual(1, mem3_util:n_val(-1,4)), + ?assertEqual(4, mem3_util:n_val(6,4)), + ok. + +config_01_setup() -> + Ini = filename:join([code:lib_dir(mem3, test), "01-config-default.ini"]), + {ok, Pid} = couch_config:start_link([Ini]), + Pid. + +config_teardown(_Pid) -> + couch_config:stop(). + +n_val_test_() -> + {"n_val tests", + [ + {setup, + fun config_01_setup/0, + fun config_teardown/1, + fun(Pid) -> + {with, Pid, [ + fun n_val_1/1 + ]} + end} + ] + }. + +n_val_1(_Pid) -> + ?assertEqual(3, mem3_util:n_val(undefined, 4)). diff --git a/test/partitions_test.erl b/test/partitions_test.erl deleted file mode 100644 index 834719b7..00000000 --- a/test/partitions_test.erl +++ /dev/null @@ -1,53 +0,0 @@ --module(partitions_test). --author('brad@cloudant.com'). - --include("../include/config.hrl"). --include("../include/common.hrl"). --include_lib("eunit/include/eunit.hrl"). - --define(FOUR_NODES, [a,b,c,d]). --define(Map1, [{d,0}, - {a,292300327466180583640736966543256603931186508596}, - {b,584600654932361167281473933086513207862373017192}, - {c,876900982398541750922210899629769811793559525788}, - {d,1169201309864722334562947866173026415724746034384}]). --define(Map2, [{c,0}, - {d,182687704666362864775460604089535377456991567873}, - {a,365375409332725729550921208179070754913983135746}, - {b,548063113999088594326381812268606132370974703619}, - {c,730750818665451459101842416358141509827966271492}, - {d,913438523331814323877303020447676887284957839365}, - {a,1096126227998177188652763624537212264741949407238}, - {b,1278813932664540053428224228626747642198940975111}]). --define(Map3, [{d,0}, - {c,0}, - {a,365375409332725729550921208179070754913983135745}, - {d,365375409332725729550921208179070754913983135745}, - {b,730750818665451459101842416358141509827966271490}, - {a,730750818665451459101842416358141509827966271490}, - {c,1096126227998177188652763624537212264741949407235}, - {b,1096126227998177188652763624537212264741949407235}]). - -%%==================================================================== -%% Tests -%%==================================================================== - -fullmap_n1_test() -> - Map1 = partitions:fullmap(<<"test">>, ?FOUR_NODES, opts(1,5)), - ?assertEqual(?Map1, Map1), - Map2 = partitions:fullmap(<<"boorad">>, ?FOUR_NODES, opts(1,8)), - ?assertEqual(?Map2, Map2), - ok. - -fullmap_Ngt1_test() -> - Map3 = partitions:fullmap(<<"boorad">>, ?FOUR_NODES, opts(2,4)), - ?assertEqual(?Map3, Map3), - ok. - - -%%==================================================================== -%% Internal functions -%%==================================================================== - -opts(N,Q) -> - [{n,integer_to_list(N)},{q,integer_to_list(Q)}]. diff --git a/test/test_suite.erl b/test/test_suite.erl deleted file mode 100644 index 22416c5d..00000000 --- a/test/test_suite.erl +++ /dev/null @@ -1,9 +0,0 @@ --module(test_suite). - --include_lib("eunit/include/eunit.hrl"). - -all_test_() -> - [ - {module, mem3_test}, - {module, partitions_test} - ]. -- cgit v1.2.3