Skip to content

Commit a82a67d

Browse files
Merge branch '4.4' into 5.1
* 4.4: [Contracts] add branch-aliases for dev-main [Cache] Make Redis initializers static Fix tests typo [Lock] Reset Key lifetime time before we acquire it [CI] Silence errors when remove file/dir on test tearDown()
2 parents a7f7bd2 + 620d064 commit a82a67d

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

Lock.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public function __destruct()
6767
*/
6868
public function acquire(bool $blocking = false): bool
6969
{
70+
$this->key->resetLifetime();
7071
try {
7172
if ($blocking) {
7273
if (!$this->store instanceof BlockingStoreInterface) {

Tests/LockTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Lock\Key;
1919
use Symfony\Component\Lock\Lock;
2020
use Symfony\Component\Lock\PersistingStoreInterface;
21+
use Symfony\Component\Lock\Store\ExpiringStoreTrait;
2122

2223
/**
2324
* @author Jérémy Derussé <jeremy@derusse.com>
@@ -372,4 +373,50 @@ public function provideExpiredDates()
372373
yield [[0.1], false];
373374
yield [[-0.1, null], false];
374375
}
376+
377+
/**
378+
* @group time-sensitive
379+
*/
380+
public function testAcquireTwiceWithExpiration()
381+
{
382+
$key = new Key(uniqid(__METHOD__, true));
383+
$store = new class() implements PersistingStoreInterface {
384+
use ExpiringStoreTrait;
385+
private $keys = [];
386+
private $initialTtl = 30;
387+
388+
public function save(Key $key)
389+
{
390+
$key->reduceLifetime($this->initialTtl);
391+
$this->keys[spl_object_hash($key)] = $key;
392+
$this->checkNotExpired($key);
393+
394+
return true;
395+
}
396+
397+
public function delete(Key $key)
398+
{
399+
unset($this->keys[spl_object_hash($key)]);
400+
}
401+
402+
public function exists(Key $key)
403+
{
404+
return isset($this->keys[spl_object_hash($key)]);
405+
}
406+
407+
public function putOffExpiration(Key $key, $ttl)
408+
{
409+
$key->reduceLifetime($ttl);
410+
$this->checkNotExpired($key);
411+
}
412+
};
413+
$ttl = 1;
414+
$lock = new Lock($key, $store, $ttl);
415+
416+
$this->assertTrue($lock->acquire());
417+
$lock->release();
418+
sleep($ttl + 1);
419+
$this->assertTrue($lock->acquire());
420+
$lock->release();
421+
}
375422
}

0 commit comments

Comments
 (0)