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/CloudServiceCompensationWor...

67 lines
2.5 KiB
PHTML

<?php
declare(strict_types=1);
$source = file_get_contents(
dirname(__DIR__) . '/app/Services/Cloud/CloudWorkerService.php'
);
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$assert(is_string($source), 'Cloud worker source could not be read.');
$constructorStart = strpos($source, 'public function __construct(');
$constructorEnd = $constructorStart === false ? false : strpos(
$source,
'private function agentForJob(',
$constructorStart
);
$constructor = $constructorStart === false || $constructorEnd === false
? '' : substr($source, $constructorStart, $constructorEnd - $constructorStart);
$assert(str_contains(
$constructor,
'?CloudServiceCompensationExecutionService $compensations = null'
) && str_contains(
$constructor,
'new CloudServiceCompensationExecutionService()'
),
'Normal cloud worker does not own an injectable compensation executor.');
$tickStart = strpos($source, 'public function tick(): bool');
$tickEnd = $tickStart === false ? false : strpos(
$source,
'private static function quarantineInvalidExecutionModeJob(',
$tickStart
);
$tick = $tickStart === false || $tickEnd === false
? '' : substr($source, $tickStart, $tickEnd - $tickStart);
$job = strpos($tick, '$jobWorked = !$quarantined && $this->tickJob();');
$compensation = strpos(
$tick,
'$compensationWorked = $this->compensations->tick();'
);
$assert($job !== false && $compensation !== false && $job < $compensation,
'A compensation failure may prevent ordinary Agent work in the same tick.');
$assert(str_contains($tick, 'if (!$this->policyOnly && !$this->vpcBatchOnly)')
&& $compensation > strpos(
$tick,
'if (!$this->policyOnly && !$this->vpcBatchOnly)'
),
'Policy-only or VPC-only workers may execute service compensation.');
$assert(str_contains(
$tick,
"\$this->observedErrors[] = 'service_compensation_failed';"
) && str_contains($tick, 'catch (\\Throwable $error)')
&& str_contains($tick, '|| $compensationWorked'),
'Compensation work or its stable observation error is not projected.');
$assert(substr_count(
$tick,
'$compensationWorked = $this->compensations->tick();'
) === 1
&& !str_contains($tick, 'while ($this->compensations')
&& !str_contains($tick, 'do { $this->compensations'),
'One worker tick may claim more than one compensation item.');
echo "Service compensation worker contracts passed.\n";