Skip to content

Commit 3c6d0f4

Browse files
committed
wip
1 parent f69cbd1 commit 3c6d0f4

File tree

3 files changed

+119
-30
lines changed

3 files changed

+119
-30
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "spiral/symfony-lock-driver",
2+
"name": "roadrunner-php/symfony-lock-driver",
33
"type": "library",
44
"description": "RoadRunner: HTTP and PSR-7 worker",
55
"license": "MIT",

src/RoadRunnerStore.php

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,56 @@
55
namespace Spiral\RoadRunner\Symfony\Lock;
66

77
use RoadRunner\Lock\LockInterface as RrLockInterface;
8+
use Spiral\Goridge\RPC\Exception\RPCException;
9+
use Symfony\Component\Lock\Exception\LockAcquiringException;
10+
use Symfony\Component\Lock\Exception\LockConflictedException;
11+
use Symfony\Component\Lock\Exception\LockReleasingException;
812
use Symfony\Component\Lock\Key;
9-
use Symfony\Component\Lock\PersistingStoreInterface;
1013
use Symfony\Component\Lock\SharedLockStoreInterface;
1114

1215
final class RoadRunnerStore implements SharedLockStoreInterface
1316
{
14-
private string $processId;
15-
1617
public function __construct(
1718
private readonly RrLockInterface $rrLock,
18-
?string $processId = null
19+
private readonly string $processId
1920
) {
20-
$this->processId = $processId ?? '*';
2121
}
2222

2323
public function save(Key $key): void
2424
{
25-
$this->rrLock->lock((string) $key, $this->processId);
25+
try {
26+
$lockId = $this->rrLock->lock((string)$key);
27+
if (false === $lockId) {
28+
throw new LockConflictedException('RoadRunner. Failed to make lock');
29+
}
30+
} catch (RPCException $e) {
31+
throw new LockAcquiringException(message: 'RoadRunner. RPC call error', previous: $e);
32+
}
2633
}
2734

28-
public function exists(Key $key): bool
35+
public function saveRead(Key $key): void
2936
{
30-
return $this->rrLock->exists((string) $key, $this->processId);
37+
if (false === $this->rrLock->lockRead((string)$key)) {
38+
throw new LockConflictedException('RoadRunner. Failed to make read lock');
39+
}
3140
}
3241

33-
public function putOffExpiration(Key $key, float $ttl): void
42+
public function exists(Key $key): bool
3443
{
35-
$this->rrLock->updateTTL((string) $key, $this->processId, $ttl);
44+
return $this->rrLock->exists((string)$key, $this->processId);
3645
}
3746

38-
public function delete(Key $key): void
47+
public function putOffExpiration(Key $key, float $ttl): void
3948
{
40-
$this->rrLock->release((string) $key, $this->processId);
49+
if (false === $this->rrLock->updateTTL((string)$key, $this->processId, $ttl)) {
50+
throw new LockConflictedException('RoadRunner. Failed to update lock ttl');
51+
}
4152
}
4253

43-
public function saveRead(Key $key): void
54+
public function delete(Key $key): void
4455
{
45-
$this->rrLock->lockRead((string) $key, $this->processId);
56+
if (false === $this->rrLock->release((string)$key, $this->processId)) {
57+
throw new LockReleasingException('RoadRunner. Failed to release lock');
58+
}
4659
}
4760
}

tests/RoadRunnerStoreTest.php

Lines changed: 91 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,57 +7,133 @@
77
use PHPUnit\Framework\TestCase;
88
use RoadRunner\Lock\LockInterface as RrLock;
99
use Spiral\RoadRunner\Symfony\Lock\RoadRunnerStore;
10+
11+
use Symfony\Component\Lock\Exception\LockConflictedException;
12+
use Symfony\Component\Lock\Exception\LockReleasingException;
1013
use Symfony\Component\Lock\Key;
1114

1215
final class RoadRunnerStoreTest extends TestCase
1316
{
14-
public function testSave(): void
17+
public function testSaveSuccess(): void
1518
{
1619
$rrLock = $this->createMock(RrLock::class);
1720
$rrLock->expects(self::once())
1821
->method('lock')
19-
->with('resource-name', '*');
20-
$store = new RoadRunnerStore($rrLock);
22+
->with('resource-name', null)
23+
->willReturn('lock-id');
24+
$store = new RoadRunnerStore($rrLock, 'pid');
2125
$store->save(new Key('resource-name'));
2226
}
2327

24-
public function testExists(): void
28+
public function testSaveReadSuccess(): void
29+
{
30+
$rrLock = $this->createMock(RrLock::class);
31+
$rrLock->expects(self::once())
32+
->method('lockRead')
33+
->with('resource-name', null)
34+
->willReturn('lock-id');
35+
$store = new RoadRunnerStore($rrLock, 'pid');
36+
$store->saveRead(new Key('resource-name'));
37+
}
38+
39+
public function testExistsSuccess(): void
2540
{
2641
$rrLock = $this->createMock(RrLock::class);
2742
$rrLock->expects(self::once())
2843
->method('exists')
29-
->with('resource-name', '*');
30-
$store = new RoadRunnerStore($rrLock);
44+
->with('resource-name', 'pid')
45+
->willReturn(true);
46+
$store = new RoadRunnerStore($rrLock, 'pid');
3147
$store->exists(new Key('resource-name'));
3248
}
3349

34-
public function testPutOffExpiration(): void
50+
public function testPutOffExpirationSuccess(): void
3551
{
3652
$rrLock = $this->createMock(RrLock::class);
3753
$rrLock->expects(self::once())
3854
->method('updateTTL')
39-
->with('resource-name', '*', 3600.0);
40-
$store = new RoadRunnerStore($rrLock);
55+
->with('resource-name', 'pid', 3600.0)
56+
->willReturn(true);
57+
$store = new RoadRunnerStore($rrLock, 'pid');
4158
$store->putOffExpiration(new Key('resource-name'), 3600.0);
4259
}
4360

44-
public function testDelete(): void
61+
public function testDeleteSuccess(): void
4562
{
4663
$rrLock = $this->createMock(RrLock::class);
4764
$rrLock->expects(self::once())
4865
->method('release')
49-
->with('resource-name', '*');
50-
$store = new RoadRunnerStore($rrLock);
66+
->with('resource-name', 'pid')
67+
->willReturn(true);
68+
$store = new RoadRunnerStore($rrLock, 'pid');
5169
$store->delete(new Key('resource-name'));
5270
}
5371

54-
public function testSaveRead(): void
72+
public function testSaveFail(): void
73+
{
74+
self::expectException(LockConflictedException::class);
75+
self::expectExceptionMessage('RoadRunner. Failed to make lock');
76+
77+
$rrLock = $this->createMock(RrLock::class);
78+
$rrLock->expects(self::once())
79+
->method('lock')
80+
->with('resource-name', null)
81+
->willReturn(false);
82+
$store = new RoadRunnerStore($rrLock, 'pid');
83+
$store->save(new Key('resource-name'));
84+
}
85+
86+
public function testSaveReadFail(): void
5587
{
88+
self::expectException(LockConflictedException::class);
89+
self::expectExceptionMessage('RoadRunner. Failed to make read lock');
90+
5691
$rrLock = $this->createMock(RrLock::class);
5792
$rrLock->expects(self::once())
5893
->method('lockRead')
59-
->with('resource-name', '*');
60-
$store = new RoadRunnerStore($rrLock);
94+
->with('resource-name', null);
95+
$store = new RoadRunnerStore($rrLock, 'pid');
6196
$store->saveRead(new Key('resource-name'));
6297
}
98+
99+
public function testExistsFail(): void
100+
{
101+
$rrLock = $this->createMock(RrLock::class);
102+
$rrLock->expects(self::once())
103+
->method('exists')
104+
->with('resource-name', 'pid')
105+
->willReturn(false);
106+
$store = new RoadRunnerStore($rrLock, 'pid');
107+
self::assertFalse(
108+
$store->exists(new Key('resource-name'))
109+
);
110+
}
111+
112+
public function testPutOffExpirationFail(): void
113+
{
114+
self::expectException(LockConflictedException::class);
115+
self::expectExceptionMessage('RoadRunner. Failed to update lock ttl');
116+
117+
$rrLock = $this->createMock(RrLock::class);
118+
$rrLock->expects(self::once())
119+
->method('updateTTL')
120+
->with('resource-name', 'pid', 3600.0)
121+
->willReturn(false);
122+
$store = new RoadRunnerStore($rrLock, 'pid');
123+
$store->putOffExpiration(new Key('resource-name'), 3600.0);
124+
}
125+
126+
public function testDeleteFail(): void
127+
{
128+
self::expectException(LockReleasingException::class);
129+
self::expectExceptionMessage('RoadRunner. Failed to release lock');
130+
131+
$rrLock = $this->createMock(RrLock::class);
132+
$rrLock->expects(self::once())
133+
->method('release')
134+
->with('resource-name', 'pid')
135+
->willReturn(false);
136+
$store = new RoadRunnerStore($rrLock, 'pid');
137+
$store->delete(new Key('resource-name'));
138+
}
63139
}

0 commit comments

Comments
 (0)