Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 169e383

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

File tree

1 file changed

+28
-0
lines changed
Filter options

1 file changed

+28
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/RateLimiter/Tests/Policy/SlidingWindowLimiterTest.php
+28Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.