You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/CloudEngineReadinessTest.php

82 lines
3.2 KiB
PHTML

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudEngineReadiness;
$assert = static function ($ok, string $message): void {
if (!$ok) {
throw new RuntimeException($message);
}
};
$now = 10.0;
$attempts = 0;
$sleeps = [];
$ready = CloudEngineReadiness::wait(
static function () use (&$attempts): bool {
$attempts++;
return $attempts === 3;
},
3.0,
static function () use (&$now): float {
return $now;
},
static function (int $microseconds) use (&$now, &$sleeps): void {
$sleeps[] = $microseconds;
$now += $microseconds / 1000000;
},
);
$assert($ready && $attempts === 3 && $sleeps === [1000000, 1000000],
'Readiness did not retry a transient startup race with bounded backoff.');
$now = 20.0;
$attempts = 0;
$sleeps = [];
$ready = CloudEngineReadiness::wait(
static function () use (&$attempts): bool {
$attempts++;
throw new RuntimeException('SECRET transport detail');
},
1.5,
static function () use (&$now): float {
return $now;
},
static function (int $microseconds) use (&$now, &$sleeps): void {
$sleeps[] = $microseconds;
$now += $microseconds / 1000000;
},
);
$assert(!$ready && $attempts === 3 && $sleeps === [1000000, 500000],
'Readiness did not stop at its monotonic deadline after probe failures.');
$root = dirname(__DIR__);
$readiness = file_get_contents($root . '/app/Services/Cloud/CloudEngineReadiness.php');
$lifecycle = file_get_contents($root . '/app/Command/CloudLifecycle.php');
$lifecycleService = file_get_contents($root . '/app/Services/Cloud/CloudLifecycleService.php');
$worker = file_get_contents($root . '/app/Command/CloudWorker.php');
$heartbeat = file_get_contents($root . '/app/Services/Cloud/CloudRuntimeHeartbeat.php');
$assert(str_contains($readiness, "new CloudEngineClient(null, 2.0)")
&& str_contains($readiness, "'GET', '/internal/v1/health'")
&& str_contains($readiness, "['accepting_mutations'] ?? null")
&& str_contains($readiness, "'session_id' => 'cloud-engine-readiness'"),
'The production gate is no longer a bounded signed engine health probe.');
$assert(str_contains($heartbeat, "'engine_readiness' => '等待私有引擎就绪'")
&& str_contains($heartbeat, "'engine_readiness_timeout'"),
'Readiness observations are not allowlisted by runtime health telemetry.');
$assert(str_contains($lifecycle, '$service->tick(200, $skipThisTick)')
&& substr_count($lifecycleService, 'if (!$skipEngineObservations)') === 2
&& str_contains($lifecycleService, "observeFailure('engine_readiness_timeout')"),
'A readiness timeout can still run first-pass engine observations.');
foreach ([$lifecycle, $worker] as $source) {
$assert(str_contains($source, 'if (!$once)')
&& str_contains($source, 'CloudEngineReadiness::wait()')
&& str_contains($source, "engine_readiness_timeout"),
'A long-running consumer lost its engine readiness gate.');
}
$assert(str_contains($worker, 'self::deliverNotifications($heartbeat)')
&& str_contains($worker, '?callable $engineReadiness = null'),
'An engine-readiness failure can still suppress DB-backed notifications.');
echo "Engine consumer readiness checks passed.\n";