From b32640350ee550c105258d47629b0b8c3e8775d2 Mon Sep 17 00:00:00 2001 From: Brad Anderson Date: Wed, 24 Mar 2010 20:49:47 -0400 Subject: store Args in mem State, instead of just 'test', and allow for an Args-supplied Config (usually for testing only) --- include/common.hrl | 2 +- src/mem3.erl | 33 ++++++++++++++++++++++----------- test/mem3_test.erl | 8 ++++++-- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/include/common.hrl b/include/common.hrl index 6d92d9fa..b5d4d7ec 100644 --- a/include/common.hrl +++ b/include/common.hrl @@ -46,5 +46,5 @@ nodes, clock, ets, - test=false + args }). diff --git a/src/mem3.erl b/src/mem3.erl index b59a1d60..1bf27bc4 100644 --- a/src/mem3.erl +++ b/src/mem3.erl @@ -70,16 +70,17 @@ state() -> %% start up membership server init(Args) -> process_flag(trap_exit,true), - Config = configuration:get_config(), - OldState = read_latest_state_file(Args, Config), + Config = get_config(Args), + Test = proplists:get_value(test, Args), + OldState = read_latest_state_file(Test, Config), State = handle_init(OldState), - {ok, State#mem{test=(Args == test)}}. - + {ok, State#mem{args=Args}}. %% new node joining to this node -handle_call({join, JoinType, ExtNodes}, _From, State) -> - Config = configuration:get_config(), +handle_call({join, JoinType, ExtNodes}, _From, + State = #mem{args=Args}) -> + Config = get_config(Args), NewState = handle_join(JoinType, ExtNodes, State, Config), {reply, ok, NewState}; @@ -140,6 +141,15 @@ code_change(OldVsn, State, _Extra) -> %%% Internal functions %%-------------------------------------------------------------------- +%% @doc if Args has config use it, otherwise call configuration module +%% most times Args will have config during testing runs +get_config(Args) -> + case proplists:get_value(config, Args) of + undefined -> configuration:get_config(); + Any -> Any + end. + + % we could be automatically: % 1. rejoining a cluster after some downtime % @@ -204,7 +214,8 @@ find_latest_state_filename(Config) -> end. -read_latest_state_file(test, _) -> +%% (Test, Config) +read_latest_state_file(true, _) -> nil; read_latest_state_file(_, Config) -> try @@ -221,7 +232,7 @@ read_latest_state_file(_, Config) -> %% @doc given Config and a list of Nodes, construct a Fullmap -create_map(#config{q=Q}, Nodes) -> +create_map(#config{q=Q} = Config, Nodes) -> [{_,FirstNode,_}|_] = Nodes, Fun = fun({_Pos, Node, Options}, Map) -> Hints = proplists:get_value(hints, Options), @@ -230,16 +241,16 @@ create_map(#config{q=Q}, Nodes) -> end, Acc0 = partitions:create_partitions(Q, FirstNode), Pmap = lists:foldl(Fun, Acc0, lists:keysort(1, Nodes)), - make_fullmap(Pmap). + make_fullmap(Pmap, Config). %% @doc construct a table with all partitions, with the primary node and all %% replication partner nodes as well. -make_fullmap(PMap) -> +make_fullmap(PMap, Config) -> {Nodes, _Parts} = lists:unzip(PMap), NodeParts = lists:flatmap( fun({Node,Part}) -> - Partners = replication:partners(Node, lists:usort(Nodes)), + Partners = replication:partners(Node, lists:usort(Nodes), Config), PartnerList = [{Partner, Part, partner} || Partner <- Partners], [{Node, Part, primary} | PartnerList] end, PMap), diff --git a/test/mem3_test.erl b/test/mem3_test.erl index ae6dcaa5..25815d54 100644 --- a/test/mem3_test.erl +++ b/test/mem3_test.erl @@ -1,6 +1,7 @@ -module(mem3_test). -include("../include/common.hrl"). +-include("../include/config.hrl"). -include_lib("eunit/include/eunit.hrl"). %% TEST SETUP @@ -23,7 +24,9 @@ all_tests_test_() -> test_setup() -> - {ok, Pid} = mem3:start_link(test), + Config = #config{n=3,r=2,w=2,q=3,directory="/srv/db", + storage_mod="dynomite_couch_storage"}, + {ok, Pid} = mem3:start_link([{test,true}, {config, Config}]), Pid. @@ -34,7 +37,8 @@ test_teardown(Pid) -> %% TESTS init(_Pid) -> - #mem{test=Test} = mem3:state(), + #mem{args=Args} = mem3:state(), + Test = proplists:get_value(test, Args), ?assertEqual(true, Test). -- cgit v1.2.3