Skip to content
This repository
Browse code

Add eunit test.

  • Loading branch information...
commit 9a7ae614d05b64f10252b9c9c03230feee7ba77b 1 parent 8b60d45
Kelly McLaughlin kellymclaughlin authored

Showing 1 changed file with 67 additions and 1 deletion. Show diff stats Hide diff stats

  1. +67 1 src/riak_core_vnode.erl
68 src/riak_core_vnode.erl
@@ -40,6 +40,11 @@
40 40 core_status/1,
41 41 handoff_error/3]).
42 42
  43 +-ifdef(TEST).
  44 +-export([test_link/2,
  45 + current_state/1]).
  46 +-endif.
  47 +
43 48 -define(normal_reason(R),
44 49 (R == normal orelse R == shutdown orelse
45 50 (is_tuple(R) andalso element(1,R) == shutdown))).
@@ -450,6 +455,8 @@ handle_event(R=?COVERAGE_REQ{}, _StateName, State) ->
450 455 active(R, State).
451 456
452 457
  458 +handle_sync_event(current_state, _From, StateName, State) ->
  459 + {reply, {StateName, State}, StateName, State};
453 460 handle_sync_event(get_mod_index, _From, StateName,
454 461 State=#state{index=Idx,mod=Mod}) ->
455 462 {reply, {Mod, Idx}, StateName, State, State#state.inactivity_timeout};
@@ -517,7 +524,7 @@ handle_info({'EXIT', Pid, Reason},
517 524 _ ->
518 525 lager:error("~p ~p worker pool crashed ~p\n", [Index, Mod, Reason]),
519 526 {pool, WorkerModule, PoolSize, WorkerArgs}=PoolConfig,
520   - lager:debug("starting worker pool ~p with size"
  527 + lager:debug("starting worker pool ~p with size "
521 528 "of ~p for vnode ~p.",
522 529 [WorkerModule, PoolSize, Index]),
523 530 {ok, NewPoolPid} =
@@ -677,3 +684,62 @@ stop_manager_event_timer(#state{manager_event_timer=undefined}) ->
677 684 ok;
678 685 stop_manager_event_timer(#state{manager_event_timer=T}) ->
679 686 gen_fsm:cancel_timer(T).
  687 +
  688 +%% ===================================================================
  689 +%% Test API
  690 +%% ===================================================================
  691 +
  692 +-ifdef(TEST).
  693 +
  694 +%% @doc Start the garbage collection server
  695 +test_link(Mod, Index) ->
  696 + gen_fsm:start_link(?MODULE, [Mod, Index, 0, node()], []).
  697 +
  698 +%% @doc Get the current state of the fsm for testing inspection
  699 +-spec current_state(pid()) -> {atom(), #state{}} | {error, term()}.
  700 +current_state(Pid) ->
  701 + gen_fsm:sync_send_all_state_event(Pid, current_state).
  702 +
  703 +pool_death_test() ->
  704 + meck:new(test_vnode),
  705 + meck:expect(test_vnode, init, fun(_) -> {ok, [], [{pool, test_pool_mod, 1, []}]} end),
  706 + meck:expect(test_vnode, terminate, fun(_, _) -> normal end),
  707 + meck:new(test_pool_mod),
  708 + meck:expect(test_pool_mod, init_worker, fun(_, _, _) -> {ok, []} end),
  709 +
  710 + {ok, Pid} = ?MODULE:test_link(test_vnode, 0),
  711 + {_, StateData1} = ?MODULE:current_state(Pid),
  712 + PoolPid1 = StateData1#state.pool_pid,
  713 + exit(PoolPid1, kill),
  714 + wait_for_process_death(PoolPid1),
  715 + ?assertNot(is_process_alive(PoolPid1)),
  716 + wait_for_state_update(StateData1, Pid),
  717 + {_, StateData2} = ?MODULE:current_state(Pid),
  718 + PoolPid2 = StateData2#state.pool_pid,
  719 + ?assertNot(PoolPid2 =:= undefined),
  720 + exit(Pid, normal),
  721 + wait_for_process_death(Pid),
  722 + meck:validate(test_pool_mod),
  723 + meck:validate(test_vnode),
  724 + meck:unload(test_pool_mod),
  725 + meck:unload(test_vnode).
  726 +
  727 +wait_for_process_death(Pid) ->
  728 + wait_for_process_death(Pid, is_process_alive(Pid)).
  729 +
  730 +wait_for_process_death(Pid, true) ->
  731 + wait_for_process_death(Pid, is_process_alive(Pid));
  732 +wait_for_process_death(_Pid, false) ->
  733 + ok.
  734 +
  735 +wait_for_state_update(OriginalStateData, Pid) ->
  736 + {_, CurrentStateData} = ?MODULE:current_state(Pid),
  737 + wait_for_state_update(OriginalStateData, CurrentStateData, Pid).
  738 +
  739 +wait_for_state_update(OriginalStateData, OriginalStateData, Pid) ->
  740 + {_, CurrentStateData} = ?MODULE:current_state(Pid),
  741 + wait_for_state_update(OriginalStateData, CurrentStateData, Pid);
  742 +wait_for_state_update(_OriginalState, _StateData, _Pid) ->
  743 + ok.
  744 +
  745 +-endif.

0 comments on commit 9a7ae61

Please sign in to comment.
Something went wrong with that request. Please try again.