|
7 | 7 | use PHPUnit\Framework\TestCase; |
8 | 8 | use RoadRunner\Lock\LockInterface as RrLock; |
9 | 9 | use Spiral\RoadRunner\Symfony\Lock\RoadRunnerStore; |
| 10 | + |
| 11 | +use Symfony\Component\Lock\Exception\LockConflictedException; |
| 12 | +use Symfony\Component\Lock\Exception\LockReleasingException; |
10 | 13 | use Symfony\Component\Lock\Key; |
11 | 14 |
|
12 | 15 | final class RoadRunnerStoreTest extends TestCase |
13 | 16 | { |
14 | | - public function testSave(): void |
| 17 | + public function testSaveSuccess(): void |
15 | 18 | { |
16 | 19 | $rrLock = $this->createMock(RrLock::class); |
17 | 20 | $rrLock->expects(self::once()) |
18 | 21 | ->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'); |
21 | 25 | $store->save(new Key('resource-name')); |
22 | 26 | } |
23 | 27 |
|
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 |
25 | 40 | { |
26 | 41 | $rrLock = $this->createMock(RrLock::class); |
27 | 42 | $rrLock->expects(self::once()) |
28 | 43 | ->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'); |
31 | 47 | $store->exists(new Key('resource-name')); |
32 | 48 | } |
33 | 49 |
|
34 | | - public function testPutOffExpiration(): void |
| 50 | + public function testPutOffExpirationSuccess(): void |
35 | 51 | { |
36 | 52 | $rrLock = $this->createMock(RrLock::class); |
37 | 53 | $rrLock->expects(self::once()) |
38 | 54 | ->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'); |
41 | 58 | $store->putOffExpiration(new Key('resource-name'), 3600.0); |
42 | 59 | } |
43 | 60 |
|
44 | | - public function testDelete(): void |
| 61 | + public function testDeleteSuccess(): void |
45 | 62 | { |
46 | 63 | $rrLock = $this->createMock(RrLock::class); |
47 | 64 | $rrLock->expects(self::once()) |
48 | 65 | ->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'); |
51 | 69 | $store->delete(new Key('resource-name')); |
52 | 70 | } |
53 | 71 |
|
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 |
55 | 87 | { |
| 88 | + self::expectException(LockConflictedException::class); |
| 89 | + self::expectExceptionMessage('RoadRunner. Failed to make read lock'); |
| 90 | + |
56 | 91 | $rrLock = $this->createMock(RrLock::class); |
57 | 92 | $rrLock->expects(self::once()) |
58 | 93 | ->method('lockRead') |
59 | | - ->with('resource-name', '*'); |
60 | | - $store = new RoadRunnerStore($rrLock); |
| 94 | + ->with('resource-name', null); |
| 95 | + $store = new RoadRunnerStore($rrLock, 'pid'); |
61 | 96 | $store->saveRead(new Key('resource-name')); |
62 | 97 | } |
| 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 | + } |
63 | 139 | } |
0 commit comments