8000 [Lock] Reset lifetime on acquireRead() by Nyholm · Pull Request #38559 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Lock] Reset lifetime on acquireRead() #38559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Lock/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public function acquire(bool $blocking = false): bool
*/
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]);
Expand Down
47 changes: 47 additions & 0 deletions src/Symfony/Component/Lock/Tests/LockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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>
Expand Down Expand Up @@ -429,6 +430,52 @@ public function testAcquireReadNoBlockingWithSharedLockStoreInterface()
$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));
Expand Down
0