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 df0d3b9183564..b76988b409fc0 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 0000000000000..3b6cd55b98898 --- /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 9b84303fc38f0..3857dfc68906c 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 @@ public function waitAndSave(Key $key) 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 0cf3dd35f7a19..159c6f514bef9 100644 --- a/src/Symfony/Component/Lock/Store/PdoStore.php +++ b/src/Symfony/Component/Lock/Store/PdoStore.php @@ -15,6 +15,7 @@ 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 @@ public function __construct($connOrDsn, array $options = [], float $gcProbabilit 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 @@ public function waitAndSave(Key $key) 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 496ce657782fd..06fd66481cc95 100644 --- a/src/Symfony/Component/Lock/Store/RedisStore.php +++ b/src/Symfony/Component/Lock/Store/RedisStore.php @@ -14,6 +14,7 @@ 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 @@ public function __construct($redisClient, float $initialTtl = 300.0) } 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 f4ab571f567a6..345fc7e249b3f 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 @@ public function testAbortAfterExpiration() { $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 45e3544e2bf82..14bf1d70af495 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 @@ public function testAbortAfterExpiration() { $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 6c7d244107b6d..878c48305d61c 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 @@ protected function getRedisConnection() return $redis; } + + /** + * @expectedException \Symfony\Component\Lock\Exception\InvalidTtlException + */ + public function testInvalidTtl() + { + new RedisStore($this->getRedisConnection(), -1); + } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy