[Lock] Reset Key lifetime time before we acquire it

This commit is contained in:
Nyholm 2020-10-13 18:10:46 +02:00
parent 15498970e6
commit 55ad70225a
No known key found for this signature in database
GPG Key ID: D6332DE2B6F8FA38
2 changed files with 48 additions and 0 deletions

View File

@ -67,6 +67,7 @@ final class Lock implements LockInterface, LoggerAwareInterface
*/
public function acquire($blocking = false): bool
{
$this->key->resetLifetime();
try {
if ($blocking) {
if (!$this->store instanceof StoreInterface && !$this->store instanceof BlockingStoreInterface) {

View File

@ -18,6 +18,7 @@ use Symfony\Component\Lock\Exception\LockConflictedException;
use Symfony\Component\Lock\Key;
use Symfony\Component\Lock\Lock;
use Symfony\Component\Lock\PersistingStoreInterface;
use Symfony\Component\Lock\Store\ExpiringStoreTrait;
use Symfony\Component\Lock\StoreInterface;
/**
@ -392,4 +393,50 @@ class LockTest extends TestCase
yield [[0.1], false];
yield [[-0.1, null], false];
}
/**
* @group time-sensitive
*/
public function testAcquireTwiceWithExpiration()
{
$key = new Key(uniqid(__METHOD__, true));
$store = new class() implements PersistingStoreInterface {
use ExpiringStoreTrait;
private $keys = [];
private $initialTtl = 30;
public function save(Key $key)
{
$key->reduceLifetime($this->initialTtl);
$this->keys[spl_object_hash($key)] = $key;
$this->checkNotExpired($key);
return true;
}
public function delete(Key $key)
{
unset($this->keys[spl_object_hash($key)]);
}
public function exists(Key $key)
{
return isset($this->keys[spl_object_hash($key)]);
}
public function putOffExpiration(Key $key, $ttl)
{
$key->reduceLifetime($ttl);
$this->checkNotExpired($key);
}
};
$ttl = 1;
$lock = new Lock($key, $store, $ttl);
$this->assertTrue($lock->acquire());
$lock->release();
sleep($ttl + 1);
$this->assertTrue($lock->acquire());
$lock->release();
}
}