FixedIntervalTokenBucketSupplier implements Supplier<TokenBucket> { @Override public TokenBucket get() { return isSame() ? activeConfiguration.getTokenBucket() : reload().getTokenBucket(); } FixedIntervalTokenBucketSupplier(String name,
                                            FixedIntervalTokenBucketConfiguration configuration,
                                            Consumer<TokenBucket> onChangeListener,
                                            Optional<TitusRuntime> titusRuntime); @Override TokenBucket get();  }
@Test public void testTokenBucketPersistsIfNotReconfigured() { settableConfig.setProperty("junit.initialNumberOfTokens", "1"); settableConfig.setProperty("junit.capacity", "1"); settableConfig.setProperty("junit.numberOfTokensPerInterval", "0"); TokenBucket tokenBucket = supplier.get(); assertThat(tokenBucket.tryTake()).isTrue(); assertThat(tokenBucket.tryTake()).isFalse(); tokenBucket.refill(1); assertThat(tokenBucket.tryTake()).isTrue(); assertThat(supplier.get() == tokenBucket).isTrue(); assertThat(buckets).hasSize(2); assertThat(buckets.get(1)).isEqualTo(tokenBucket); }
@Test public void testTokenBucketIsRecreatedIfConfigurationChanges() { settableConfig.setProperty("junit.initialNumberOfTokens", "1"); settableConfig.setProperty("junit.capacity", "1"); settableConfig.setProperty("junit.numberOfTokensPerInterval", "0"); TokenBucket first = supplier.get(); assertThat(first.tryTake()).isTrue(); assertThat(first.tryTake()).isFalse(); settableConfig.setProperty("junit.initialNumberOfTokens", "2"); settableConfig.setProperty("junit.capacity", "2"); TokenBucket second = supplier.get(); assertThat(first != second).isTrue(); assertThat(second.tryTake()).isTrue(); assertThat(second.tryTake()).isTrue(); assertThat(second.tryTake()).isFalse(); assertThat(buckets).hasSize(3); assertThat(buckets.get(1)).isEqualTo(first); assertThat(buckets.get(2)).isEqualTo(second); }
@Test public void testToString() { String text = supplier.get().toString(); assertThat(text).isEqualTo("DefaultTokenBucket{name='junit', capacity=1, refillStrategy=FixedIntervalRefillStrategy{refillRate=1.00 refill/s}, numberOfTokens=0}"); }