|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Yiisoft\Queue\Tests\Unit\Cli; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 9 | +use PHPUnit\Framework\Attributes\RequiresPhpExtension; |
| 10 | +use Yiisoft\Queue\Cli\SignalLoop; |
| 11 | + |
| 12 | +#[RequiresPhpExtension('pcntl')] |
| 13 | +final class SignalLoopTest extends TestCase |
| 14 | +{ |
| 15 | + public function setUp(): void |
| 16 | + { |
| 17 | + parent::setUp(); |
| 18 | + pcntl_async_signals(true); |
| 19 | + } |
| 20 | + |
| 21 | + public function testMemoryLimitReached(): void |
| 22 | + { |
| 23 | + $loop = new SignalLoop(1); |
| 24 | + self::assertFalse($loop->canContinue()); |
| 25 | + } |
| 26 | + |
| 27 | + public function testSuspendAndResume(): void |
| 28 | + { |
| 29 | + $loop = new SignalLoop(0); |
| 30 | + pcntl_signal(\SIGALRM, static function (): void { |
| 31 | + posix_kill(getmypid(), \SIGCONT); |
| 32 | + }); |
| 33 | + |
| 34 | + posix_kill(getmypid(), \SIGTSTP); |
| 35 | + pcntl_alarm(1); |
| 36 | + |
| 37 | + $start = microtime(true); |
| 38 | + $result = $loop->canContinue(); |
| 39 | + $elapsed = microtime(true) - $start; |
| 40 | + |
| 41 | + self::assertTrue($result); |
| 42 | + self::assertGreaterThan(0.5, $elapsed); |
| 43 | + } |
| 44 | + |
| 45 | + #[DataProvider('exitSignalProvider')] |
| 46 | + public function testExitSignals(int $signal): void |
| 47 | + { |
| 48 | + $loop = new SignalLoop(0); |
| 49 | + |
| 50 | + self::assertTrue($loop->canContinue(), 'Loop should continue'); |
| 51 | + posix_kill(getmypid(), $signal); |
| 52 | + |
| 53 | + self::assertFalse($loop->canContinue(), "Loop should not continue after receiving signal {$signal}"); |
| 54 | + } |
| 55 | + |
| 56 | + public static function exitSignalProvider(): iterable |
| 57 | + { |
| 58 | + yield 'SIGHUP' => [\SIGHUP]; |
| 59 | + yield 'SIGINT' => [\SIGINT]; |
| 60 | + yield 'SIGTERM' => [\SIGTERM]; |
| 61 | + } |
| 62 | + |
| 63 | + public function testResumeSignal(): void |
| 64 | + { |
| 65 | + $loop = new SignalLoop(0); |
| 66 | + |
| 67 | + // First suspend the loop |
| 68 | + posix_kill(getmypid(), \SIGTSTP); |
| 69 | + |
| 70 | + // Then immediately resume |
| 71 | + posix_kill(getmypid(), \SIGCONT); |
| 72 | + |
| 73 | + $start = microtime(true); |
| 74 | + $result = $loop->canContinue(); |
| 75 | + $elapsed = microtime(true) - $start; |
| 76 | + |
| 77 | + self::assertTrue($result); |
| 78 | + self::assertLessThan(0.1, $elapsed, 'Loop should resume quickly without waiting'); |
| 79 | + } |
| 80 | + |
| 81 | + public function testMultipleExitSignals(): void |
| 82 | + { |
| 83 | + $loop = new SignalLoop(0); |
| 84 | + |
| 85 | + // Send multiple exit signals |
| 86 | + posix_kill(getmypid(), \SIGINT); |
| 87 | + posix_kill(getmypid(), \SIGTERM); |
| 88 | + |
| 89 | + $result = $loop->canContinue(); |
| 90 | + |
| 91 | + self::assertFalse($result, 'Loop should not continue after receiving any exit signal'); |
| 92 | + } |
| 93 | + |
| 94 | + public function testSuspendOverridesResume(): void |
| 95 | + { |
| 96 | + $loop = new SignalLoop(0); |
| 97 | + |
| 98 | + // Resume first |
| 99 | + posix_kill(getmypid(), \SIGCONT); |
| 100 | + // Then suspend |
| 101 | + posix_kill(getmypid(), \SIGTSTP); |
| 102 | + |
| 103 | + // Set up alarm to resume after 1 second |
| 104 | + pcntl_signal(\SIGALRM, static function (): void { |
| 105 | + posix_kill(getmypid(), \SIGCONT); |
| 106 | + }); |
| 107 | + pcntl_alarm(1); |
| 108 | + |
| 109 | + $start = microtime(true); |
| 110 | + $result = $loop->canContinue(); |
| 111 | + $elapsed = microtime(true) - $start; |
| 112 | + |
| 113 | + self::assertTrue($result); |
| 114 | + self::assertGreaterThan(0.5, $elapsed, 'Loop should wait for resume after suspend'); |
| 115 | + } |
| 116 | +} |
0 commit comments