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.
169 lines
6.4 KiB
PHP
169 lines
6.4 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Services\Cloud\CloudTrafficPoolFreshness;
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
|
|
$assert = static function (bool $condition, string $message): void {
|
|
if (!$condition) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
};
|
|
$capsule = new Capsule();
|
|
$capsule->addConnection([
|
|
'driver' => 'sqlite', 'database' => ':memory:',
|
|
], 'default');
|
|
$capsule->setAsGlobal();
|
|
$capsule->bootEloquent();
|
|
$schema = $capsule->schema('default');
|
|
|
|
$schema->create('cloud_traffic_pools', static function (Blueprint $t): void {
|
|
$t->increments('id');
|
|
$t->integer('user_id');
|
|
$t->integer('traffic_pool_group_id');
|
|
$t->string('state');
|
|
$t->integer('available_bytes')->default(0);
|
|
$t->integer('overage_bytes')->default(0);
|
|
$t->integer('version')->default(1);
|
|
$t->integer('updated_at');
|
|
});
|
|
$schema->create('cloud_traffic_entitlements',
|
|
static function (Blueprint $t): void {
|
|
$t->increments('id');
|
|
$t->integer('pool_id');
|
|
$t->string('quota_mode');
|
|
$t->string('state');
|
|
$t->integer('starts_at');
|
|
$t->integer('ends_at');
|
|
$t->integer('granted_bytes');
|
|
$t->integer('consumed_bytes');
|
|
$t->integer('forfeited_bytes')->default(0);
|
|
});
|
|
$schema->create('cloud_service_periods', static function (Blueprint $t): void {
|
|
$t->increments('id');
|
|
$t->integer('instance_id');
|
|
$t->integer('user_id');
|
|
$t->integer('traffic_pool_group_id');
|
|
$t->string('traffic_quota_mode');
|
|
$t->string('status');
|
|
$t->integer('starts_at');
|
|
$t->integer('ends_at');
|
|
});
|
|
$schema->create('cloud_instances', static function (Blueprint $t): void {
|
|
$t->increments('id');
|
|
$t->integer('user_id');
|
|
$t->integer('lifetime_download_bytes');
|
|
$t->integer('lifetime_upload_bytes');
|
|
$t->integer('traffic_synced_at');
|
|
});
|
|
$schema->create('cloud_traffic_instance_cursors',
|
|
static function (Blueprint $t): void {
|
|
$t->integer('instance_id')->primary();
|
|
$t->integer('user_id');
|
|
$t->integer('pool_id');
|
|
$t->integer('accounted_download_bytes');
|
|
$t->integer('accounted_upload_bytes');
|
|
$t->integer('last_observed_at');
|
|
$t->string('state');
|
|
});
|
|
$schema->create('cloud_traffic_scan_failures',
|
|
static function (Blueprint $t): void {
|
|
$t->increments('id');
|
|
$t->string('scan_kind');
|
|
$t->integer('subject_id');
|
|
});
|
|
$schema->create('cloud_traffic_usage_settlement_buckets',
|
|
static function (Blueprint $t): void {
|
|
$t->increments('id');
|
|
$t->integer('pool_id');
|
|
$t->string('state');
|
|
$t->integer('seal_at');
|
|
});
|
|
|
|
$now = 2_000_000;
|
|
Capsule::table('cloud_traffic_pools')->insert([
|
|
'id' => 1, 'user_id' => 7, 'traffic_pool_group_id' => 3,
|
|
'state' => 'available', 'updated_at' => $now - 60,
|
|
]);
|
|
Capsule::table('cloud_service_periods')->insert([
|
|
'id' => 10, 'instance_id' => 20, 'user_id' => 7,
|
|
'traffic_pool_group_id' => 3, 'traffic_quota_mode' => 'finite',
|
|
'status' => 'active', 'starts_at' => $now - 1000,
|
|
'ends_at' => $now + 1000,
|
|
]);
|
|
Capsule::table('cloud_traffic_entitlements')->insert([
|
|
'id' => 30, 'pool_id' => 1, 'quota_mode' => 'finite',
|
|
'state' => 'active', 'starts_at' => $now - 1000,
|
|
'ends_at' => $now + 1000, 'granted_bytes' => 1000,
|
|
'consumed_bytes' => 120, 'forfeited_bytes' => 0,
|
|
]);
|
|
Capsule::table('cloud_instances')->insert([
|
|
'id' => 20, 'user_id' => 7, 'lifetime_download_bytes' => 100,
|
|
'lifetime_upload_bytes' => 20, 'traffic_synced_at' => $now - 60,
|
|
]);
|
|
Capsule::table('cloud_traffic_instance_cursors')->insert([
|
|
'instance_id' => 20, 'user_id' => 7, 'pool_id' => 1,
|
|
'accounted_download_bytes' => 100, 'accounted_upload_bytes' => 20,
|
|
'last_observed_at' => $now - 60, 'state' => 'ready',
|
|
]);
|
|
|
|
$current = CloudTrafficPoolFreshness::snapshot(7, 3, $now);
|
|
$assert($current['status'] === 'current'
|
|
&& $current['safe_code'] === 'current'
|
|
&& $current['last_confirmed_at'] === $now - 60
|
|
&& $current['unsettled_instance_count'] === 0,
|
|
'Fully accounted counters were not projected as current.');
|
|
|
|
Capsule::table('cloud_instances')->where('id', 20)->update([
|
|
'lifetime_download_bytes' => 130,
|
|
'traffic_synced_at' => $now - 10,
|
|
]);
|
|
$delayed = CloudTrafficPoolFreshness::snapshot(7, 3, $now);
|
|
$assert($delayed['status'] === 'delayed'
|
|
&& $delayed['safe_code'] === 'settlement_delayed'
|
|
&& $delayed['unsettled_instance_count'] === 1
|
|
&& $delayed['last_confirmed_at'] === $now - 60,
|
|
'Persisted but unsettled counters did not retain the last confirmation.');
|
|
|
|
Capsule::table('cloud_traffic_instance_cursors')->where('instance_id', 20)
|
|
->update(['state' => 'attention']);
|
|
$attention = CloudTrafficPoolFreshness::snapshot(7, 3, $now);
|
|
$assert($attention['status'] === 'attention'
|
|
&& $attention['safe_code'] === 'cursor_attention',
|
|
'Attention cursor was presented as ordinary delay.');
|
|
|
|
Capsule::table('cloud_instances')->where('id', 20)->update([
|
|
'lifetime_download_bytes' => 100,
|
|
'traffic_synced_at' => $now - 60,
|
|
]);
|
|
Capsule::table('cloud_traffic_instance_cursors')->where('instance_id', 20)
|
|
->update(['state' => 'ready']);
|
|
Capsule::table('cloud_traffic_usage_settlement_buckets')->insert([
|
|
'pool_id' => 1, 'state' => 'open', 'seal_at' => $now - 400,
|
|
]);
|
|
$overdue = CloudTrafficPoolFreshness::snapshot(7, 3, $now);
|
|
$assert($overdue['status'] === 'delayed'
|
|
&& $overdue['overdue_bucket_count'] === 1,
|
|
'Five-minute settlement delay was hidden.');
|
|
Capsule::table('cloud_traffic_usage_settlement_buckets')->where('pool_id', 1)
|
|
->update(['seal_at' => $now - 4000]);
|
|
$overdueAttention = CloudTrafficPoolFreshness::snapshot(7, 3, $now);
|
|
$assert($overdueAttention['status'] === 'attention'
|
|
&& $overdueAttention['safe_code'] === 'settlement_overdue',
|
|
'One-hour settlement delay did not escalate to attention.');
|
|
|
|
$pending = CloudTrafficPoolFreshness::snapshot(9, 3, $now);
|
|
$assert($pending['status'] === 'delayed'
|
|
&& $pending['safe_code'] === 'projection_pending'
|
|
&& $pending['last_confirmed_at'] === null,
|
|
'Missing projection fabricated current accounting.');
|
|
$group = CloudTrafficPoolFreshness::forGroup(3, $now);
|
|
$assert($group['status'] === 'attention'
|
|
&& $group['pool_count'] === 1
|
|
&& $group['overdue_bucket_count'] === 1,
|
|
'Administrator group projection drifted from the user freshness model.');
|
|
|
|
echo "Traffic-pool freshness projection checks passed.\n";
|