-
-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathWithoutOverlappingMiddlewareTest.php
More file actions
114 lines (93 loc) · 4.42 KB
/
WithoutOverlappingMiddlewareTest.php
File metadata and controls
114 lines (93 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
declare(strict_types=1);
namespace Tests\Unit\Middleware;
use Illuminate\Support\Facades\Cache;
use Mockery\MockInterface;
use Tests\Fixtures\TestActivity;
use Tests\Fixtures\TestWorkflow;
use Tests\TestCase;
use Workflow\Middleware\WithoutOverlappingMiddleware;
final class WithoutOverlappingMiddlewareTest extends TestCase
{
public function testMiddleware(): void
{
$this->app->make('cache')
->store()
->clear();
$middleware = new WithoutOverlappingMiddleware(1, WithoutOverlappingMiddleware::WORKFLOW);
$this->assertSame($middleware->getLockKey(), 'laravel-workflow-overlap:1');
$this->assertSame($middleware->getWorkflowSemaphoreKey(), 'laravel-workflow-overlap:1:workflow');
$this->assertSame($middleware->getActivitySemaphoreKey(), 'laravel-workflow-overlap:1:activity');
$middleware = new WithoutOverlappingMiddleware(1, WithoutOverlappingMiddleware::ACTIVITY);
$this->assertSame($middleware->getLockKey(), 'laravel-workflow-overlap:1');
$this->assertSame($middleware->getWorkflowSemaphoreKey(), 'laravel-workflow-overlap:1:workflow');
$this->assertSame($middleware->getActivitySemaphoreKey(), 'laravel-workflow-overlap:1:activity');
}
public function testAllowsOnlyOneWorkflowInstance(): void
{
$this->app->make('cache')
->store()
->clear();
$workflow1 = $this->mock(TestWorkflow::class);
$middleware1 = new WithoutOverlappingMiddleware(1, WithoutOverlappingMiddleware::WORKFLOW);
$workflow2 = $this->mock(TestWorkflow::class, static function (MockInterface $mock) {
$mock->shouldReceive('release')
->once();
});
$middleware2 = new WithoutOverlappingMiddleware(1, WithoutOverlappingMiddleware::WORKFLOW);
$activity = $this->mock(TestActivity::class, static function (MockInterface $mock) {
$mock->shouldReceive('release')
->once();
});
$middleware3 = new WithoutOverlappingMiddleware(1, WithoutOverlappingMiddleware::ACTIVITY);
$middleware1->handle($workflow1, function ($job) use (
$middleware1,
$workflow2,
$middleware2,
$activity,
$middleware3
) {
$this->assertSame(1, Cache::get($middleware1->getWorkflowSemaphoreKey()));
$this->assertNull(Cache::get($middleware1->getActivitySemaphoreKey()));
$middleware2->handle($workflow2, static function ($job) {
});
$middleware3->handle($activity, static function ($job) {
});
});
$this->assertSame(0, Cache::get($middleware1->getWorkflowSemaphoreKey()));
$this->assertNull(Cache::get($middleware1->getActivitySemaphoreKey()));
}
public function testAllowsMultipleActivityInstances(): void
{
$this->app->make('cache')
->store()
->clear();
$activity1 = $this->mock(TestActivity::class);
$middleware1 = new WithoutOverlappingMiddleware(1, WithoutOverlappingMiddleware::ACTIVITY);
$activity2 = $this->mock(TestActivity::class);
$middleware2 = new WithoutOverlappingMiddleware(1, WithoutOverlappingMiddleware::ACTIVITY);
$workflow1 = $this->mock(TestWorkflow::class, static function (MockInterface $mock) {
$mock->shouldReceive('release')
->once();
});
$middleware3 = new WithoutOverlappingMiddleware(1, WithoutOverlappingMiddleware::WORKFLOW);
$middleware1->handle($activity1, function ($job) use (
$middleware1,
$activity2,
$middleware2,
$workflow1,
$middleware3
) {
$this->assertNull(Cache::get($middleware1->getWorkflowSemaphoreKey()));
$this->assertSame(1, count(Cache::get($middleware1->getActivitySemaphoreKey())));
$middleware2->handle($activity2, function ($job) use ($middleware2) {
$this->assertNull(Cache::get($middleware2->getWorkflowSemaphoreKey()));
$this->assertSame(2, count(Cache::get($middleware2->getActivitySemaphoreKey())));
});
$middleware3->handle($workflow1, static function ($job) {
});
});
$this->assertNull(Cache::get($middleware1->getWorkflowSemaphoreKey()));
$this->assertSame(0, count(Cache::get($middleware1->getActivitySemaphoreKey())));
}
}