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

91 lines
4.3 KiB
PHP

<?php
$source = file_get_contents(dirname(__DIR__)
. '/app/Services/Cloud/CloudTrafficPoolAccounting.php');
if (!is_string($source)) {
throw new RuntimeException('Unable to read traffic-pool accounting module.');
}
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$method = static function (
string $name,
string $visibility = 'public'
) use ($source): string {
$start = strpos($source,
$visibility . ' static function ' . $name . '(');
if ($start === false) {
throw new RuntimeException('Missing method ' . $name);
}
$next = strpos($source, "\n public static function ", $start + 1);
$private = strpos($source, "\n private static function ", $start + 1);
$ends = array_values(array_filter([$next, $private],
static fn ($value): bool => $value !== false));
$end = $ends === [] ? strlen($source) : min($ends);
return substr($source, $start, $end - $start);
};
$settle = $method('settleInstanceUsageAttempt', 'private');
$poolLock = strpos($settle, "->where('traffic_pool_group_id', \$groupId)\n ->lockForUpdate()");
$cursorLock = strpos($settle,
"'instance_id', \$instanceId\n )->lockForUpdate()");
$applyUsage = strpos($settle, 'self::applyUsageDeltaLocked(');
$helperStart = strpos($source,
'private static function applyUsageDeltaLocked(');
$helperEnd = strpos($source, "\n private static function ",
$helperStart + 1);
$helper = $helperStart === false ? '' : substr(
$source,
$helperStart,
($helperEnd === false ? strlen($source) : $helperEnd) - $helperStart
);
$entitlementLock = strpos($helper, "->lockForUpdate()->get();");
$assert($poolLock !== false && $cursorLock !== false
&& $applyUsage !== false && $entitlementLock !== false
&& $poolLock < $cursorLock && $cursorLock < $applyUsage,
'Usage settlement no longer follows pool -> cursor -> entitlement locks.');
$assert(!str_contains($settle,
"CloudInstance::where('id', \$instanceId)->lockForUpdate()")
&& !str_contains($settle, 'CloudAgentClient'),
'Pool accounting must not lock an instance first or call an Agent.');
$assert(str_contains($settle, 'snapshotObservedAt')
&& str_contains($settle, "'retry_snapshot' => true"),
'Usage settlement no longer binds counters to one durable sample time.');
$period = $method('reconcileServicePeriod');
$periodPoolLock = strpos($period,
"->where('traffic_pool_group_id', \$groupId)\n ->lockForUpdate()");
$periodRowLock = strpos($period,
"CloudServicePeriod::where('id', \$servicePeriodId)\n ->lockForUpdate()");
$periodEntitlementLock = strpos($period,
"'service_period_id', \$servicePeriodId\n )->lockForUpdate()");
$assert($periodPoolLock !== false && $periodRowLock !== false
&& $periodEntitlementLock !== false
&& $periodPoolLock < $periodRowLock
&& $periodRowLock < $periodEntitlementLock,
'Period reconciliation no longer locks pool before period and entitlement.');
$due = $method('reconcileDue');
$assert(str_contains($due,
"->whereIn('due_group.mode', ['shadow', 'enforced'])")
&& str_contains($due,
"->whereIn('changed_group.mode', ['shadow', 'enforced'])"),
'Disabled groups can permanently starve due-period reconciliation.');
$assert(str_contains($source, "'amount_bytes' => (int) (\$data['amount_bytes'] ?? 0)")
&& str_contains($source, "'effective_at' => (int) \$data['effective_at']")
&& str_contains($source, "'entitlement_id' => \$data['entitlement_id'] ?? null"),
'Event idempotency hash does not cover the complete accounting envelope.');
$assert(str_contains($source, 'public static function resolveAttention(')
&& str_contains($source, 'expectedVersion')
&& str_contains($source, "'event_type' => 'attention_resolved'"),
'Attention fences have no versioned, audited recovery interface.');
$pending = $method('settlePendingUsage');
$assert(str_contains($pending, "->orWhere('traffic_cursor.state', 'ready')")
&& !str_contains($pending,
"->where('cloud_instances.status', '<>', 'deleted')"),
'Normal scans must isolate attention cursors and settle deleted tail data.');
echo "Cloud traffic pool lock-order contract passed.\n";