From d71f14ac296ef11d4d57da687f5488dd656d8847 Mon Sep 17 00:00:00 2001 From: codex Date: Sun, 1 Nov 2026 14:52:01 +0800 Subject: [PATCH] =?UTF-8?q?test(traffic):=20=E5=9B=BA=E5=8C=96=E8=B7=A8?= =?UTF-8?q?=E5=9C=B0=E5=8C=BA=E7=BA=BF=E8=B7=AF=E6=B1=A0=E5=90=88=E5=90=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CloudAdminTrafficPoolServiceTest.php | 14 ++-- web/tests/CloudInstanceNetworkPolicyTest.php | 48 +++++++++++++ .../CloudTrafficPoolPeriodBindingTest.php | 71 +++++++++++++++++-- 3 files changed, 122 insertions(+), 11 deletions(-) diff --git a/web/tests/CloudAdminTrafficPoolServiceTest.php b/web/tests/CloudAdminTrafficPoolServiceTest.php index cdf4da25..a30a6a48 100644 --- a/web/tests/CloudAdminTrafficPoolServiceTest.php +++ b/web/tests/CloudAdminTrafficPoolServiceTest.php @@ -222,6 +222,10 @@ Capsule::table('cloud_network_lines')->insert([ 'code' => 'standard', 'name' => 'Standard', 'is_default' => 0, 'status' => 'active', 'sort' => 1, 'created_at' => $now, 'updated_at' => $now], + ['id' => 3, 'region_id' => 2, 'traffic_pool_group_id' => null, + 'code' => 'premium', 'name' => 'Premium', 'is_default' => 0, + 'status' => 'active', 'sort' => 0, 'created_at' => $now, + 'updated_at' => $now], ]); $created = CloudAdminTrafficPoolService::create([ @@ -234,12 +238,12 @@ $assert($created['item']['mode'] === 'disabled' 'A new traffic-pool group did not start disabled at revision one.'); $mapped = CloudAdminTrafficPoolService::replaceLines($groupId, [ - 'line_ids' => [1], + 'line_ids' => [1, 3], 'expected_revision' => 1, ]); -$assert($mapped['item']['line_ids'] === [1] +$assert($mapped['item']['line_ids'] === [1, 3] && $mapped['item']['revision'] === 2, - 'The line mapping was not saved atomically.'); + 'The cross-region line mapping was not saved atomically.'); $second = CloudAdminTrafficPoolService::create([ 'code' => 'standard_pool', @@ -253,12 +257,14 @@ CloudAdminTrafficPoolService::replaceLines($secondId, [ $expectConflict( static fn (): array => CloudAdminTrafficPoolService::replaceLines( $groupId, - ['line_ids' => [1, 2], 'expected_revision' => 2] + ['line_ids' => [1, 2, 3], 'expected_revision' => 2] ), 'A line owned by another group was partially attached.' ); $assert((int) Capsule::table('cloud_network_lines')->where('id', 1) ->value('traffic_pool_group_id') === $groupId + && (int) Capsule::table('cloud_network_lines')->where('id', 3) + ->value('traffic_pool_group_id') === $groupId && (int) Capsule::table('cloud_network_lines')->where('id', 2) ->value('traffic_pool_group_id') === $secondId, 'A failed mapping request left a partial catalog update.'); diff --git a/web/tests/CloudInstanceNetworkPolicyTest.php b/web/tests/CloudInstanceNetworkPolicyTest.php index acf3884b..7618e50c 100644 --- a/web/tests/CloudInstanceNetworkPolicyTest.php +++ b/web/tests/CloudInstanceNetworkPolicyTest.php @@ -215,6 +215,54 @@ $assert($exhausted['desired_suspended'] === true && (bool) $traffic->desired_active, 'Pool exhaustion did not create one pending aggregate suspension.'); +$remoteInstance = new CloudInstance(); +$remoteInstance->id = 11; +$remoteInstance->user_id = 20; +$remoteInstance->service_status = 'active'; +$remoteInstance->network_status = 'active'; +$remoteInstance->network_suspended_at = null; +$remoteInstance->expires_at = 2000; +$remoteInstance->updated_at = 1002; +$remoteInstance->save(); +$remotePeriod = new CloudServicePeriod(); +$remotePeriod->id = 31; +$remotePeriod->instance_id = 11; +$remotePeriod->user_id = 20; +$remotePeriod->traffic_pool_group_id = 2; +$remotePeriod->traffic_quota_mode = 'finite'; +$remotePeriod->status = 'active'; +$remotePeriod->starts_at = 900; +$remotePeriod->ends_at = 2000; +$remotePeriod->save(); +$remoteResult = $remoteInstance->getConnection()->transaction( + static function () use ($remoteInstance, $remotePeriod): array { + $locked = CloudInstance::where('id', $remoteInstance->id) + ->lockForUpdate()->firstOrFail(); + $result = CloudInstanceNetworkPolicy::synchronizeLocked( + $locked, + $remotePeriod, + 1002 + ); + $locked->save(); + return $result; + } +); +$assert($remoteResult['desired_suspended'] === true + && (bool) CloudInstanceNetworkRestriction::where('instance_id', 11) + ->where('reason_code', 'traffic_pool_exhausted') + ->firstOrFail()->desired_active, + 'Pool exhaustion did not cover another instance sharing the commercial line group.'); +$remoteInstance->getConnection()->transaction( + static function () use ($remoteInstance): void { + CloudInstanceNetworkPolicy::releaseAfterInstanceDeletedLocked( + CloudInstance::where('id', $remoteInstance->id) + ->lockForUpdate()->firstOrFail() + ); + } +); +$remotePeriod->delete(); +$remoteInstance->delete(); + $instance->getConnection()->transaction(static function () use ($instance): void { $locked = CloudInstance::where('id', $instance->id) ->lockForUpdate()->firstOrFail(); diff --git a/web/tests/CloudTrafficPoolPeriodBindingTest.php b/web/tests/CloudTrafficPoolPeriodBindingTest.php index 83e61856..aec14b2d 100644 --- a/web/tests/CloudTrafficPoolPeriodBindingTest.php +++ b/web/tests/CloudTrafficPoolPeriodBindingTest.php @@ -62,13 +62,24 @@ $capsule->schema('default')->create( } ); Capsule::table('cloud_traffic_pool_groups')->insert([ - 'id' => 7, - 'code' => 'premium', - 'name' => 'Premium', - 'mode' => 'disabled', - 'revision' => 1, - 'created_at' => 1, - 'updated_at' => 1, + [ + 'id' => 7, + 'code' => 'premium', + 'name' => 'Premium', + 'mode' => 'disabled', + 'revision' => 1, + 'created_at' => 1, + 'updated_at' => 1, + ], + [ + 'id' => 8, + 'code' => 'standard', + 'name' => 'Standard', + 'mode' => 'disabled', + 'revision' => 1, + 'created_at' => 1, + 'updated_at' => 1, + ], ]); $pool = new CloudResourcePool(); @@ -96,6 +107,52 @@ $assert($snapshot === [ 'traffic_quota_mode' => 'finite', ], 'Purchase did not freeze the configured line group.'); +$remotePool = new CloudResourcePool(); +$remotePool->id = 4; +$remotePool->line_id = 6; +$remoteLine = new CloudNetworkLine(); +$remoteLine->id = 6; +$remoteLine->traffic_pool_group_id = 7; +$remoteLine->region_id = 2; +$remoteLine->code = 'premium'; +$remoteLine->name = 'Premium'; +$remoteLine->is_default = false; +$remoteLine->status = 'active'; +$remoteLine->sort = 0; +$remoteLine->created_at = 1; +$remoteLine->updated_at = 1; +$remoteLine->save(); +$remoteSnapshot = CloudTrafficPoolPeriodBinding::purchaseSnapshot( + $remotePool, + $remoteLine, + 2048 +); +$assert($remoteSnapshot['traffic_pool_group_id'] === 7, + 'The same commercial line in another region did not freeze the shared pool identity.'); + +$isolatedPool = new CloudResourcePool(); +$isolatedPool->id = 5; +$isolatedPool->line_id = 7; +$isolatedLine = new CloudNetworkLine(); +$isolatedLine->id = 7; +$isolatedLine->traffic_pool_group_id = 8; +$isolatedLine->region_id = 2; +$isolatedLine->code = 'standard'; +$isolatedLine->name = 'Standard'; +$isolatedLine->is_default = false; +$isolatedLine->status = 'active'; +$isolatedLine->sort = 1; +$isolatedLine->created_at = 1; +$isolatedLine->updated_at = 1; +$isolatedLine->save(); +$isolatedSnapshot = CloudTrafficPoolPeriodBinding::purchaseSnapshot( + $isolatedPool, + $isolatedLine, + 2048 +); +$assert($isolatedSnapshot['traffic_pool_group_id'] === 8, + 'A different commercial line was merged into the cross-region pool identity.'); + Capsule::table('cloud_network_lines')->where('id', 5)->update([ 'traffic_pool_group_id' => null, ]);