Skip to content

Commit

Permalink
Test case where bucket starts charged
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonVides committed Dec 19, 2023
1 parent 70910df commit 86af4c8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/opuntia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ new(0) ->
new({MaximumTokens, Rate, TimeUnit})
when ?NON_NEG_INT(MaximumTokens), ?NON_NEG_INT(Rate), ?TU(TimeUnit) ->
#token_bucket_shaper{shape = {MaximumTokens, Rate, TimeUnit},
available_tokens = 0,
available_tokens = MaximumTokens,
last_update = erlang:monotonic_time(TimeUnit)}.

%% @doc Update shaper and return possible waiting time.
Expand Down
11 changes: 7 additions & 4 deletions test/opuntia_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,13 @@ convert_time_unit(Time, millisecond, second) -> Time / 1000;
convert_time_unit(Time, second, microsecond) -> Time * 1000 * 1000;
convert_time_unit(Time, second, millisecond) -> Time * 1000.

%% If consuming at maximum speed, timewindow is never filled so we don't need to care for it
should_take_at_least(Consumed, {_TimeWindow, Rate, TimeUnit}, Unit) ->
Expected = Consumed / Rate,
floor(convert_time_unit(Expected, TimeUnit, Unit)).
should_take_at_least(Consumed, {MaximumTokens, Rate, TimeUnit}, Unit) ->
case Consumed - MaximumTokens of
ToThrottle when ToThrottle =< 0 -> 0;
ToThrottle ->
Expected = ToThrottle / Rate,
floor(convert_time_unit(Expected, TimeUnit, Unit))
end.

run_shaper(Shaper, 0) ->
Shaper;
Expand Down

0 comments on commit 86af4c8

Please sign in to comment.