Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add crash/restart test and reschedule token rates on restart

  • Loading branch information...
commit 03c4e58e9a0492594df94e6801368685a413caea 1 parent 3cf55bc
@buddhisthead buddhisthead authored
Showing with 40 additions and 4 deletions.
  1. +18 −2 src/riak_core_token_manager.erl
  2. +22 −2 test/token_manager_tests.erl
View
20 src/riak_core_token_manager.erl
@@ -51,6 +51,9 @@
tokens_blocked/1
]).
+%% testing
+-export([start/1]).
+
%% reporting
-export([clear_history/0,
head/0,
@@ -231,6 +234,10 @@ start_link() ->
start_link(Interval) ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [Interval], []).
+%% Test entry point to start stand-alone server
+start(Interval) ->
+ gen_server:start({local, ?SERVER}, ?MODULE, [Interval], []).
+
%%%% Gen Server %%%%%%%
%% @private
@@ -328,8 +335,10 @@ handle_info({refill_tokens, Type}, State) ->
{noreply, State2};
%% Handle transfer of ETS table from table manager
handle_info({'ETS-TRANSFER', TableId, Pid, _Data}, State) ->
- io:format("table_mgr (~p) -> token_mgr (~p) receiving ownership of TableId: ~p~n", [Pid, self(), TableId]),
- {noreply, State#state{table_id=TableId}};
+ lager:debug("table_mgr (~p) -> token_mgr (~p) receiving ownership of TableId: ~p", [Pid, self(), TableId]),
+ State2 = State#state{table_id=TableId},
+ reschedule_token_refills(State2),
+ {noreply, State2};
handle_info({'DOWN', Ref, _, _, _}, State) ->
lager:info("Linked process died with ref ~p: ", [Ref]),
{noreply, State};
@@ -528,6 +537,13 @@ maybe_unblock_blocked(Type, State) ->
Queue = token_queue(Type, State),
give_available_tokens(Type, PosNumAvailable, Queue, State).
+%% Get existing token type info from ETS table and schedule all for refill.
+%% This is needed because we just reloaded our saved persisent state data
+%% after a crash.
+reschedule_token_refills(State) ->
+ Types = do_token_types(State),
+ [schedule_refill_tokens(Type, State) || Type <- Types].
+
%% Schedule a timer event to refill tokens of given type
schedule_refill_tokens(Type, State) ->
{Period, _Count} = ?rate(token_info(Type, State)),
View
24 test/token_manager_tests.erl
@@ -12,17 +12,23 @@ token_mgr_test_() ->
{timeout, 60000, %% Seconds to finish all of the tests
{setup, fun() ->
riak_core_table_manager:start_link([{?TM_ETS_TABLE, ?TM_ETS_OPTS}]),
- ?TOK_MGR:start_link(1) %% setup with history window to 1 seconds
+ start_token_mgr()
end,
fun(_) -> ok end, %% cleanup
fun(_) ->
[ %% Tests
- { "set/get token rates",
+ { "set/get token rates + verify rates",
fun() ->
setup_token_rates(),
verify_token_rates()
end},
+ { "crash token manager + verify rates persist",
+ fun() ->
+ crash_and_restart_token_manager(),
+ verify_token_rates()
+ end},
+
{ "head - empty",
fun() ->
%% we haven't taken a sample yet, so history is empty
@@ -269,4 +275,18 @@ verify_token_rates() ->
DefaultRate = {0,0},
?assertEqual(DefaultRate, Rate).
+%% start a stand-alone server, not linked, so that when we crash it, it
+%% doesn't take down our test too.
+start_token_mgr() ->
+ %% setup with history window to 1 seconds
+ ?TOK_MGR:start(1).
+
+crash_and_restart_token_manager() ->
+ Pid = erlang:whereis(?TOK_MGR),
+ ?assertNot(Pid == undefined),
+ erlang:exit(Pid, kill),
+ timer:sleep(100),
+ start_token_mgr(),
+ timer:sleep(100).
+
-endif.
Please sign in to comment.
Something went wrong with that request. Please try again.