8000 Reintroduce peek consume test for sliding window policy · symfony/symfony@169e383 · GitHub
[go: up one dir, main page]

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

+28
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)
0