Skip to content

Commit

Permalink
Run the functional tests on redis
Browse files Browse the repository at this point in the history
  • Loading branch information
sunspikes committed Nov 8, 2017
1 parent 6901f96 commit 16358df
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
language: php

services:
- redis-server

php:
- 7.0
- 7.1

before_script:
- echo "extension = redis.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
- composer self-update
- composer install --prefer-source --dev

Expand Down
4 changes: 0 additions & 4 deletions src/Cache/ThrottlerCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@ public function setItem(string $key, ThrottlerItemInterface $item): bool
{
try {
$cacheItem = $this->cacheItemPool->getItem($key);

if ($cacheItem->isHit()) {
$this->cacheItemPool->deleteItem($key);
}
} catch (PsrCacheException $e) {
throw new CacheAdapterException($e->getMessage(), $e->getCode(), $e->getPrevious());
}
Expand Down
51 changes: 40 additions & 11 deletions tests/Functional/AbstractThrottlerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Sunspikes\Tests\Ratelimit\Functional;

use Cache\Adapter\PHPArray\ArrayCachePool;
use Cache\Adapter\Redis\RedisCachePool;
use PHPUnit\Framework\TestCase;
use Sunspikes\Ratelimit\Cache\ThrottlerCache;
use Sunspikes\Ratelimit\Cache\ThrottlerCacheInterface;
Expand All @@ -15,25 +16,38 @@ abstract class AbstractThrottlerTestCase extends TestCase
*/
protected $ratelimiter;

/**
* @var array
*/
protected $cache = [];

/**
* @inheritdoc
*/
protected function setUp()
{
$pool = new ArrayCachePool(null, $this->cache);
$pool = $this->getCachePool();
$cache = new ThrottlerCache($pool);

$this->ratelimiter = $this->createRatelimiter($cache);
}

/**
* Get the cache pool adapter to use
*
* @return ArrayCachePool|RedisCachePool
*/
private function getCachePool()
{
if (class_exists(\Redis::class)) {
$redis = new \Redis();
if (true === $redis->connect('localhost')) {
return new RedisCachePool($redis);
}
}

return new ArrayCachePool();
}

public function testThrottlePreLimit()
{
$throttle = $this->ratelimiter->get('pre-limit-test');
$key = $this->getRateLimiterKey('pre-limit-test');
$throttle = $this->ratelimiter->get($key);

for ($i = 0; ++$i < $this->getMaxAttempts();) {
$throttle->hit();
Expand All @@ -44,7 +58,8 @@ public function testThrottlePreLimit()

public function testThrottlePostLimit()
{
$throttle = $this->ratelimiter->get('post-limit-test');
$key = $this->getRateLimiterKey('post-limit-test');
$throttle = $this->ratelimiter->get($key);

for ($i = 0; $i < $this->getMaxAttempts(); $i++) {
$throttle->hit();
Expand All @@ -55,7 +70,8 @@ public function testThrottlePostLimit()

public function testThrottleAccess()
{
$throttle = $this->ratelimiter->get('access-test');
$key = $this->getRateLimiterKey('access-test');
$throttle = $this->ratelimiter->get($key);

for ($i = 0; $i < $this->getMaxAttempts(); $i++) {
$throttle->access();
Expand All @@ -66,7 +82,8 @@ public function testThrottleAccess()

public function testThrottleCount()
{
$throttle = $this->ratelimiter->get('count-test');
$key = $this->getRateLimiterKey('count-test');
$throttle = $this->ratelimiter->get($key);

for ($i = 0; $i < $this->getMaxAttempts(); $i++) {
$throttle->access();
Expand All @@ -77,13 +94,25 @@ public function testThrottleCount()

public function testClear()
{
$throttle = $this->ratelimiter->get('clear-test');
$key = $this->getRateLimiterKey('clear-test');
$throttle = $this->ratelimiter->get($key);
$throttle->hit();
$throttle->clear();

self::assertEquals(0, $throttle->count());
}

/**
* Get an unique key based on throttling mode
*
* @param string $key
* @return string
*/
private function getRateLimiterKey(string $key): string
{
return $key .'-'. strtolower(static::class);
}

/**
* @return int
*/
Expand Down

0 comments on commit 16358df

Please sign in to comment.