Skip to content

Commit 1e78dfb

Browse files
authored
[12.x] Expand Redis DurationLimiter tests (#57947)
* Increase Redis DurationLimiter test coverage in DurationLimiterTest * Replace sleep() with Carbon time travel in DurationLimiterTest * Revert to sleep() in DurationLimiterTest - Carbon time travel not compatible
1 parent 4028c1d commit 1e78dfb

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

tests/Redis/DurationLimiterTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,78 @@ public function testItReturnsTheCallbackResult()
9191
$this->assertSame('foo', $result);
9292
}
9393

94+
public function testAcquireSetsDecaysAtAndRemaining()
95+
{
96+
$limiter = new DurationLimiter($this->redis(), 'acquire-key', 2, 2);
97+
98+
$acquired1 = $limiter->acquire();
99+
$this->assertTrue($acquired1);
100+
$this->assertGreaterThanOrEqual(time(), $limiter->decaysAt);
101+
$this->assertSame(1, $limiter->remaining);
102+
103+
$acquired2 = $limiter->acquire();
104+
$this->assertTrue($acquired2);
105+
$this->assertSame(0, $limiter->remaining);
106+
107+
$acquired3 = $limiter->acquire();
108+
$this->assertFalse($acquired3);
109+
$this->assertSame(0, $limiter->remaining);
110+
}
111+
112+
public function testTooManyAttemptsReportsCorrectly()
113+
{
114+
$limiter = new DurationLimiter($this->redis(), 'too-many-key', 2, 1);
115+
116+
// Initially, should not have too many attempts
117+
$this->assertFalse($limiter->tooManyAttempts());
118+
$this->assertSame(0, $limiter->decaysAt); // As per script for non-existing key
119+
$this->assertGreaterThan(0, $limiter->remaining); // Remaining is positive future timestamp placeholder
120+
121+
// Use up the available slots
122+
$this->assertTrue($limiter->acquire());
123+
$this->assertTrue($limiter->acquire());
124+
125+
// Now, too many attempts within the same window
126+
$this->assertTrue($limiter->tooManyAttempts());
127+
$this->assertSame(0, max(0, $limiter->remaining));
128+
129+
// After decay window, attempts should be allowed again
130+
sleep(1);
131+
$this->assertFalse($limiter->tooManyAttempts());
132+
}
133+
134+
public function testClearResetsLimiter()
135+
{
136+
$limiter = new DurationLimiter($this->redis(), 'clear-key', 1, 2);
137+
138+
$this->assertTrue($limiter->acquire());
139+
$this->assertFalse($limiter->acquire());
140+
141+
// Clear and try again
142+
$limiter->clear();
143+
$this->assertTrue($limiter->acquire());
144+
}
145+
146+
public function testBlockReturnsTrueWithoutCallback()
147+
{
148+
$limiter = new DurationLimiter($this->redis(), 'no-callback-key', 1, 1);
149+
150+
$this->assertTrue($limiter->block(1));
151+
}
152+
153+
public function testAcquireResetsAfterDecay()
154+
{
155+
$limiter = new DurationLimiter($this->redis(), 'reset-after-decay-key', 1, 1);
156+
157+
$this->assertTrue($limiter->acquire());
158+
$this->assertFalse($limiter->acquire());
159+
160+
sleep(1);
161+
162+
$this->assertTrue($limiter->acquire());
163+
$this->assertSame(0, $limiter->remaining);
164+
}
165+
94166
private function redis()
95167
{
96168
return $this->redis['phpredis']->connection();

0 commit comments

Comments
 (0)