DefaultTokenBucket implements TokenBucket { @Override public boolean tryTake() { return tryTake(1); } DefaultTokenBucket(String name, long capacity, RefillStrategy refillStrategy, long initialNumberOfTokens); @Override String getName(); @Override long getCapacity(); @Override long getNumberOfTokens(); @Override boolean tryTake(); @Override boolean tryTake(long numberOfTokens); @Override void take(); @Override void take(long numberOfTokens); @Override void refill(long numberOfTokens); @Override RefillStrategy getRefillStrategy(); @Override String toString();  }
@Test public void tryTakeShouldGetToken() { TestRefillStrategy testRefillStrategy = createTestRefillStrategy(); TokenBucket tokenBucket = createTokenBucket(10, testRefillStrategy, 0); testRefillStrategy.setAmountToRefill(1); boolean gotToken = tokenBucket.tryTake(); assertTrue(gotToken); testRefillStrategy.setAmountToRefill(10); gotToken = tokenBucket.tryTake(10); assertTrue(gotToken); }
@Test public void tryTakeShouldNotGetToken() { TestRefillStrategy testRefillStrategy = createTestRefillStrategy(); TokenBucket tokenBucket = createTokenBucket(10, testRefillStrategy, 0); boolean gotToken = tokenBucket.tryTake(); assertFalse(gotToken); testRefillStrategy.setAmountToRefill(5); gotToken = tokenBucket.tryTake(6); assertFalse(gotToken); }