|
4 | 4 |
|
5 | 5 | namespace Tests\Unit\Middleware; |
6 | 6 |
|
| 7 | +use Illuminate\Contracts\Cache\Lock; |
| 8 | +use Illuminate\Contracts\Cache\Repository; |
7 | 9 | use Illuminate\Support\Facades\Cache; |
8 | 10 | use Mockery\MockInterface; |
9 | 11 | use Tests\Fixtures\TestActivity; |
@@ -111,4 +113,60 @@ public function testAllowsMultipleActivityInstances(): void |
111 | 113 | $this->assertNull(Cache::get($middleware1->getWorkflowSemaphoreKey())); |
112 | 114 | $this->assertSame(0, count(Cache::get($middleware1->getActivitySemaphoreKey()))); |
113 | 115 | } |
| 116 | + |
| 117 | + public function testUnknownTypeDoesNotCallNext(): void |
| 118 | + { |
| 119 | + $this->app->make('cache') |
| 120 | + ->store() |
| 121 | + ->clear(); |
| 122 | + |
| 123 | + $job = $this->mock(TestActivity::class, static function (MockInterface $mock) { |
| 124 | + $mock->shouldReceive('release') |
| 125 | + ->once(); |
| 126 | + }); |
| 127 | + |
| 128 | + $middleware = new WithoutOverlappingMiddleware(1, 999); |
| 129 | + |
| 130 | + $middleware->handle($job, function ($job) { |
| 131 | + $this->fail('Should not call next when type is unknown'); |
| 132 | + }); |
| 133 | + |
| 134 | + $this->assertNull(Cache::get($middleware->getWorkflowSemaphoreKey())); |
| 135 | + $this->assertNull(Cache::get($middleware->getActivitySemaphoreKey())); |
| 136 | + } |
| 137 | + |
| 138 | + public function testReleaseWhenCompareAndSetFails(): void |
| 139 | + { |
| 140 | + $this->app->make('cache') |
| 141 | + ->store() |
| 142 | + ->clear(); |
| 143 | + |
| 144 | + $job = $this->mock(TestWorkflow::class, static function (MockInterface $mock) { |
| 145 | + $mock->shouldReceive('release') |
| 146 | + ->once(); |
| 147 | + }); |
| 148 | + |
| 149 | + $lock = $this->mock(Lock::class, static function (MockInterface $mock) { |
| 150 | + $mock->shouldReceive('get') |
| 151 | + ->once() |
| 152 | + ->andReturn(false); |
| 153 | + }); |
| 154 | + |
| 155 | + $cache = $this->mock(Repository::class, static function (MockInterface $mock) use ($lock) { |
| 156 | + $mock->shouldReceive('lock') |
| 157 | + ->once() |
| 158 | + ->andReturn($lock); |
| 159 | + $mock->shouldReceive('get') |
| 160 | + ->andReturn([]); |
| 161 | + }); |
| 162 | + |
| 163 | + $middleware = new WithoutOverlappingMiddleware(1, WithoutOverlappingMiddleware::WORKFLOW); |
| 164 | + |
| 165 | + $middleware->handle($job, function ($job) { |
| 166 | + $this->fail('Should not call next when lock is not acquired'); |
| 167 | + }); |
| 168 | + |
| 169 | + $this->assertNull(Cache::get($middleware->getWorkflowSemaphoreKey())); |
| 170 | + $this->assertNull(Cache::get($middleware->getActivitySemaphoreKey())); |
| 171 | + } |
114 | 172 | } |
0 commit comments