// enough free tokens
$this->
assertEquals(0,
$limiter->
reserve(10, 300
)->
getWaitDuration());
// waiting time within set maximum
$this->
assertEquals(300,
$limiter->
reserve(5, 300
)->
getWaitDuration());
// waiting time exceeded maximum time (as 5 tokens are already reserved)
$limiter->
reserve(5, 300
);
} public function testConsume() { $rate = Rate::
perSecond(10
);
$limiter =
$this->
createLimiter(10,
$rate);
// enough free tokens
$rateLimit =
$limiter->
consume(5
);
$this->
assertTrue($rateLimit->
isAccepted());
$this->
assertEquals(5,
$rateLimit->
getRemainingTokens());
$this->
assertEqualsWithDelta(time(),
$rateLimit->
getRetryAfter()->
getTimestamp(), 1
);
$this->
assertSame(10,
$rateLimit->
getLimit());
// there are only 5 available free tokens left now
$rateLimit =
$limiter->
consume(10
);
$this->
assertEquals(5,
$rateLimit->
getRemainingTokens());