PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.10.29
ووسلام – همگام سازی ووکامرس و باسلام v1.10.29
1.10.29 1.10.28 1.1.26 1.10.27 1.10.24 1.10.25 1.10.23 1.10.22 1.10.21 1.10.19 1.10.20 1.10.18 1.10.17 1.10.15 1.10.14 1.10.13 1.10.12 1.10.10 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 All 62 releases
sync-basalam / tests / Services / AsyncWorkerHealthTest.php

AsyncWorkerHealthTest.php in ووسلام – همگام سازی ووکامرس و باسلام 1.10.29, at tests/Services/AsyncWorkerHealthTest.php

120 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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