Skip to content

[lock] Fix non-blocking store fallback #38329

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
Sep 28, 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
16 changes: 14 additions & 2 deletions src/Symfony/Component/Lock/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public function acquire(bool $blocking = false): bool
if (!$this->store instanceof BlockingStoreInterface) {
while (true) {
try {
$this->store->wait($this->key);
$this->store->save($this->key);
break;
} catch (LockConflictedException $e) {
usleep((100 + random_int(-10, 10)) * 1000);
}
Expand Down Expand Up @@ -127,7 +128,18 @@ public function acquireRead(bool $blocking = false): bool
return $this->acquire($blocking);
}
if ($blocking) {
$this->store->waitAndSaveRead($this->key);
if (!$this->store instanceof BlockingSharedLockStoreInterface) {
while (true) {
try {
$this->store->saveRead($this->key);
break;
} catch (LockConflictedException $e) {
usleep((100 + random_int(-10, 10)) * 1000);
}
}
} else {
$this->store->waitAndSaveRead($this->key);
}
} else {
$this->store->saveRead($this->key);
}
Expand Down
136 changes: 134 additions & 2 deletions src/Symfony/Component/Lock/Tests/LockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@

use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Component\Lock\BlockingSharedLockStoreInterface;
use Symfony\Component\Lock\BlockingStoreInterface;
use Symfony\Component\Lock\Exception\LockConflictedException;
use Symfony\Component\Lock\Key;
use Symfony\Component\Lock\Lock;
use Symfony\Component\Lock\PersistingStoreInterface;
use Symfony\Component\Lock\SharedLockStoreInterface;

/**
* @author Jérémy Derussé <jeremy@derusse.com>
Expand All @@ -40,7 +42,7 @@ public function testAcquireNoBlocking()
$this->assertTrue($lock->acquire(false));
}

public function testAcquireNoBlockingStoreInterface()
public function testAcquireNoBlockingWithPersistingStoreInterface()
{
$key = new Key(uniqid(__METHOD__, true));
$store = $this->getMockBuilder(PersistingStoreInterface::class)->getMock();
Expand All @@ -56,6 +58,44 @@ public function testAcquireNoBlockingStoreInterface()
$this->assertTrue($lock->acquire(false));
}

public function testAcquireBlockingWithPersistingStoreInterface()
{
$key = new Key(uniqid(__METHOD__, true));
$store = $this->getMockBuilder(PersistingStoreInterface::class)->getMock();
$lock = new Lock($key, $store);

$store
->expects($this->once())
->method('save');
$store
->method('exists')
->willReturnOnConsecutiveCalls(true, false);

$this->assertTrue($lock->acquire(true));
}

public function testAcquireBlockingRetryWithPersistingStoreInterface()
{
$key = new Key(uniqid(__METHOD__, true));
$store = $this->getMockBuilder(PersistingStoreInterface::class)->getMock();
$lock = new Lock($key, $store);

$store
->expects($this->any())
->method('save')
->willReturnCallback(static function () {
if (1 === random_int(0, 1)) {
return;
}
throw new LockConflictedException('boom');
});
$store
->method('exists')
->willReturnOnConsecutiveCalls(true, false);

$this->assertTrue($lock->acquire(true));
}

public function testAcquireReturnsFalse()
{
$key = new Key(uniqid(__METHOD__, true));
Expand Down Expand Up @@ -90,7 +130,7 @@ public function testAcquireReturnsFalseStoreInterface()
$this->assertFalse($lock->acquire(false));
}

public function testAcquireBlocking()
public function testAcquireBlockingWithBlockingStoreInterface()
{
$key = new Key(uniqid(__METHOD__, true));
$store = $this->createMock(BlockingStoreInterface::class);
Expand Down Expand Up @@ -372,4 +412,96 @@ public function provideExpiredDates()
yield [[0.1], false];
yield [[-0.1, null], false];
}

public function testAcquireReadNoBlockingWithSharedLockStoreInterface()
{
$key = new Key(uniqid(__METHOD__, true));
$store = $this->createMock(SharedLockStoreInterface::class);
$lock = new Lock($key, $store);

$store
->expects($this->once())
->method('saveRead');
$store
->method('exists')
->willReturnOnConsecutiveCalls(true, false);

$this->assertTrue($lock->acquireRead(false));
}

public function testAcquireReadBlockingWithBlockingSharedLockStoreInterface()
{
$key = new Key(uniqid(__METHOD__, true));
$store = $this->createMock(BlockingSharedLockStoreInterface::class);
$lock = new Lock($key, $store);

$store
->expects($this->once())
->method('waitAndSaveRead');
$store
->method('exists')
->willReturnOnConsecutiveCalls(true, false);

$this->assertTrue($lock->acquireRead(true));
}

public function testAcquireReadBlockingWithSharedLockStoreInterface()
{
$key = new Key(uniqid(__METHOD__, true));
$store = $this->createMock(SharedLockStoreInterface::class);
$lock = new Lock($key, $store);

$store
->expects($this->any())
->method('saveRead')
->willReturnCallback(static function () {
if (1 === random_int(0, 1)) {
return;
}
throw new LockConflictedException('boom');
});
$store
->method('exists')
->willReturnOnConsecutiveCalls(true, false);

$this->assertTrue($lock->acquireRead(true));
}

public function testAcquireReadBlockingWithBlockingLockStoreInterface()
{
$key = new Key(uniqid(__METHOD__, true));
$store = $this->createMock(BlockingStoreInterface::class);
$lock = new Lock($key, $store);

$store
->expects($this->once())
->method('waitAndSave');
$store
->method('exists')
->willReturnOnConsecutiveCalls(true, false);

$this->assertTrue($lock->acquireRead(true));
}

public function testAcquireReadBlockingWithPersistingStoreInterface()
{
$key = new Key(uniqid(__METHOD__, true));
$store = $this->createMock(PersistingStoreInterface::class);
$lock = new Lock($key, $store);

$store
->expects($this->any())
->method('save')
->willReturnCallback(static function () {
if (1 === random_int(0, 1)) {
return;
}
throw new LockConflictedException('boom');
});
$store
->method('exists')
->willReturnOnConsecutiveCalls(true, false);

$this->assertTrue($lock->acquireRead(true));
}
}