Is there an infinite capacity and zero capacity bucket available for testing? #347
-
I am learning bucket4j and am trying to figure out a good way to simulate different scenarios in my testing. In these tests I want to simulate both a bucket that always allows consumption, and another bucket that never allows. With this, I can test my code that handles both scenarios. My first approach is to simply mock the bucket4j which does work, but it doesn't feel that elegant. Instead, I created a bucket interface which can be implemented by test buckets. However, I can't really create a zero capacity bucket. I can create a bucket with availability one and then instantly consume the last capacity, but it isn't that great. For the inifinite bucket, I created a bucket that adds one every nanosecond. Is it possible to create an ideal infinite and zero bucket? LIke
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hello @etnoy It is not possible to achive via builder but, can be simulated in the following way: Effectively empty: Bucket bucket = ...;
bucket.consumeIgnoringRateLimits(Long.MAX_VALUE / 2);
System.out.println(bucket.getAvailableTokens()); Effectively infinite: Bucket bucket = ...;
bucket.forceAddTokens(Long.MAX_VALUE / 2);
System.out.println(bucket.getAvailableTokens()); |
Beta Was this translation helpful? Give feedback.
Hello @etnoy
It is not possible to achive via builder but, can be simulated in the following way:
Effectively empty:
Effectively infinite: