Commits

Anonymous committed b49aa79 Merge

merge with bitbucket tip

  • Participants
  • Parent commits ace78e0, fcd3d87

Comments (0)

Files changed (3)

scripts/run_tests.escript

                    (M) when M =:= json_pp ->
                         false;
                    (M) ->
+                        proplists:get_value(test, M:module_info(exports)) == 0 orelse
                         nomatch == re:run(atom_to_list(M), NonTestRe)
                 end,
                 proplists:get_value(modules, App)),
-    
+
     crypto:start(),
     start_cover(Modules),
     eunit:test(Modules, [verbose]),
                   [], Modules),
     IndexFilename = filename:join([CoverBase, "index.html"]),
     {ok, Index} = file:open(IndexFilename, [write]),
-    {LineTotal, CoverTotal} = 
+    {LineTotal, CoverTotal} =
         lists:foldl(fun({_,_,Lines,Covered}, {LineAcc, CovAcc}) ->
                             {LineAcc+Lines, CovAcc+Covered}
                     end, {0,0}, Coverages),
     io:format("Cover analysis in ~s~n", [IndexFilename]).
 
 analyze_module(CoverBase, Module) ->
-    {ok, Filename} = 
+    {ok, Filename} =
         cover:analyze_to_file(
           Module,
           filename:join(CoverBase, atom_to_list(Module)++".COVER.html"),
 percentage(_, 0) -> 1000.0;
 percentage(Part, Total) ->
     (Part/Total)*100.
-

src/riak_claim.erl

                         end;
                    ({I0, I1}, {C0, C1}) ->
                         %% wrap-around end-start of the ring
-                        Span = I0+trunc(math:pow(2, 160))-1-I1,
+                        Span = I1+trunc(math:pow(2, 160))-1-I0,
                         if Span > C1-C0 ->
                                 {I0, I1};
                            true ->
     ?assertEqual(yes, erlang:element(1,default_wants_claim(Ring))),
     riak_ring_manager:stop(),
     riak_eventer:stop().
+
+find_biggest_hole_test() ->
+    Max = trunc(math:pow(2, 160)),
+    Part16 = Max/16,
+
+    %% single partition claimed
+    ?assertEqual({Part16*5, Part16*5},
+                 find_biggest_hole([Part16*5])),
+    
+    %% simple hole is in the middle
+    ?assertEqual({Part16*3, Part16*13},
+                 find_biggest_hole([Part16*3, Part16*13])),
+    %% complex hole in the middle
+    ?assertEqual({Part16*5, Part16*10},
+                 find_biggest_hole([Part16*3, Part16*5,
+                                    Part16*10, Part16*15])),
+    
+    %% simple hole is around the end
+    ?assertEqual({Part16*10, Part16*8},
+                 find_biggest_hole([Part16*8, Part16*10])),
+    %% complex hole is around the end
+    ?assertEqual({Part16*13, Part16*3},
+                 find_biggest_hole([Part16*3, Part16*7,
+                                    Part16*10, Part16*13])).

src/riak_event_logger.erl

 %% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 %% KIND, either express or implied.  See the License for the
 %% specific language governing permissions and limitations
-%% under the License.    
+%% under the License.
 
-%% @doc 
-%% riak_event_logger is an example of how to connect to a 
+%% @doc
+%% riak_event_logger is an example of how to connect to a
 %% running Riak cluster to receive events.
 
 -module(riak_event_logger).
 -behavior(gen_server).
 
+-include_lib("eunit/include/eunit.hrl").
+
 -define (RECONNECT_INTERVAL, 200).
 -define (SERVER, ?MODULE).
--record (state, {node, pid, fd}).
--export ([start/2, start_link/2]).
+-record (state, {node, pid, fd, is_test=false}).
+-export ([start/2, start_link/2, test_start/2]).
 -export ([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
 
 %% @private
 start(Node, Filename) ->
-    gen_server:start({local, ?SERVER}, ?MODULE, [Node, Filename], []).
-
-start_link(Node, Filename) -> 
-    gen_server:start_link({local, ?SERVER}, ?MODULE, [Node, Filename], []).
+    gen_server:start({local, ?SERVER}, ?MODULE, [Node, Filename, false], []).
 
 %% @private
-init([Node, Filename]) -> 
+%% For testing only
+test_start(Node, FileName) ->
+    gen_server:start({local, ?SERVER}, ?MODULE, [Node, FileName, true], []).
+
+start_link(Node, Filename) ->
+    gen_server:start_link({local, ?SERVER}, ?MODULE, [Node, Filename, false], []).
+
+%% @private
+init([Node, Filename, IsTest]) ->
     % If this gen_server dies and is supervised, then it will
     % be restarted under a new pid. If this happens, then we will
     % lose our connection to the Riak cluster. So, send a keepalive
     % every few seconds to reconnect.
     timer:apply_interval(?RECONNECT_INTERVAL, gen_server, call, [?SERVER, connect]),
-    
+
     % Open the file, get the file descriptor....
     {ok, FD} = case Filename of
-        _ when Filename == "stdout" orelse Filename == "" -> 
+        _ when Filename == "stdout" orelse Filename == "" ->
             {ok, stdout};
         _ ->
             {ok, CWD} = file:get_cwd(),
             LogFN = filename:join([CWD,Filename]),
             ok = filelib:ensure_dir(LogFN),
-            io:format("Writing event log to ~p~n",[LogFN]),
             file:open(LogFN, [raw, append, delayed_write])
     end,
-    
-    State = #state {
-        node = Node,
-        fd = FD
-    },
+
+    State = #state{node = Node,
+                   fd = FD,
+                   is_test=IsTest},
     {ok, State}.
-    
+
 %% @private
 %% Check if we need to reconnect to the Riak cluster.
-handle_call(connect, _From, State) ->
-    PidHasChanged = State#state.pid /= self(),
-    case PidHasChanged of
-        true ->  register_for_events(State);
-        false -> ignore
-    end,
+handle_call(connect, _From, State) when State#state.pid /= self() ->
+    register_for_events(State),
     {reply, ok, State#state { pid=self() }};
-    
-handle_call(_, _, State) -> {ok, State}.
+
+handle_call(_, _, State) -> {reply, ok, State}.
 
 handle_cast(_, State) -> {noreply, State}.
 
             file:write(FD, io_lib:format(": ~p~n",[Event]))
     end,
     {noreply, State};
-    
+
 handle_info(_, State) -> {noreply, State}.
 
 %% @private
 %% @private
 code_change(_OldVsn, State, _Extra) -> {ok, State}.
 
-register_for_events(State) ->
+register_for_events(State) when State#state.is_test =:= false ->
     % Get the client...
     {ok, C} = riak:client_connect(State#state.node),
 
     % Attach the eventer...
     Desc = io_lib:format("~s", [?SERVER]),
-    C:add_event_handler(self(), Desc, {'_', '_', '_', '_'}, []).
+    C:add_event_handler(self(), Desc, {'_', '_', '_', '_'}, []);
+register_for_events(_State) ->
+  ok.
 
 
 %%% DATE FUNCTIONS %%%
     io_lib:format("[~2..0w/~s/~4..0w:~2..0w:~2..0w:~2..0w ~s]",
                   [Date,month(Month),Year, Hour, Min, Sec, zone()]).
 
+%% @private
+file_write_test() ->
+    {T1, T2, T3} = erlang:now(),
+    random:seed(T1, T2, T3),
+    FileName = "/tmp/event_" ++ integer_to_list(random:uniform(1000)) ++ ".log",
+    {ok, Pid} = riak_event_logger:test_start(foo, FileName),
+    Pid ! {event, test_event},
+    %% Wait for delayed_write to flush
+    timer:sleep(2100),
+    exit(Pid, shutdown),
+    {ok, Data} = file:read_file(FileName),
+    %file:delete(FileName),
+    ?assert(size(Data) > 0).