Skip to content

Commit 169e383

Browse files
committed
Reintroduce peek consume test for sliding window policy
1 parent b83b5f7 commit 169e383

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/Symfony/Component/RateLimiter/Tests/Policy/SlidingWindowLimiterTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,34 @@ public function testReserve()
7878
$this->assertEqualsWithDelta(12 / 5, $limiter->reserve(4)->getWaitDuration(), 1);
7979
}
8080

81+
public function testPeekConsume()
82+
{
83+
$limiter = $this->createLimiter();
84+
85+
$limiter->consume(9);
86+
87+
// peek by consuming 0 tokens twice (making sure peeking doesn't claim a token)
88+
for ($i = 0; $i < 2; ++$i) {
89+
$rateLimit = $limiter->consume(0);
90+
$this->assertTrue($rateLimit->isAccepted());
91+
$this->assertSame(10, $rateLimit->getLimit());
92+
$this->assertEquals(
93+
\DateTimeImmutable::createFromFormat('U.u', sprintf('%.6F', microtime(true))),
94+
$rateLimit->getRetryAfter()
95+
);
96+
}
97+
98+
$limiter->consume();
99+
100+
$rateLimit = $limiter->consume(0);
101+
$this->assertEquals(0, $rateLimit->getRemainingTokens());
102+
$this->assertTrue($rateLimit->isAccepted());
103+
$this->assertEquals(
104+
\DateTimeImmutable::createFromFormat('U.u', sprintf('%.6F', microtime(true) + 12)),
105+
$rateLimit->getRetryAfter()
106+
);
107+
}
108+
81109
private function createLimiter(): SlidingWindowLimiter
82110
{
83111
return new SlidingWindowLimiter('test', 10, new \DateInterval('PT12S'), $this->storage);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy