Skip to content

Commit 81730d4

Browse files
Merge branch '5.1' into 5.x
* 5.1: [Contracts] add branch-aliases for dev-main [Cache] Make Redis initializers static [Messenger] Fixed typos in Connection [CI] Fixed build on AppVeyor Fix tests typo [Lock] Reset Key lifetime time before we acquire it [CI] Silence errors when remove file/dir on test tearDown() Fix tests Remove content-type check on toArray methods
2 parents d461aa0 + a82a67d commit 81730d4

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

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) {

Tests/LockTest.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ public function exists(Key $key)
460460
return isset($this->keys[spl_object_hash($key)]);
461461
}
462462

463-
public function putOffExpiration(Key $key, float $ttl)
463+
public function putOffExpiration(Key $key, $ttl)
464464
{
465465
$key->reduceLifetime($ttl);
466466
$this->checkNotExpired($key);
@@ -476,6 +476,52 @@ public function putOffExpiration(Key $key, float $ttl)
476476
$lock->release();
477477
}
478478

479+
/**
480+
* @group time-sensitive
481+
*/
482+
public function testAcquireTwiceWithExpiration()
483+
{
484+
$key = new Key(uniqid(__METHOD__, true));
485+
$store = new class() implements PersistingStoreInterface {
486+
use ExpiringStoreTrait;
487+
private $keys = [];
488+
private $initialTtl = 30;
489+
490+
public function save(Key $key)
491+
{
492+
$key->reduceLifetime($this->initialTtl);
493+
$this->keys[spl_object_hash($key)] = $key;
494+
$this->checkNotExpired($key);
495+
496+
return true;
497+
}
498+
499+
public function delete(Key $key)
500+
{
501+
unset($this->keys[spl_object_hash($key)]);
502+
}
503+
504+
public function exists(Key $key)
505+
{
506+
return isset($this->keys[spl_object_hash($key)]);
507+
}
508+
509+
public function putOffExpiration(Key $key, $ttl)
510+
{
511+
$key->reduceLifetime($ttl);
512+
$this->checkNotExpired($key);
513+
}
514+
};
515+
$ttl = 1;
516+
$lock = new Lock($key, $store, $ttl);
517+
518+
$this->assertTrue($lock->acquire());
519+
$lock->release();
520+
sleep($ttl + 1);
521+
$this->assertTrue($lock->acquire());
522+
$lock->release();
523+
}
524+
479525
public function testAcquireReadBlockingWithBlockingSharedLockStoreInterface()
480526
{
481527
$key = new Key(uniqid(__METHOD__, true));

0 commit comments

Comments
 (0)