Skip to content

Commit 8430954

Browse files
committed
bug symfony#38559 [Lock] Reset lifetime on acquireRead() (Nyholm)
This PR was merged into the 5.x branch. Discussion ---------- [Lock] Reset lifetime on acquireRead() | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | n/a Same as symfony#38553 but this is for `acquireRead()` instead of `acquire()`. `acquireRead()` is new in 5.2. Commits ------- de412bf Reset lifetime on acquireRead()
2 parents 8fa0573 + de412bf commit 8430954

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/Symfony/Component/Lock/Lock.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ public function acquire(bool $blocking = false): bool
121121
*/
122122
public function acquireRead(bool $blocking = false): bool
123123
{
124+
$this->key->resetLifetime();
124125
try {
125126
if (!$this->store instanceof SharedLockStoreInterface) {
126127
$this->logger->debug('Store does not support ReadLocks, fallback to WriteLock.', ['resource' => $this->key]);

src/Symfony/Component/Lock/Tests/LockTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Lock\Lock;
2121
use Symfony\Component\Lock\PersistingStoreInterface;
2222
use Symfony\Component\Lock\SharedLockStoreInterface;
23+
use Symfony\Component\Lock\Store\ExpiringStoreTrait;
2324

2425
/**
2526
* @author Jérémy Derussé <[email protected]>
@@ -429,6 +430,52 @@ public function testAcquireReadNoBlockingWithSharedLockStoreInterface()
429430
$this->assertTrue($lock->acquireRead(false));
430431
}
431432

433+
/**
434+
* @group time-sensitive
435+
*/
436+
public function testAcquireReadTwiceWithExpiration()
437+
{
438+
$key = new Key(uniqid(__METHOD__, true));
439+
$store = new class() implements PersistingStoreInterface {
440+
use ExpiringStoreTrait;
441+
private $keys = [];
442+
private $initialTtl = 30;
443+
444+
public function save(Key $key)
445+
{
446+
$key->reduceLifetime($this->initialTtl);
447+
$this->keys[spl_object_hash($key)] = $key;
448+
$this->checkNotExpired($key);
449+
450+
return true;
451+
}
452+
453+
public function delete(Key $key)
454+
{
455+
unset($this->keys[spl_object_hash($key)]);
456+
}
457+
458+
public function exists(Key $key)
459+
{
460+
return isset($this->keys[spl_object_hash($key)]);
461+
}
462+
463+
public function putOffExpiration(Key $key, float $ttl)
464+
{
465+
$key->reduceLifetime($ttl);
466+
$this->checkNotExpired($key);
467+
}
468+
};
469+
$ttl = 1;
470+
$lock = new Lock($key, $store, $ttl);
471+
472+
$this->assertTrue($lock->acquireRead());
473+
$lock->release();
474+
sleep($ttl + 1);
475+
$this->assertTrue($lock->acquireRead());
476+
$lock->release();
477+
}
478+
432479
public function testAcquireReadBlockingWithBlockingSharedLockStoreInterface()
433480
{
434481
$key = new Key(uniqid(__METHOD__, true));

0 commit comments

Comments
 (0)