8000 [Lock] Reset Key lifetime time before we acquire it · symfony/symfony@f87c11e · GitHub
[go: up one dir, main page]

Skip to content

Commit f87c11e

Browse files
committed
[Lock] Reset Key lifetime time before we acquire it
1 parent edb5fed commit f87c11e

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
@@ -66,6 +66,7 @@ public function __destruct()
6666
*/
6767
public function acquire(bool $blocking = false): bool
6868
{
69+
$this->key->resetLifetime();
6970
try {
7071
if ($blocking) {
7172
if (!$this->store instanceof BlockingStoreInterface) {

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é <jeremy@derusse.com>
@@ -504,4 +505,50 @@ public function testAcquireReadBlockingWithPersistingStoreInterface()
504505

505506
$this->assertTrue($lock->acquireRead(true));
506507
}
508+
509+
/**
510+
* @group time-sensitive
511+
*/
512+
public function testAcquireTwiceWithExpiration()
513+
{
514+
$key = new Key(uniqid(__METHOD__, true));
515+
$store = new class() implements PersistingStoreInterface {
516+
use ExpiringStoreTrait;
517+
private $keys = [];
518+
private $initialTtl = 30;
519+
520+
public function save(Key $key)
521+
{
522+
$key->reduceLifetime($this->initialTtl);
523+
$this->keys[spl_object_hash($key)] = $key;
524+
$this->checkNotExpired($key);
525+
526+
return true;
527+
}
528+
529+
public function delete(Key $key)
530+
{
531+
unset($this->keys[spl_object_hash($key)]);
532+
}
533+
534+
public function exists(Key $key)
535+
{
536+
return isset($this->keys[spl_object_hash($key)]);
537+
}
538+
539+
public function putOffExpiration(Key $key, float $ttl)
540+
{
541+
$key->reduceLifetime($ttl);
542+
$this->checkNotExpired($key);
543+
}
544+
};
545+
$ttl = 1;
546+
$lock = new Lock($key, $store, $ttl);
547+
548+
$this->assertTrue($lock->acquire());
549+
$lock->release();
550+
sleep($ttl + 1);
551+
$this->assertTrue($lock->acquire());
552+
$lock->release();
553+
}
507554
}

0 commit comments

Comments
 (0)
0