Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add head(Token) unit test

  • Loading branch information...
commit 2763efa348916d801b52969f5344f56df5a9cb38 1 parent 3fd7251
Chris T buddhisthead authored
Showing with 28 additions and 5 deletions.
  1. +4 −2 src/riak_core_token_manager.erl
  2. +24 −3 test/token_manager_tests.erl
6 src/riak_core_token_manager.erl
View
@@ -16,6 +16,8 @@
%% specific language governing permissions and limitations
%% under the License.
%%
+%% ./rebar skip_deps=true eunit suite=token_manager
+%%
%% -------------------------------------------------------------------
-module(riak_core_token_manager).
@@ -383,13 +385,13 @@ do_hist(End, TokenType, Offset, Count, #state{history=HistQueue}) ->
StatsDictList = queue:to_list(Hist),
[orddict:to_list(Stat) || Stat <- StatsDictList];
_T ->
- [{TokenType, stat_window(TokenType, StatsDict)} || StatsDict <- queue:to_list(Hist)]
+ [[{TokenType, stat_window(TokenType, StatsDict)}] || StatsDict <- queue:to_list(Hist)]
end
end.
segment_queue(First, Last, Queue) ->
QLen = queue:len(Queue),
- ?debugFmt("First: ~p, Last: ~p, QLen: ~p", [First, Last, QLen]),
+%% ?debugFmt("First: ~p, Last: ~p, QLen: ~p", [First, Last, QLen]),
case QLen >= Last andalso QLen > 0 of
true ->
%% trim off extra tail, then trim head
27 test/token_manager_tests.erl
View
@@ -143,7 +143,7 @@ token_mgr_test_() ->
?assertEqual([], ?TOK_MGR:tokens_given())
end},
- { "synchronous gets and history test",
+ { "synchronous gets and ps/history",
fun() ->
Tokens = expected_token_types(),
%% clear history and let 1 sample be taken with empty stats
@@ -156,7 +156,17 @@ token_mgr_test_() ->
timer:sleep(1000),
%% Sample3: max given for each token, plus 2 blocked
E3 = [max_out_plus(Token, self(), 2) || Token <- Tokens],
- timer:sleep(1000),
+
+ %% let all spawned procs run
+ timer:sleep(100),
+
+ PS = ?TOK_MGR:ps(),
+ Given = ?TOK_MGR:tokens_given(),
+ Blocked = ?TOK_MGR:tokens_blocked(),
+ ?assertNot(PS == []),
+ ?assertEqual(PS, Given++Blocked),
+
+ timer:sleep(1000-100),
%% verify history
%% TODO: refills is hard to calculate and needs a better solution
?assertEqual([E1,E2,E3], ?TOK_MGR:head()),
@@ -175,13 +185,24 @@ token_mgr_test_() ->
?assertEqual([E1,E2,E3], ?TOK_MGR:head(all,0)),
?assertEqual([E1,E2,E3], ?TOK_MGR:head(all,-1)),
?assertEqual([E3], ?TOK_MGR:tail(all,0)),
- ?assertEqual([E3], ?TOK_MGR:tail(all,-1))
+ ?assertEqual([E3], ?TOK_MGR:tail(all,-1)),
+
+ %% calling head on a specific token yields a list of lists,
+ %% but each "of lists" has only one thing in it.
+ [check_head_token(Token, [E1,E2,E3]) || Token <- Tokens]
end}
] end}
}.
+check_head_token(Token, StatsList) ->
+ F1 = fun(Stats) -> filter_stat(Token, Stats) end,
+ ?assertEqual(?TOK_MGR:head(Token), lists:map(F1, StatsList)).
+
+filter_stat(Token, Stats) ->
+ lists:filter(fun({T,_Stat}) -> Token==T end, Stats).
+
spawn_sync_request(Token, Pid, Meta) ->
spawn(fun() -> ok = ?TOK_MGR:get_token_sync(Token, Pid, Meta) end).
Please sign in to comment.
Something went wrong with that request. Please try again.