Skip to content

Commit

Permalink
[8.x] Make sure availableIn returns positive values (#37809)
Browse files Browse the repository at this point in the history
* Make sure availableIn returns positive values

* Fix code style
  • Loading branch information
rickycheers authored Jun 24, 2021
1 parent e8ecb95 commit 7a8b3a1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Cache/RateLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,6 @@ public function clear($key)
*/
public function availableIn($key)
{
return $this->cache->get($key.':timer') - $this->currentTime();
return max(0, $this->cache->get($key.':timer') - $this->currentTime());
}
}
10 changes: 10 additions & 0 deletions tests/Cache/CacheRateLimiterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,14 @@ public function testClearClearsTheCacheKeys()

$rateLimiter->clear('key');
}

public function testAvailableInReturnsPositiveValues()
{
$cache = m::mock(Cache::class);
$cache->shouldReceive('get')->andReturn(now()->subSeconds(60)->getTimestamp(), null);
$rateLimiter = new RateLimiter($cache);

$this->assertTrue($rateLimiter->availableIn('key:timer') >= 0);
$this->assertTrue($rateLimiter->availableIn('key:timer') >= 0);
}
}
39 changes: 39 additions & 0 deletions tests/Integration/Queue/RateLimitedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Bus\Queueable;
use Illuminate\Cache\RateLimiter;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Contracts\Cache\Repository as Cache;
use Illuminate\Contracts\Queue\Job;
use Illuminate\Queue\CallQueuedHandler;
use Illuminate\Queue\InteractsWithQueue;
Expand Down Expand Up @@ -37,6 +38,44 @@ public function testUnlimitedJobsAreExecuted()
$this->assertJobRanSuccessfully(RateLimitedTestJob::class);
}

public function testRateLimitedJobsAreNotExecutedOnLimitReached2()
{
$cache = m::mock(Cache::class);
$cache->shouldReceive('get')->andReturn(0, 1, null);
$cache->shouldReceive('add')->andReturn(true, true);
$cache->shouldReceive('increment')->andReturn(1);
$cache->shouldReceive('has')->andReturn(true);

$rateLimiter = new RateLimiter($cache);
$this->app->instance(RateLimiter::class, $rateLimiter);
$rateLimiter = $this->app->make(RateLimiter::class);

$rateLimiter->for('test', function ($job) {
return Limit::perHour(1);
});

$this->assertJobRanSuccessfully(RateLimitedTestJob::class);

// Assert Job was released and released with a delay greater than 0
RateLimitedTestJob::$handled = false;
$instance = new CallQueuedHandler(new Dispatcher($this->app), $this->app);

$job = m::mock(Job::class);

$job->shouldReceive('hasFailed')->once()->andReturn(false);
$job->shouldReceive('release')->once()->withArgs(function ($delay) {
return $delay >= 0;
});
$job->shouldReceive('isReleased')->andReturn(true);
$job->shouldReceive('isDeletedOrReleased')->once()->andReturn(true);

$instance->call($job, [
'command' => serialize($command = new RateLimitedTestJob),
]);

$this->assertFalse(RateLimitedTestJob::$handled);
}

public function testRateLimitedJobsAreNotExecutedOnLimitReached()
{
$rateLimiter = $this->app->make(RateLimiter::class);
Expand Down

0 comments on commit 7a8b3a1

Please sign in to comment.