Skip to content

Commit

Permalink
Expired elements also get removed in the 'load' method.
Browse files Browse the repository at this point in the history
Added use of case test. See #74.
  • Loading branch information
ariasmn committed May 11, 2021
1 parent d1831bb commit bbef016
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
22 changes: 17 additions & 5 deletions src/Ganesha/Storage/Adapter/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public function setConfiguration(Configuration $configuration): void
*/
public function load(string $service): int
{
$this->removeExpiredElements($service);

$r = $this->redis->zCard($service);

if ($r === false) {
Expand Down Expand Up @@ -100,11 +102,7 @@ public function increment(string $service): void
{
$t = microtime(true);

$expires = $t - $this->configuration->timeWindow();

if ($this->redis->zRemRangeByScore($service, '-inf', $expires) === false) {
throw new StorageException('Failed to remove expired elements. service: ' . $service);
}
$this->removeExpiredElements($service);

$this->redis->zAdd($service, $t, $t);
}
Expand Down Expand Up @@ -179,4 +177,18 @@ public function reset(): void
{
// TODO: Implement reset() method.
}

/**
* @param string $service
*
* @throws StorageException
*/
private function removeExpiredElements(string $service): void
{
$expires = microtime(true) - $this->configuration->timeWindow();

if ($this->redis->zRemRangeByScore($service, '-inf', $expires) === false) {
throw new StorageException('Failed to remove expired elements. service: ' . $service);
}
}
}
18 changes: 16 additions & 2 deletions tests/Ackintosh/Ganesha/Storage/Adapter/AbstractRedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ public function incrementAndLoad()
$this->assertSame(2, $result);
}

/**
* @test
* @expectedException \Ackintosh\Ganesha\Exception\StorageException
* @expectedExceptionMessageRegExp /\AFailed to remove expired elements/
*/
public function incrementThrowsExceptionWhenFailedToRunzRemRangeByScore()
{
$mock = $this->getMockBuilder(\Redis::class)->getMock();
$mock->method('zRemRangeByScore')
->willReturn(false);

$this->createAdapterWithMock($mock)->increment($this->service);
}

/**
* @test
* @expectedException \Ackintosh\Ganesha\Exception\StorageException
Expand Down Expand Up @@ -125,13 +139,13 @@ public function incrementThrowsException()
* @expectedException \Ackintosh\Ganesha\Exception\StorageException
* @expectedExceptionMessageRegExp /\AFailed to remove expired elements/
*/
public function incrementThrowsExceptionWhenFailedToRunzRemRangeByScore()
public function loadThrowsExceptionWhenFailedToRunzRemRangeByScore()
{
$mock = $this->getMockBuilder(\Redis::class)->getMock();
$mock->method('zRemRangeByScore')
->willReturn(false);

$this->createAdapterWithMock($mock)->increment($this->service);
$this->createAdapterWithMock($mock)->load($this->service);
}

/**
Expand Down

0 comments on commit bbef016

Please sign in to comment.