|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Tests\Console\Scheduling; |
| 4 | + |
| 5 | +use Illuminate\Console\OutputStyle; |
| 6 | +use Illuminate\Console\Scheduling\ScheduleWorkCommand; |
| 7 | +use Illuminate\Console\Signals; |
| 8 | +use Illuminate\Support\Carbon; |
| 9 | +use Illuminate\Tests\Console\Fixtures\FakeSignalsRegistry; |
| 10 | +use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; |
| 11 | +use Mockery as m; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | +use ReflectionProperty; |
| 14 | +use Symfony\Component\Console\Input\ArrayInput; |
| 15 | +use Symfony\Component\Console\Output\BufferedOutput; |
| 16 | +use Symfony\Component\Process\Process; |
| 17 | + |
| 18 | +class ScheduleWorkCommandTest extends TestCase |
| 19 | +{ |
| 20 | + use MockeryPHPUnitIntegration; |
| 21 | + |
| 22 | + /** |
| 23 | + * The signal availability resolver in place before the test ran. |
| 24 | + * |
| 25 | + * @var (callable(): bool)|null |
| 26 | + */ |
| 27 | + protected $originalAvailabilityResolver; |
| 28 | + |
| 29 | + protected function setUp(): void |
| 30 | + { |
| 31 | + parent::setUp(); |
| 32 | + |
| 33 | + $this->originalAvailabilityResolver = (new ReflectionProperty(Signals::class, 'availabilityResolver')) |
| 34 | + ->getValue(); |
| 35 | + |
| 36 | + Carbon::setTestNow(Carbon::create(2026, 6, 27, 12, 0, 30)); |
| 37 | + } |
| 38 | + |
| 39 | + protected function tearDown(): void |
| 40 | + { |
| 41 | + Carbon::setTestNow(); |
| 42 | + |
| 43 | + Signals::resolveAvailabilityUsing($this->originalAvailabilityResolver); |
| 44 | + |
| 45 | + parent::tearDown(); |
| 46 | + } |
| 47 | + |
| 48 | + public function test_stop_signal_marks_the_worker_to_quit() |
| 49 | + { |
| 50 | + if (! extension_loaded('pcntl')) { |
| 51 | + $this->markTestSkipped('The pcntl extension is required to trap signals.'); |
| 52 | + } |
| 53 | + |
| 54 | + // When a handler is registered, Signals chains any handler already bound |
| 55 | + // to the signal (see Signals::initializeSignal). Other tests may leave |
| 56 | + // real handlers in place, so reset these signals to their default |
| 57 | + // disposition to keep this test isolated from the global signal state. |
| 58 | + $previousHandlers = []; |
| 59 | + |
| 60 | + foreach ([SIGINT, SIGTERM, SIGQUIT] as $signal) { |
| 61 | + $previousHandlers[$signal] = pcntl_signal_get_handler($signal); |
| 62 | + |
| 63 | + pcntl_signal($signal, SIG_DFL); |
| 64 | + } |
| 65 | + |
| 66 | + try { |
| 67 | + Signals::resolveAvailabilityUsing(fn () => true); |
| 68 | + |
| 69 | + $registry = new FakeSignalsRegistry; |
| 70 | + |
| 71 | + $command = new ScheduleWorkCommandTestStub; |
| 72 | + |
| 73 | + // Wire the command up to a fake signal registry so we can simulate a |
| 74 | + // signal being delivered without sending a real one to the process. |
| 75 | + (fn () => $this->signals = new Signals($registry))->call($command); |
| 76 | + |
| 77 | + $command->callListenForSignals(); |
| 78 | + |
| 79 | + $this->assertFalse($command->shouldQuit(), 'The worker should not quit before a signal is received.'); |
| 80 | + |
| 81 | + $registry->handle(SIGTERM); |
| 82 | + |
| 83 | + $this->assertTrue($command->shouldQuit(), 'The worker should be marked to quit after a SIGTERM.'); |
| 84 | + } finally { |
| 85 | + foreach ($previousHandlers as $signal => $handler) { |
| 86 | + pcntl_signal($signal, $handler ?: SIG_DFL); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public function test_in_flight_executions_finish_before_the_worker_quits() |
| 92 | + { |
| 93 | + $execution = m::mock(Process::class); |
| 94 | + $execution->shouldReceive('getIncrementalOutput')->andReturn('scheduled task ran', ''); |
| 95 | + $execution->shouldReceive('getIncrementalErrorOutput')->andReturn(''); |
| 96 | + |
| 97 | + // The worker should poll the running execution after the signal arrives, |
| 98 | + // wait for it to report finished, and flush its output before quitting. |
| 99 | + $execution->shouldReceive('isRunning')->twice()->andReturn(true, false); |
| 100 | + |
| 101 | + $command = new ScheduleWorkCommandTestStub; |
| 102 | + $command->setOutput(new OutputStyle(new ArrayInput([]), $buffer = new BufferedOutput)); |
| 103 | + |
| 104 | + // Simulate the stop signal arriving while a "schedule:run" execution is |
| 105 | + // still in progress (after the worker's very first loop tick). |
| 106 | + $command->onTick = function (ScheduleWorkCommandTestStub $command) { |
| 107 | + if ($command->ticks === 1) { |
| 108 | + $command->markShouldQuit(); |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + $status = $command->work('schedule:run', [$execution]); |
| 113 | + |
| 114 | + $this->assertSame(ScheduleWorkCommand::SUCCESS, $status); |
| 115 | + $this->assertSame([], $command->remainingExecutions(), 'The execution should have been drained before quitting.'); |
| 116 | + $this->assertStringContainsString('scheduled task ran', $buffer->fetch()); |
| 117 | + } |
| 118 | + |
| 119 | + public function test_no_new_executions_are_started_after_a_stop_signal() |
| 120 | + { |
| 121 | + // A new run would normally be started on the zero second of the minute. |
| 122 | + Carbon::setTestNow(Carbon::create(2026, 6, 27, 12, 0, 0)); |
| 123 | + |
| 124 | + $command = new ScheduleWorkCommandTestStub; |
| 125 | + $command->markShouldQuit(); |
| 126 | + |
| 127 | + // With no executions in flight and a stop signal already received, the |
| 128 | + // worker must exit immediately without starting a new "schedule:run". |
| 129 | + // If the guard regressed, it would try to spawn a real process here. |
| 130 | + $status = $command->work('schedule:run'); |
| 131 | + |
| 132 | + $this->assertSame(ScheduleWorkCommand::SUCCESS, $status); |
| 133 | + $this->assertSame([], $command->remainingExecutions()); |
| 134 | + $this->assertSame(1, $command->ticks, 'The worker should exit after a single tick when idle and quitting.'); |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +class ScheduleWorkCommandTestStub extends ScheduleWorkCommand |
| 139 | +{ |
| 140 | + /** |
| 141 | + * The number of times the worker has ticked (slept). |
| 142 | + * |
| 143 | + * @var int |
| 144 | + */ |
| 145 | + public $ticks = 0; |
| 146 | + |
| 147 | + /** |
| 148 | + * A callback invoked on every worker tick, used to drive the loop in tests. |
| 149 | + * |
| 150 | + * @var (callable(self): void)|null |
| 151 | + */ |
| 152 | + public $onTick; |
| 153 | + |
| 154 | + public function callListenForSignals(): void |
| 155 | + { |
| 156 | + $this->listenForSignals(); |
| 157 | + } |
| 158 | + |
| 159 | + public function shouldQuit(): bool |
| 160 | + { |
| 161 | + return $this->shouldQuit; |
| 162 | + } |
| 163 | + |
| 164 | + public function markShouldQuit(): void |
| 165 | + { |
| 166 | + $this->shouldQuit = true; |
| 167 | + } |
| 168 | + |
| 169 | + public function work($command, array $executions = []) |
| 170 | + { |
| 171 | + $this->executions = $executions; |
| 172 | + |
| 173 | + return parent::work($command); |
| 174 | + } |
| 175 | + |
| 176 | + public function remainingExecutions(): array |
| 177 | + { |
| 178 | + return $this->executions; |
| 179 | + } |
| 180 | + |
| 181 | + protected function sleep() |
| 182 | + { |
| 183 | + $this->ticks++; |
| 184 | + |
| 185 | + if ($this->onTick) { |
| 186 | + ($this->onTick)($this); |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments