| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Tests; |
| 4 |
|
| 5 |
use PHPUnit\Framework\TestCase; |
| 6 |
use SyncBasalam\Services\AsyncWorkerHealth; |
| 7 |
|
| 8 |
class AsyncWorkerHealthTest extends TestCase |
| 9 |
{ |
| 10 |
private $options; |
| 11 |
private $server; |
| 12 |
private $environment; |
| 13 |
|
| 14 |
protected function setUp(): void |
| 15 |
{ |
| 16 |
$this->options = $GLOBALS['sync_basalam_test_options'] ?? []; |
| 17 |
$this->server = $_SERVER; |
| 18 |
$this->environment = [getenv('noabort'), getenv('REDIRECT_noabort')]; |
| 19 |
$GLOBALS['sync_basalam_test_options'] = []; |
| 20 |
unset($_SERVER['noabort'], $_SERVER['REDIRECT_noabort']); |
| 21 |
putenv('noabort'); |
| 22 |
putenv('REDIRECT_noabort'); |
| 23 |
} |
| 24 |
|
| 25 |
protected function tearDown(): void |
| 26 |
{ |
| 27 |
$GLOBALS['sync_basalam_test_options'] = $this->options; |
| 28 |
$_SERVER = $this->server; |
| 29 |
foreach (['noabort', 'REDIRECT_noabort'] as $index => $name) { |
| 30 |
$value = $this->environment[$index]; |
| 31 |
putenv($value === false ? $name : $name . '=' . $value); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
public function testOtherPhpRuntimesDoNotGetALiteSpeedWarning(): void |
| 36 |
{ |
| 37 |
$health = new RuntimeWorkerHealth(false); |
| 38 |
$health->observeAsyncEnvironment(); |
| 39 |
self::assertNull($health->getNotice()); |
| 40 |
} |
| 41 |
|
| 42 |
public function testMissingProtectionPersistsAcrossRequestsAndFastReturns(): void |
| 43 |
{ |
| 44 |
$health = new RuntimeWorkerHealth(true); |
| 45 |
$health->observeAsyncEnvironment(); |
| 46 |
$health->recordJobReturned((object) ['id' => 10], 0.5); |
| 47 |
$notice = (new RuntimeWorkerHealth(true))->getNotice(); |
| 48 |
self::assertSame('notice-warning', $notice['class']); |
| 49 |
self::assertStringContainsString('LiteSpeed Cache', $notice['guidance']); |
| 50 |
} |
| 51 |
|
| 52 |
/** @dataProvider protectionSources */ |
| 53 |
public function testEffectiveProtectionIsRecognized(string $source, string $name): void |
| 54 |
{ |
| 55 |
if ($source === 'server') $_SERVER[$name] = '1'; |
| 56 |
else putenv($name . '=1'); |
| 57 |
$health = new RuntimeWorkerHealth(true); |
| 58 |
$health->observeAsyncEnvironment(); |
| 59 |
self::assertNull($health->getNotice()); |
| 60 |
} |
| 61 |
|
| 62 |
public function protectionSources(): array |
| 63 |
{ |
| 64 |
return [['server', 'noabort'], ['server', 'REDIRECT_noabort'], ['env', 'noabort'], ['env', 'REDIRECT_noabort']]; |
| 65 |
} |
| 66 |
|
| 67 |
public function testZeroDoesNotCountAsProtection(): void |
| 68 |
{ |
| 69 |
$_SERVER['noabort'] = '0'; |
| 70 |
$health = new RuntimeWorkerHealth(true); |
| 71 |
$health->observeAsyncEnvironment(); |
| 72 |
self::assertNotNull($health->getNotice()); |
| 73 |
} |
| 74 |
|
| 75 |
public function testStaleInterruptionPersistsUntilAProtectedJobActuallyReturns(): void |
| 76 |
{ |
| 77 |
$health = new RuntimeWorkerHealth(true); |
| 78 |
$health->observeAsyncEnvironment(); |
| 79 |
$job = (object) ['id' => 23, 'started_at' => time() - 121, 'payload' => 'private']; |
| 80 |
$health->recordInterruptedJob($job); |
| 81 |
self::assertSame('notice-error', $health->getNotice()['class']); |
| 82 |
$health->recordJobReturned($job, 0.1); |
| 83 |
self::assertSame('notice-error', $health->getNotice()['class']); |
| 84 |
|
| 85 |
$_SERVER['noabort'] = '1'; |
| 86 |
$nextRequest = new RuntimeWorkerHealth(true); |
| 87 |
$nextRequest->observeAsyncEnvironment(); |
| 88 |
self::assertNotNull($nextRequest->getNotice(), 'Empty or contended batches must not clear interruption evidence.'); |
| 89 |
$nextRequest->recordJobReturned($job, 7.5); |
| 90 |
self::assertNull($nextRequest->getNotice()); |
| 91 |
$state = get_option(AsyncWorkerHealth::OPTION_KEY); |
| 92 |
self::assertSame(23, $state['last_returned_job_id']); |
| 93 |
self::assertArrayNotHasKey('payload', $state); |
| 94 |
} |
| 95 |
|
| 96 |
public function testNonLiteSpeedInterruptionGivesGenericHostingAdvice(): void |
| 97 |
{ |
| 98 |
$health = new RuntimeWorkerHealth(false); |
| 99 |
$health->recordInterruptedJob((object) ['id' => 1, 'started_at' => time() - 121]); |
| 100 |
self::assertStringNotContainsString('noabort', $health->getNotice()['guidance']); |
| 101 |
$health->observeAsyncEnvironment(); |
| 102 |
$health->recordJobReturned((object) ['id' => 2], 1.0); |
| 103 |
self::assertNull($health->getNotice()); |
| 104 |
} |
| 105 |
|
| 106 |
public function testCorruptOptionDoesNotProduceAnAlert(): void |
| 107 |
{ |
| 108 |
update_option(AsyncWorkerHealth::OPTION_KEY, 'invalid'); |
| 109 |
self::assertNull((new RuntimeWorkerHealth(false))->getNotice()); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
class RuntimeWorkerHealth extends AsyncWorkerHealth |
| 114 |
{ |
| 115 |
private bool $liteSpeed; |
| 116 |
|
| 117 |
public function __construct(bool $liteSpeed) { $this->liteSpeed = $liteSpeed; } |
| 118 |
protected function isLiteSpeed(): bool { return $this->liteSpeed; } |
| 119 |
} |
| 120 |
|