Skip to content

Commit

Permalink
wait for duration limiters too
Browse files Browse the repository at this point in the history
  • Loading branch information
themsaid committed Aug 25, 2017
1 parent 27e8438 commit aba76bf
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Redis/Limiters/DurationLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function block($timeout, $callback = null)
$starting = time();

while (! $this->acquire()) {
if ($this->decay > 1 || time() - $timeout >= $starting) {
if (time() - $timeout >= $starting) {
throw new LimiterTimeoutException;
}

Expand Down
28 changes: 3 additions & 25 deletions tests/Redis/DurationLimiterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ public function it_locks_tasks_when_no_slot_available()
{
$store = [];

(new DurationLimiter($this->redis(), 'key', 2, 2))->block(2, function () use (&$store) {
(new DurationLimiter($this->redis(), 'key', 2, 2))->block(0, function () use (&$store) {
$store[] = 1;
});

(new DurationLimiter($this->redis(), 'key', 2, 2))->block(2, function () use (&$store) {
(new DurationLimiter($this->redis(), 'key', 2, 2))->block(0, function () use (&$store) {
$store[] = 2;
});

try {
(new DurationLimiter($this->redis(), 'key', 2, 2))->block(2, function () use (&$store) {
(new DurationLimiter($this->redis(), 'key', 2, 2))->block(0, function () use (&$store) {
$store[] = 3;
});
} catch (\Throwable $e) {
Expand Down Expand Up @@ -79,28 +79,6 @@ public function it_fails_immediately_or_retries_for_a_while_based_on_a_given_tim
$this->assertEquals([1, 3], $store);
}

/**
* @test
*/
public function it_doesnt_retry_if_duration_more_than_1_second()
{
$store = [];

(new DurationLimiter($this->redis(), 'key', 1, 60))->block(2, function () use (&$store) {
$store[] = 1;
});

try {
$this->assertEquals([1], $store);

(new DurationLimiter($this->redis(), 'key', 1, 60))->block(120, function () use (&$store) {
$store[] = 3;
});
} catch (\Throwable $e) {
$this->assertInstanceOf(LimiterTimeoutException::class, $e);
}
}

/**
* @return Client
*/
Expand Down

0 comments on commit aba76bf

Please sign in to comment.