|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zenstruck\ScheduleBundle\Tests\Schedule\Extension; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
|
|
|
|
|
6
|
|
|
use Psr\Log\Test\TestLogger; |
|
7
|
|
|
use Symfony\Component\Lock\LockFactory; |
|
8
|
|
|
use Symfony\Component\Lock\Store\FlockStore; |
|
9
|
|
|
use Zenstruck\ScheduleBundle\Event\BeforeScheduleEvent; |
|
10
|
|
|
use Zenstruck\ScheduleBundle\Event\BeforeTaskEvent; |
|
11
|
|
|
use Zenstruck\ScheduleBundle\Schedule; |
|
12
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Exception\SkipSchedule; |
|
13
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Exception\SkipTask; |
|
14
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Extension\Handler\SingleServerHandler; |
|
15
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Extension\SingleServerExtension; |
|
16
|
|
|
use Zenstruck\ScheduleBundle\Tests\Fixture\MockScheduleBuilder; |
|
17
|
|
|
use Zenstruck\ScheduleBundle\Tests\Fixture\MockTask; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @author Kevin Bond <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
final class SingleServerExtensionTest extends TestCase |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @test |
|
26
|
|
|
*/ |
|
27
|
|
|
public function can_lock_schedule() |
|
28
|
|
|
{ |
|
29
|
|
|
$logger = new TestLogger(); |
|
30
|
|
|
$lockFactory = new LockFactory(new FlockStore()); |
|
31
|
|
|
$lockFactory->setLogger($logger); |
|
32
|
|
|
|
|
33
|
|
|
(new MockScheduleBuilder()) |
|
34
|
|
|
->addHandler(new SingleServerHandler($lockFactory)) |
|
35
|
|
|
->addExtension(new SingleServerExtension()) |
|
36
|
|
|
->run() |
|
37
|
|
|
; |
|
38
|
|
|
|
|
39
|
|
|
$this->assertTrue($logger->hasInfoThatContains('Successfully acquired')); |
|
40
|
|
|
$this->assertTrue($logger->hasInfoThatContains('Expiration defined')); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @test |
|
45
|
|
|
*/ |
|
46
|
|
|
public function can_lock_task() |
|
47
|
|
|
{ |
|
48
|
|
|
$logger = new TestLogger(); |
|
49
|
|
|
$lockFactory = new LockFactory(new FlockStore()); |
|
50
|
|
|
$lockFactory->setLogger($logger); |
|
51
|
|
|
|
|
52
|
|
|
(new MockScheduleBuilder()) |
|
53
|
|
|
->addHandler(new SingleServerHandler($lockFactory)) |
|
54
|
|
|
->addTask((new MockTask())->onSingleServer()) |
|
55
|
|
|
->run() |
|
56
|
|
|
; |
|
57
|
|
|
|
|
58
|
|
|
$this->assertTrue($logger->hasInfoThatContains('Successfully acquired')); |
|
59
|
|
|
$this->assertTrue($logger->hasInfoThatContains('Expiration defined')); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @test |
|
64
|
|
|
*/ |
|
65
|
|
|
public function provides_helpful_message_if_handler_not_configured() |
|
66
|
|
|
{ |
|
67
|
|
|
$this->expectException(\LogicException::class); |
|
68
|
|
|
$this->expectExceptionMessage('To use "onSingleServer" you must configure a lock factory (config path: "zenstruck_schedule.single_server_handler")'); |
|
69
|
|
|
|
|
70
|
|
|
(new MockScheduleBuilder()) |
|
71
|
|
|
->addExtension(new SingleServerExtension()) |
|
72
|
|
|
->run() |
|
73
|
|
|
; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @test |
|
78
|
|
|
*/ |
|
79
|
|
|
public function skips_schedule_if_locked() |
|
80
|
|
|
{ |
|
81
|
|
|
$schedule1 = new Schedule(); |
|
82
|
|
|
$schedule1->addCommand('my:command'); |
|
83
|
|
|
$schedule1->onSingleServer(); |
|
84
|
|
|
$schedule2 = new Schedule(); |
|
85
|
|
|
$schedule2->addCommand('my:command'); |
|
86
|
|
|
$schedule2->onSingleServer(); |
|
87
|
|
|
|
|
88
|
|
|
$handler = new SingleServerHandler(new LockFactory(new FlockStore())); |
|
89
|
|
|
$event1 = new BeforeScheduleEvent($schedule1); |
|
90
|
|
|
$event2 = new BeforeScheduleEvent($schedule2); |
|
91
|
|
|
|
|
92
|
|
|
$handler->filterSchedule($event1, $schedule1->getExtensions()[0]); |
|
93
|
|
|
|
|
94
|
|
|
$this->expectException(SkipSchedule::class); |
|
95
|
|
|
$this->expectExceptionMessage('Schedule running on another server.'); |
|
96
|
|
|
|
|
97
|
|
|
$handler->filterSchedule($event2, $schedule2->getExtensions()[0]); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @test |
|
102
|
|
|
*/ |
|
103
|
|
|
public function skips_task_if_locked() |
|
104
|
|
|
{ |
|
105
|
|
|
$schedule1 = new Schedule(); |
|
106
|
|
|
$task1 = $schedule1->addCommand('my:command')->onSingleServer(); |
|
107
|
|
|
$schedule2 = new Schedule(); |
|
108
|
|
|
$task2 = $schedule2->addCommand('my:command')->onSingleServer(); |
|
109
|
|
|
|
|
110
|
|
|
$event1 = new BeforeTaskEvent(new BeforeScheduleEvent($schedule1), $task1); |
|
111
|
|
|
$event2 = new BeforeTaskEvent(new BeforeScheduleEvent($schedule2), $task2); |
|
112
|
|
|
|
|
113
|
|
|
$handler = new SingleServerHandler(new LockFactory(new FlockStore())); |
|
114
|
|
|
|
|
115
|
|
|
$handler->filterTask($event1, $task1->getExtensions()[0]); |
|
116
|
|
|
|
|
117
|
|
|
$this->expectException(SkipTask::class); |
|
118
|
|
|
$this->expectExceptionMessage('Task running on another server.'); |
|
119
|
|
|
|
|
120
|
|
|
$handler->filterTask($event2, $task2->getExtensions()[0]); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths