[Lock] add an InvalidTTLException to be more accurate

This commit is contained in:
Amrouche Hamza 2019-06-26 08:45:45 +02:00
parent efaa154c74
commit 37509192d8
No known key found for this signature in database
GPG Key ID: E45A3DA456145BC1
8 changed files with 69 additions and 4 deletions

View File

@ -1,6 +1,11 @@
CHANGELOG
=========
4.4.0
-----
* added InvalidTtlException
4.2.0
-----

View File

@ -0,0 +1,19 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Lock\Exception;
/**
* @author Amrouche Hamza <hamza.simperfit@gmail.com>
*/
class InvalidTtlException extends InvalidArgumentException implements ExceptionInterface
{
}

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\Lock\Store;
use Symfony\Component\Lock\Exception\InvalidArgumentException;
use Symfony\Component\Lock\Exception\InvalidTtlException;
use Symfony\Component\Lock\Exception\LockConflictedException;
use Symfony\Component\Lock\Key;
use Symfony\Component\Lock\StoreInterface;
@ -79,7 +80,7 @@ class MemcachedStore implements StoreInterface
public function putOffExpiration(Key $key, $ttl)
{
if ($ttl < 1) {
throw new InvalidArgumentException(sprintf('%s() expects a TTL greater or equals to 1 second. Got %s.', __METHOD__, $ttl));
throw new InvalidTtlException(sprintf('%s() expects a TTL greater or equals to 1 second. Got %s.', __METHOD__, $ttl));
}
// Interface defines a float value but Store required an integer.

View File

@ -15,6 +15,7 @@ use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Schema\Schema;
use Symfony\Component\Lock\Exception\InvalidArgumentException;
use Symfony\Component\Lock\Exception\InvalidTtlException;
use Symfony\Component\Lock\Exception\LockConflictedException;
use Symfony\Component\Lock\Exception\NotSupportedException;
use Symfony\Component\Lock\Key;
@ -80,7 +81,7 @@ class PdoStore implements StoreInterface
throw new InvalidArgumentException(sprintf('"%s" requires gcProbability between 0 and 1, "%f" given.', __METHOD__, $gcProbability));
}
if ($initialTtl < 1) {
throw new InvalidArgumentException(sprintf('%s() expects a strictly positive TTL, "%d" given.', __METHOD__, $initialTtl));
throw new InvalidTtlException(sprintf('%s() expects a strictly positive TTL, "%d" given.', __METHOD__, $initialTtl));
}
if ($connOrDsn instanceof \PDO) {
@ -153,7 +154,7 @@ class PdoStore implements StoreInterface
public function putOffExpiration(Key $key, $ttl)
{
if ($ttl < 1) {
throw new InvalidArgumentException(sprintf('%s() expects a TTL greater or equals to 1 second. Got %s.', __METHOD__, $ttl));
throw new InvalidTtlException(sprintf('%s() expects a TTL greater or equals to 1 second. Got %s.', __METHOD__, $ttl));
}
$key->reduceLifetime($ttl);

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\Lock\Store;
use Symfony\Component\Cache\Traits\RedisClusterProxy;
use Symfony\Component\Cache\Traits\RedisProxy;
use Symfony\Component\Lock\Exception\InvalidArgumentException;
use Symfony\Component\Lock\Exception\InvalidTtlException;
use Symfony\Component\Lock\Exception\LockConflictedException;
use Symfony\Component\Lock\Key;
use Symfony\Component\Lock\StoreInterface;
@ -41,7 +42,7 @@ class RedisStore implements StoreInterface
}
if ($initialTtl <= 0) {
throw new InvalidArgumentException(sprintf('%s() expects a strictly positive TTL. Got %d.', __METHOD__, $initialTtl));
throw new InvalidTtlException(sprintf('%s() expects a strictly positive TTL. Got %d.', __METHOD__, $initialTtl));
}
$this->redis = $redisClient;

View File

@ -11,6 +11,7 @@
namespace Symfony\Component\Lock\Tests\Store;
use Symfony\Component\Lock\Key;
use Symfony\Component\Lock\Store\MemcachedStore;
/**
@ -57,4 +58,13 @@ class MemcachedStoreTest extends AbstractStoreTest
{
$this->markTestSkipped('Memcached expects a TTL greater than 1 sec. Simulating a slow network is too hard');
}
/**
* @expectedException \Symfony\Component\Lock\Exception\InvalidTtlException
*/
public function testInvalidTtl()
{
$store = $this->getStore();
$store->putOffExpiration(new Key('toto'), 0.1);
}
}

View File

@ -11,6 +11,7 @@
namespace Symfony\Component\Lock\Tests\Store;
use Symfony\Component\Lock\Key;
use Symfony\Component\Lock\Store\PdoStore;
/**
@ -57,4 +58,21 @@ class PdoStoreTest extends AbstractStoreTest
{
$this->markTestSkipped('Pdo expects a TTL greater than 1 sec. Simulating a slow network is too hard');
}
/**
* @expectedException \Symfony\Component\Lock\Exception\InvalidTtlException
*/
public function testInvalidTtl()
{
$store = $this->getStore();
$store->putOffExpiration(new Key('toto'), 0.1);
}
/**
* @expectedException \Symfony\Component\Lock\Exception\InvalidTtlException
*/
public function testInvalidTtlConstruct()
{
return new PdoStore('sqlite:'.self::$dbFile, [], 0.1, 0.1);
}
}

View File

@ -11,6 +11,8 @@
namespace Symfony\Component\Lock\Tests\Store;
use Symfony\Component\Lock\Store\RedisStore;
/**
* @author Jérémy Derussé <jeremy@derusse.com>
*
@ -33,4 +35,12 @@ class RedisStoreTest extends AbstractRedisStoreTest
return $redis;
}
/**
* @expectedException \Symfony\Component\Lock\Exception\InvalidTtlException
*/
public function testInvalidTtl()
{
new RedisStore($this->getRedisConnection(), -1);
}
}