From 37509192d8a0712bcaa095ec686465dfd5f4c384 Mon Sep 17 00:00:00 2001 From: Amrouche Hamza Date: Wed, 26 Jun 2019 08:45:45 +0200 Subject: [PATCH] [Lock] add an InvalidTTLException to be more accurate --- src/Symfony/Component/Lock/CHANGELOG.md | 5 +++++ .../Lock/Exception/InvalidTtlException.php | 19 +++++++++++++++++++ .../Component/Lock/Store/MemcachedStore.php | 3 ++- src/Symfony/Component/Lock/Store/PdoStore.php | 5 +++-- .../Component/Lock/Store/RedisStore.php | 3 ++- .../Lock/Tests/Store/MemcachedStoreTest.php | 10 ++++++++++ .../Lock/Tests/Store/PdoStoreTest.php | 18 ++++++++++++++++++ .../Lock/Tests/Store/RedisStoreTest.php | 10 ++++++++++ 8 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 src/Symfony/Component/Lock/Exception/InvalidTtlException.php diff --git a/src/Symfony/Component/Lock/CHANGELOG.md b/src/Symfony/Component/Lock/CHANGELOG.md index df0d3b9183..b76988b409 100644 --- a/src/Symfony/Component/Lock/CHANGELOG.md +++ b/src/Symfony/Component/Lock/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +4.4.0 +----- + + * added InvalidTtlException + 4.2.0 ----- diff --git a/src/Symfony/Component/Lock/Exception/InvalidTtlException.php b/src/Symfony/Component/Lock/Exception/InvalidTtlException.php new file mode 100644 index 0000000000..3b6cd55b98 --- /dev/null +++ b/src/Symfony/Component/Lock/Exception/InvalidTtlException.php @@ -0,0 +1,19 @@ + + * + * 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 + */ +class InvalidTtlException extends InvalidArgumentException implements ExceptionInterface +{ +} diff --git a/src/Symfony/Component/Lock/Store/MemcachedStore.php b/src/Symfony/Component/Lock/Store/MemcachedStore.php index 9b84303fc3..3857dfc689 100644 --- a/src/Symfony/Component/Lock/Store/MemcachedStore.php +++ b/src/Symfony/Component/Lock/Store/MemcachedStore.php @@ -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. diff --git a/src/Symfony/Component/Lock/Store/PdoStore.php b/src/Symfony/Component/Lock/Store/PdoStore.php index 0cf3dd35f7..159c6f514b 100644 --- a/src/Symfony/Component/Lock/Store/PdoStore.php +++ b/src/Symfony/Component/Lock/Store/PdoStore.php @@ -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); diff --git a/src/Symfony/Component/Lock/Store/RedisStore.php b/src/Symfony/Component/Lock/Store/RedisStore.php index 496ce65778..06fd66481c 100644 --- a/src/Symfony/Component/Lock/Store/RedisStore.php +++ b/src/Symfony/Component/Lock/Store/RedisStore.php @@ -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; diff --git a/src/Symfony/Component/Lock/Tests/Store/MemcachedStoreTest.php b/src/Symfony/Component/Lock/Tests/Store/MemcachedStoreTest.php index f4ab571f56..345fc7e249 100644 --- a/src/Symfony/Component/Lock/Tests/Store/MemcachedStoreTest.php +++ b/src/Symfony/Component/Lock/Tests/Store/MemcachedStoreTest.php @@ -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); + } } diff --git a/src/Symfony/Component/Lock/Tests/Store/PdoStoreTest.php b/src/Symfony/Component/Lock/Tests/Store/PdoStoreTest.php index 45e3544e2b..14bf1d70af 100644 --- a/src/Symfony/Component/Lock/Tests/Store/PdoStoreTest.php +++ b/src/Symfony/Component/Lock/Tests/Store/PdoStoreTest.php @@ -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); + } } diff --git a/src/Symfony/Component/Lock/Tests/Store/RedisStoreTest.php b/src/Symfony/Component/Lock/Tests/Store/RedisStoreTest.php index 6c7d244107..878c48305d 100644 --- a/src/Symfony/Component/Lock/Tests/Store/RedisStoreTest.php +++ b/src/Symfony/Component/Lock/Tests/Store/RedisStoreTest.php @@ -11,6 +11,8 @@ namespace Symfony\Component\Lock\Tests\Store; +use Symfony\Component\Lock\Store\RedisStore; + /** * @author Jérémy Derussé * @@ -33,4 +35,12 @@ class RedisStoreTest extends AbstractRedisStoreTest return $redis; } + + /** + * @expectedException \Symfony\Component\Lock\Exception\InvalidTtlException + */ + public function testInvalidTtl() + { + new RedisStore($this->getRedisConnection(), -1); + } }