Skip to content

Commit

Permalink
feature #32180 [Lock] add an InvalidTTLException to be more accurate …
Browse files Browse the repository at this point in the history
…(Simperfit)

This PR was merged into the 4.4 branch.

Discussion
----------

[Lock] add an InvalidTTLException to be more accurate

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | yes <!-- please update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | none   <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | not needed<!-- required for new features -->

<!--
Replace this notice by a short README for your feature/bugfix. This will help people
understand your PR and can be used as a start for the documentation.

Additionally (see https://symfony.com/roadmap):
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too).
 - Features and deprecations must be submitted against branch 4.4.
 - Legacy code removals go to the master branch.
-->

This adds a new exception to be more precise when using wrong ttl.

Commits
-------

3750919 [Lock] add an InvalidTTLException to be more accurate
  • Loading branch information
fabpot committed Jun 29, 2019
2 parents 3644b70 + 3750919 commit 8dd5464
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/Symfony/Component/Lock/CHANGELOG.md
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.4.0
-----

* added InvalidTtlException

4.2.0
-----

Expand Down
19 changes: 19 additions & 0 deletions src/Symfony/Component/Lock/Exception/InvalidTtlException.php
@@ -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
{
}
3 changes: 2 additions & 1 deletion src/Symfony/Component/Lock/Store/MemcachedStore.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions src/Symfony/Component/Lock/Store/PdoStore.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Lock/Store/RedisStore.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Lock/Tests/Store/MemcachedStoreTest.php
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Lock\Tests\Store;

use Symfony\Component\Lock\Key;
use Symfony\Component\Lock\Store\MemcachedStore;

/**
Expand Down Expand Up @@ -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);
}
}
18 changes: 18 additions & 0 deletions src/Symfony/Component/Lock/Tests/Store/PdoStoreTest.php
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Lock\Tests\Store;

use Symfony\Component\Lock\Key;
use Symfony\Component\Lock\Store\PdoStore;

/**
Expand Down Expand Up @@ -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);
}
}
10 changes: 10 additions & 0 deletions src/Symfony/Component/Lock/Tests/Store/RedisStoreTest.php
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Lock\Tests\Store;

use Symfony\Component\Lock\Store\RedisStore;

/**
* @author Jérémy Derussé <jeremy@derusse.com>
*
Expand All @@ -33,4 +35,12 @@ protected function getRedisConnection()

return $redis;
}

/**
* @expectedException \Symfony\Component\Lock\Exception\InvalidTtlException
*/
public function testInvalidTtl()
{
new RedisStore($this->getRedisConnection(), -1);
}
}

0 comments on commit 8dd5464

Please sign in to comment.