Anonymous avatar Anonymous committed 2f91713

mockable servers, and a test in riak_bucket

Comments (0)

Files changed (7)

scripts/run_tests.escript

                         nomatch == re:run(atom_to_list(M), NonTestRe)
                 end,
                 proplists:get_value(modules, App)),
-
     crypto:start(),
-    setup_mock_ring(),
-
     start_cover(Modules),
     eunit:test(Modules, [verbose]),
     analyze_cover(Modules);
 percentage(Part, Total) ->
     (Part/Total)*100.
 
-setup_mock_ring() ->
-    Ring0 = lists:foldl(fun(_,R) ->
-                               riak_ring:transfer_node(
-                                 hd(riak_ring:my_indices(R)),
-                                 othernode@otherhost, R) end,
-                       riak_ring:fresh(16,node()),[1,2,3,4,5,6]),
-    Ring = lists:foldl(fun(_,R) ->
-                               riak_ring:transfer_node(
-                                 hd(riak_ring:my_indices(R)),
-                                 othernode2@otherhost2, R) end,
-                       Ring0,[1,2,3,4,5,6]),
-    ets:new(nodelocal_ring, [protected, named_table]),
-    ets:insert(nodelocal_ring, {ring, Ring}).

src/riak_bucket.erl

 %% @type riak_bucketprops() = [{Propkey :: atom(), Propval :: term()}]
 
 -module(riak_bucket).
+-include_lib("eunit/include/eunit.hrl").
 -export([set_bucket/2, get_bucket/1, get_bucket/2]).
 -export([defaults/0]).
 
      {young_vclock, 21600},
      {big_vclock, 50},
      {small_vclock, 10}].
+
+simple_set_test() ->
+    riak_ring_manager:start_link(test),
+    riak_eventer:start_link(test),
+    ok = set_bucket(a_bucket,[{key,value}]),
+    Bucket = get_bucket(a_bucket),
+    riak_ring_manager:stop(),
+    riak_eventer:stop(),
+    ?assertEqual(value, proplists:get_value(key, Bucket)).

src/riak_claim.erl

 never_wants_claim(_) -> no.
 
 wants_claim_test() ->
+    riak_ring_manager:start_link(test),
+    riak_eventer:start_link(test),
+    riak_test_util:setup_mockring1(),
     {ok, Ring} = riak_ring_manager:get_my_ring(),
-    {yes, _}  = default_wants_claim(Ring).
+    ?assertEqual(yes, erlang:element(1,default_wants_claim(Ring))),
+    riak_ring_manager:stop(),
+    riak_eventer:stop().

src/riak_eventer.erl

 
 -module(riak_eventer).
 -behaviour(gen_server2).
--export([start_link/0]).
+-export([start_link/0,start_link/1,stop/0]).
 -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
 	 terminate/2, code_change/3]).
 
 
 %% @private
 start_link() -> gen_server2:start_link({local, ?MODULE}, ?MODULE, [], []).
+start_link(test) -> % when started this way, run a mock server (nop)
+    gen_server2:start_link({local, ?MODULE}, ?MODULE, [test], []).
 
 %% @private
-init([]) -> {ok, stateless_server}.
+init([]) -> {ok, stateless_server};
+init([test]) -> {ok, test}.
 
 notify(Event) ->
     gen_server2:cast(riak_local_logger, {event, Event}),
 notify(Module, EventName, EventDetail) ->
     notify({Module, EventName, node(), EventDetail}).
 
+%% @private (only used for test instances)
+stop() -> gen_server2:cast(?MODULE, stop).
+
 %% @private
+handle_cast(stop, State) -> {stop,normal,State};
+
+handle_cast({event, _Event}, test) -> {noreply,test};
 handle_cast({event, Event}, State) ->
     {ok, Ring} = riak_ring_manager:get_my_ring(),    %%%% TEST EVENTS!
     Eventers = get_eventers(Ring),

src/riak_ring_gossiper.erl

     end.
 
 gossip_to(RemoteNode) ->
-    riak_eventer:notify(riak_ring_gossiper, send, RemoteNode),
-    {ok, MyRing} = riak_ring_manager:get_my_ring(),
-    riak_connect:cast(RemoteNode, {gossip_ring, MyRing}).
+    case lists:member(riak_ring_gossiper, registered()) of
+        false -> nop; % only gossip if we can also receive
+        true ->
+            riak_eventer:notify(riak_ring_gossiper, send, RemoteNode),
+            {ok, MyRing} = riak_ring_manager:get_my_ring(),
+            riak_connect:cast(RemoteNode, {gossip_ring, MyRing})
+    end.
 
 gossip_ring_to(RemoteNode,Ring) ->
     riak_eventer:notify(riak_ring_gossiper, send, RemoteNode),

src/riak_ring_manager.erl

 -include_lib("eunit/include/eunit.hrl").
 
 -behaviour(gen_server2).
--export([start_link/0]).
+-export([start_link/0,start_link/1,stop/0]).
 -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
 	 terminate/2, code_change/3]).
 -export([get_my_ring/0,set_my_ring/1,write_ringfile/0,prune_ringfiles/0,
         read_ringfile/1,find_latest_ringfile/0]).
 
 start_link() -> gen_server2:start_link({local, ?MODULE}, ?MODULE, [], []).
-
-verify_get_test() ->
-    % not a real test, just verifying mock setup
-    {ok, R} = get_my_ring(),
-    ?assertEqual(length(riak_ring:my_indices(R)), 4).
+start_link(test) -> % when started this way, run a mock server (no disk, etc)
+    gen_server2:start_link({local, ?MODULE}, ?MODULE, [test], []).
 
 %% @private
 init([]) ->
     Ring = riak_ring:fresh(),
     ets:new(nodelocal_ring, [protected, named_table]),
     ets:insert(nodelocal_ring, {ring, Ring}),
-    {ok, stateless_server}.
+    {ok, stateless_server};
+init([test]) ->
+    Ring = riak_ring:fresh(16,node()),
+    ets:new(nodelocal_ring, [protected, named_table]),
+    ets:insert(nodelocal_ring, {ring, Ring}),
+    {ok, test}.
 
 %% @spec get_my_ring() -> {ok, riak_ring:riak_ring()} | {error, Reason}
 get_my_ring() ->
         [] -> {error, no_ring}
     end.
 %% @spec set_my_ring(riak_ring:riak_ring()) -> ok
-set_my_ring(Ring) -> gen_server2:cast(?MODULE, {set_my_ring, Ring}).
+set_my_ring(Ring) -> gen_server2:call(?MODULE, {set_my_ring, Ring}).
 %% @spec write_ringfile() -> ok
 write_ringfile() -> gen_server2:cast(?MODULE, write_ringfile).
+%% @private (only used for test instances)
+stop() -> gen_server2:cast(?MODULE, stop).
 
 %% @private
-handle_cast({set_my_ring, Ring}, State) -> 
-    ets:insert(nodelocal_ring, {ring, Ring}),
-    {noreply,State};
+handle_cast(stop, State) -> {stop,normal,State};
 
+handle_cast(write_ringfile, test) -> {noreply,test};
 handle_cast(write_ringfile, State) ->
     {ok, Ring} = get_my_ring(),
     spawn(fun() -> do_write_ringfile(Ring) end),
 handle_info(_Info, State) -> {noreply, State}.
 
 %% @private
-handle_call(_Msg,_From,State) -> {noreply, State}.
+handle_call({set_my_ring, Ring}, _From, State) -> 
+    ets:insert(nodelocal_ring, {ring, Ring}),
+    {reply,ok,State}.
 
 %% @private
 terminate(_Reason, _State) -> ok.

src/riak_test_util.erl

 -module(riak_test_util).
--export([standard_backend_test/1]).
+-export([standard_backend_test/1,setup_mockring1/0]).
 -include_lib("eunit/include/eunit.hrl").
 
 standard_backend_test(BackendMod) ->
     ?assertEqual({error, notfound}, BackendMod:get(S, {b2, <<"k2">>})),
     ?assertEqual([{b1, <<"k1">>}], BackendMod:list(S)),
     ok = BackendMod:stop(S).
+
+setup_mockring1() ->
+    % requires a running riak_ring_manager, in test-mode is ok
+    Ring0 = lists:foldl(fun(_,R) ->
+                               riak_ring:transfer_node(
+                                 hd(riak_ring:my_indices(R)),
+                                 othernode@otherhost, R) end,
+                       riak_ring:fresh(16,node()),[1,2,3,4,5,6]),
+    Ring = lists:foldl(fun(_,R) ->
+                               riak_ring:transfer_node(
+                                 hd(riak_ring:my_indices(R)),
+                                 othernode2@otherhost2, R) end,
+                       Ring0,[1,2,3,4,5,6]),
+    riak_ring_manager:set_my_ring(Ring).
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.