Open PushkarPahare opened 7 years ago
I believe this can be fixed by replacing:
this.lastRefillTime = -periodDurationInNanos;
this.nextRefillTime = -periodDurationInNanos;
with:
this.lastRefillTime = ticker.read();
this.nextRefillTime = lastRefillTime + periodDurationInNanos;
There is problem with initializing lastRefillTime to negative periodDurationInNanos. Say periodDurationInNanos is 1 (period 1, time unit nanoseconds), in that case it will initialize lastRefillTime to -1.
Now lets look at the period calculation:
[1] will return a big number (say 1188589593996144), and [2] will simple add 1 to the big number making it 1188589593996145. (See the problem yet). If you divide this by periodDurationInNanos (which was 1 btw). You get huge amount of tokens prebuilt.
Why does not test catch this? MockTicket always starts at 0. But System ticker won't.