|
7 | 7 | use PHPUnit\Framework\TestCase; |
8 | 8 | use RoadRunner\Lock\LockInterface as RrLock; |
9 | 9 | use Spiral\RoadRunner\Symfony\Lock\RoadRunnerStore; |
| 10 | +use Symfony\Component\Lock\Exception\LockConflictedException; |
| 11 | +use Symfony\Component\Lock\Exception\LockReleasingException; |
10 | 12 | use Symfony\Component\Lock\Key; |
11 | 13 |
|
12 | 14 | final class RoadRunnerStoreTest extends TestCase |
13 | 15 | { |
14 | | - public function testSave(): void |
| 16 | + public function testSaveSuccess(): void |
15 | 17 | { |
16 | 18 | $rrLock = $this->createMock(RrLock::class); |
17 | 19 | $rrLock->expects(self::once()) |
18 | 20 | ->method('lock') |
19 | | - ->with('resource-name', '*'); |
| 21 | + ->with('resource-name', null) |
| 22 | + ->willReturn('lock-id'); |
20 | 23 | $store = new RoadRunnerStore($rrLock); |
21 | | - $store->save(new Key('resource-name')); |
| 24 | + $key = new Key('resource-name'); |
| 25 | + $store->save($key); |
| 26 | + self::assertTrue($key->hasState(RoadRunnerStore::class)); |
| 27 | + self::assertSame('lock-id', $key->getState(RoadRunnerStore::class)); |
22 | 28 | } |
23 | 29 |
|
24 | | - public function testExists(): void |
| 30 | + public function testSaveReadSuccess(): void |
| 31 | + { |
| 32 | + $rrLock = $this->createMock(RrLock::class); |
| 33 | + $rrLock->expects(self::once()) |
| 34 | + ->method('lockRead') |
| 35 | + ->with('resource-name', null) |
| 36 | + ->willReturn('lock-id'); |
| 37 | + $store = new RoadRunnerStore($rrLock); |
| 38 | + $key = new Key('resource-name'); |
| 39 | + $store->saveRead($key); |
| 40 | + } |
| 41 | + |
| 42 | + public function testExistsSuccess(): void |
25 | 43 | { |
26 | 44 | $rrLock = $this->createMock(RrLock::class); |
27 | 45 | $rrLock->expects(self::once()) |
28 | 46 | ->method('exists') |
29 | | - ->with('resource-name', '*'); |
| 47 | + ->with('resource-name') |
| 48 | + ->willReturn(true); |
30 | 49 | $store = new RoadRunnerStore($rrLock); |
31 | | - $store->exists(new Key('resource-name')); |
| 50 | + $key = new Key('resource-name'); |
| 51 | + $key->setState(RoadRunnerStore::class, 'lock-id'); |
| 52 | + $store->exists($key); |
32 | 53 | } |
33 | 54 |
|
34 | | - public function testPutOffExpiration(): void |
| 55 | + public function testPutOffExpirationSuccess(): void |
35 | 56 | { |
36 | 57 | $rrLock = $this->createMock(RrLock::class); |
37 | 58 | $rrLock->expects(self::once()) |
38 | 59 | ->method('updateTTL') |
39 | | - ->with('resource-name', '*', 3600.0); |
| 60 | + ->with('resource-name', 'lock-id', 3600.0) |
| 61 | + ->willReturn(true); |
40 | 62 | $store = new RoadRunnerStore($rrLock); |
41 | | - $store->putOffExpiration(new Key('resource-name'), 3600.0); |
| 63 | + $key = new Key('resource-name'); |
| 64 | + $key->setState(RoadRunnerStore::class, 'lock-id'); |
| 65 | + $store->putOffExpiration($key, 3600.0); |
42 | 66 | } |
43 | 67 |
|
44 | | - public function testDelete(): void |
| 68 | + public function testDeleteSuccess(): void |
45 | 69 | { |
46 | 70 | $rrLock = $this->createMock(RrLock::class); |
47 | 71 | $rrLock->expects(self::once()) |
48 | 72 | ->method('release') |
49 | | - ->with('resource-name', '*'); |
| 73 | + ->with('resource-name') |
| 74 | + ->willReturn(true); |
50 | 75 | $store = new RoadRunnerStore($rrLock); |
51 | | - $store->delete(new Key('resource-name')); |
| 76 | + $key = new Key('resource-name'); |
| 77 | + $key->setState(RoadRunnerStore::class, 'lock-id'); |
| 78 | + $store->delete($key); |
52 | 79 | } |
53 | 80 |
|
54 | | - public function testSaveRead(): void |
| 81 | + public function testSaveFail(): void |
55 | 82 | { |
| 83 | + self::expectException(LockConflictedException::class); |
| 84 | + self::expectExceptionMessage('RoadRunner. Failed to make lock'); |
| 85 | + |
| 86 | + $rrLock = $this->createMock(RrLock::class); |
| 87 | + $rrLock->expects(self::once()) |
| 88 | + ->method('lock') |
| 89 | + ->with('resource-name', null) |
| 90 | + ->willReturn(false); |
| 91 | + $store = new RoadRunnerStore($rrLock); |
| 92 | + $store->save(new Key('resource-name')); |
| 93 | + } |
| 94 | + |
| 95 | + public function testSaveReadFail(): void |
| 96 | + { |
| 97 | + self::expectException(LockConflictedException::class); |
| 98 | + self::expectExceptionMessage('RoadRunner. Failed to make read lock'); |
| 99 | + |
56 | 100 | $rrLock = $this->createMock(RrLock::class); |
57 | 101 | $rrLock->expects(self::once()) |
58 | 102 | ->method('lockRead') |
59 | | - ->with('resource-name', '*'); |
| 103 | + ->with('resource-name', null); |
| 104 | + $store = new RoadRunnerStore($rrLock); |
| 105 | + $key = new Key('resource-name'); |
| 106 | + $store->saveRead($key); |
| 107 | + } |
| 108 | + |
| 109 | + public function testExistsFail(): void |
| 110 | + { |
| 111 | + $rrLock = $this->createMock(RrLock::class); |
| 112 | + $rrLock->expects(self::once()) |
| 113 | + ->method('exists') |
| 114 | + ->with('resource-name') |
| 115 | + ->willReturn(false); |
| 116 | + $store = new RoadRunnerStore($rrLock); |
| 117 | + $key = new Key('resource-name'); |
| 118 | + $key->setState(RoadRunnerStore::class, 'lock-id'); |
| 119 | + self::assertFalse($store->exists($key)); |
| 120 | + } |
| 121 | + |
| 122 | + public function testPutOffExpirationFail(): void |
| 123 | + { |
| 124 | + self::expectException(LockConflictedException::class); |
| 125 | + self::expectExceptionMessage('RoadRunner. Failed to update lock ttl'); |
| 126 | + |
| 127 | + $rrLock = $this->createMock(RrLock::class); |
| 128 | + $rrLock->expects(self::once()) |
| 129 | + ->method('updateTTL') |
| 130 | + ->with('resource-name', 'lock-id', 3600.0) |
| 131 | + ->willReturn(false); |
| 132 | + $store = new RoadRunnerStore($rrLock); |
| 133 | + $key = new Key('resource-name'); |
| 134 | + $key->setState(RoadRunnerStore::class, 'lock-id'); |
| 135 | + $store->putOffExpiration($key, 3600.0); |
| 136 | + } |
| 137 | + |
| 138 | + public function testDeleteFail(): void |
| 139 | + { |
| 140 | + self::expectException(LockReleasingException::class); |
| 141 | + self::expectExceptionMessage('RoadRunner. Failed to release lock'); |
| 142 | + |
| 143 | + $rrLock = $this->createMock(RrLock::class); |
| 144 | + $rrLock->expects(self::once()) |
| 145 | + ->method('release') |
| 146 | + ->with('resource-name') |
| 147 | + ->willReturn(false); |
60 | 148 | $store = new RoadRunnerStore($rrLock); |
61 | | - $store->saveRead(new Key('resource-name')); |
| 149 | + $key = new Key('resource-name'); |
| 150 | + $key->setState(RoadRunnerStore::class, 'lock-id'); |
| 151 | + $store->delete($key); |
62 | 152 | } |
63 | 153 | } |
0 commit comments