Reset lifetime on acquireRead()

This commit is contained in:
Nyholm 2020-10-14 08:54:02 +02:00
parent 8fa0573ab6
commit de412bf24b
No known key found for this signature in database
GPG Key ID: D6332DE2B6F8FA38
2 changed files with 48 additions and 0 deletions

View File

@ -121,6 +121,7 @@ final class Lock implements SharedLockInterface, LoggerAwareInterface
*/
public function acquireRead(bool $blocking = false): bool
{
$this->key->resetLifetime();
try {
if (!$this->store instanceof SharedLockStoreInterface) {
$this->logger->debug('Store does not support ReadLocks, fallback to WriteLock.', ['resource' => $this->key]);

View File

@ -20,6 +20,7 @@ use Symfony\Component\Lock\Key;
use Symfony\Component\Lock\Lock;
use Symfony\Component\Lock\PersistingStoreInterface;
use Symfony\Component\Lock\SharedLockStoreInterface;
use Symfony\Component\Lock\Store\ExpiringStoreTrait;
/**
* @author Jérémy Derussé <jeremy@derusse.com>
@ -429,6 +430,52 @@ class LockTest extends TestCase
$this->assertTrue($lock->acquireRead(false));
}
/**
* @group time-sensitive
*/
public function testAcquireReadTwiceWithExpiration()
{
$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, float $ttl)
{
$key->reduceLifetime($ttl);
$this->checkNotExpired($key);
}
};
$ttl = 1;
$lock = new Lock($key, $store, $ttl);
$this->assertTrue($lock->acquireRead());
$lock->release();
sleep($ttl + 1);
$this->assertTrue($lock->acquireRead());
$lock->release();
}
public function testAcquireReadBlockingWithBlockingSharedLockStoreInterface()
{
$key = new Key(uniqid(__METHOD__, true));