Skip to content

Commit 5a1a904

Browse files
committed
[Lock] add an InvalidTTLException to be more accurate
1 parent efaa154 commit 5a1a904

File tree

8 files changed

+70
-4
lines changed

8 files changed

+70
-4
lines changed

src/Symfony/Component/Lock/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.4.0
5+
-----
6+
7+
* added InvalidTTLException
8+
49
4.2.0
510
-----
611

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Lock\Exception;
13+
14+
/**
15+
* @author Amrouche Hamza <hamza.simperfit@gmail.com>
16+
*/
17+
class InvalidTTLException extends InvalidArgumentException implements ExceptionInterface
18+
{
19+
}

src/Symfony/Component/Lock/Store/MemcachedStore.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Lock\Store;
1313

1414
use Symfony\Component\Lock\Exception\InvalidArgumentException;
15+
use Symfony\Component\Lock\Exception\InvalidTTLException;
1516
use Symfony\Component\Lock\Exception\LockConflictedException;
1617
use Symfony\Component\Lock\Key;
1718
use Symfony\Component\Lock\StoreInterface;
@@ -79,7 +80,7 @@ public function waitAndSave(Key $key)
7980
public function putOffExpiration(Key $key, $ttl)
8081
{
8182
if ($ttl < 1) {
82-
throw new InvalidArgumentException(sprintf('%s() expects a TTL greater or equals to 1 second. Got %s.', __METHOD__, $ttl));
83+
throw new InvalidTTLException(sprintf('%s() expects a TTL greater or equals to 1 second. Got %s.', __METHOD__, $ttl));
8384
}
8485

8586
// Interface defines a float value but Store required an integer.

src/Symfony/Component/Lock/Store/PdoStore.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Doctrine\DBAL\DBALException;
1616
use Doctrine\DBAL\Schema\Schema;
1717
use Symfony\Component\Lock\Exception\InvalidArgumentException;
18+
use Symfony\Component\Lock\Exception\InvalidTTLException;
1819
use Symfony\Component\Lock\Exception\LockConflictedException;
1920
use Symfony\Component\Lock\Exception\NotSupportedException;
2021
use Symfony\Component\Lock\Key;
@@ -80,7 +81,7 @@ public function __construct($connOrDsn, array $options = [], float $gcProbabilit
8081
throw new InvalidArgumentException(sprintf('"%s" requires gcProbability between 0 and 1, "%f" given.', __METHOD__, $gcProbability));
8182
}
8283
if ($initialTtl < 1) {
83-
throw new InvalidArgumentException(sprintf('%s() expects a strictly positive TTL, "%d" given.', __METHOD__, $initialTtl));
84+
throw new InvalidTTLException(sprintf('%s() expects a strictly positive TTL, "%d" given.', __METHOD__, $initialTtl));
8485
}
8586

8687
if ($connOrDsn instanceof \PDO) {
@@ -153,7 +154,7 @@ public function waitAndSave(Key $key)
153154
public function putOffExpiration(Key $key, $ttl)
154155
{
155156
if ($ttl < 1) {
156-
throw new InvalidArgumentException(sprintf('%s() expects a TTL greater or equals to 1 second. Got %s.', __METHOD__, $ttl));
157+
throw new InvalidTTLException(sprintf('%s() expects a TTL greater or equals to 1 second. Got %s.', __METHOD__, $ttl));
157158
}
158159

159160
$key->reduceLifetime($ttl);

src/Symfony/Component/Lock/Store/RedisStore.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Cache\Traits\RedisClusterProxy;
1515
use Symfony\Component\Cache\Traits\RedisProxy;
1616
use Symfony\Component\Lock\Exception\InvalidArgumentException;
17+
use Symfony\Component\Lock\Exception\InvalidTTLException;
1718
use Symfony\Component\Lock\Exception\LockConflictedException;
1819
use Symfony\Component\Lock\Key;
1920
use Symfony\Component\Lock\StoreInterface;
@@ -41,7 +42,7 @@ public function __construct($redisClient, float $initialTtl = 300.0)
4142
}
4243

4344
if ($initialTtl <= 0) {
44-
throw new InvalidArgumentException(sprintf('%s() expects a strictly positive TTL. Got %d.', __METHOD__, $initialTtl));
45+
throw new InvalidTTLException(sprintf('%s() expects a strictly positive TTL. Got %d.', __METHOD__, $initialTtl));
4546
}
4647

4748
$this->redis = $redisClient;

src/Symfony/Component/Lock/Tests/Store/MemcachedStoreTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Lock\Tests\Store;
1313

14+
use Symfony\Component\Lock\Key;
1415
use Symfony\Component\Lock\Store\MemcachedStore;
1516

1617
/**
@@ -57,4 +58,13 @@ public function testAbortAfterExpiration()
5758
{
5859
$this->markTestSkipped('Memcached expects a TTL greater than 1 sec. Simulating a slow network is too hard');
5960
}
61+
62+
/**
63+
* @expectedException \Symfony\Component\Lock\Exception\InvalidTTLException
64+
*/
65+
public function testInvalidTTL()
66+
{
67+
$store = $this->getStore();
68+
$store->putOffExpiration(new Key('toto'), 0.1);
69+
}
6070
}

src/Symfony/Component/Lock/Tests/Store/PdoStoreTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Lock\Tests\Store;
1313

14+
use Symfony\Component\Lock\Key;
1415
use Symfony\Component\Lock\Store\PdoStore;
1516

1617
/**
@@ -57,4 +58,22 @@ public function testAbortAfterExpiration()
5758
{
5859
$this->markTestSkipped('Pdo expects a TTL greater than 1 sec. Simulating a slow network is too hard');
5960
}
61+
62+
/**
63+
* @expectedException \Symfony\Component\Lock\Exception\InvalidTTLException
64+
*/
65+
public function testInvalidTTL()
66+
{
67+
$store = $this->getStore();
68+
$store->putOffExpiration(new Key('toto'), 0.1);
69+
}
70+
71+
/**
72+
* @expectedException \Symfony\Component\Lock\Exception\InvalidTTLException
73+
*/
74+
public function testInvalidTTLConstruct()
75+
{
76+
return new PdoStore('sqlite:'.self::$dbFile, [], 0.1, 0.1);
77+
78+
}
6079
}

src/Symfony/Component/Lock/Tests/Store/RedisStoreTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Lock\Tests\Store;
1313

14+
use Symfony\Component\Lock\Store\RedisStore;
15+
1416
/**
1517
* @author Jérémy Derussé <jeremy@derusse.com>
1618
*
@@ -33,4 +35,12 @@ protected function getRedisConnection()
3335

3436
return $redis;
3537
}
38+
39+
/**
40+
* @expectedException \Symfony\Component\Lock\Exception\InvalidTTLException
41+
*/
42+
public function testInvalidTTL()
43+
{
44+
new RedisStore($this->getRedisConnection(), -1);
45+
}
3646
}

0 commit comments

Comments
 (0)
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