|
|
|
|
@ -124,6 +124,17 @@ namespace {
|
|
|
|
|
$table->integer('created_at');
|
|
|
|
|
$table->integer('updated_at');
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_invoice_items', static function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->integer('invoice_id');
|
|
|
|
|
$table->integer('order_item_id')->nullable();
|
|
|
|
|
$table->string('code');
|
|
|
|
|
$table->string('label');
|
|
|
|
|
$table->string('amount');
|
|
|
|
|
$table->string('currency')->default('USD');
|
|
|
|
|
$table->text('snapshot_json');
|
|
|
|
|
$table->integer('created_at');
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_mail_outbox', static function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->integer('invoice_id');
|
|
|
|
|
@ -478,5 +489,90 @@ namespace {
|
|
|
|
|
=== $pendingExpiry + 3600,
|
|
|
|
|
'Resolved payment wait did not resume compensation exactly once.');
|
|
|
|
|
|
|
|
|
|
$mailExpiry = $clockNow + 3600;
|
|
|
|
|
$makeInstance(3, $mailExpiry, 'vm-renewal-mail-sending');
|
|
|
|
|
$makeBatch(3, 3, 'c');
|
|
|
|
|
Capsule::table('cloud_invoices')->insert([
|
|
|
|
|
'id' => 3, 'invoice_no' => 'INV-MAIL-SENDING',
|
|
|
|
|
'purpose' => 'renewal', 'instance_id' => 3, 'order_id' => null,
|
|
|
|
|
'user_id' => 1, 'plan_id' => 5, 'renewal_id' => null,
|
|
|
|
|
'renews_expires_at' => $mailExpiry, 'due_at' => $mailExpiry,
|
|
|
|
|
'amount' => '12.00', 'currency' => 'USD', 'validity_days' => 30,
|
|
|
|
|
'traffic_quota_bytes' => 2048, 'status' => 'open',
|
|
|
|
|
'paid_via' => null, 'payment_reference' => null, 'paid_at' => null,
|
|
|
|
|
'created_at' => $now, 'updated_at' => $now,
|
|
|
|
|
]);
|
|
|
|
|
Capsule::table('cloud_mail_outbox')->insert([
|
|
|
|
|
'invoice_id' => 3, 'kind' => 'renewal_due', 'status' => 'sending',
|
|
|
|
|
'attempts' => 1, 'available_at' => $clockNow,
|
|
|
|
|
'lease_token' => str_repeat('f', 32),
|
|
|
|
|
'lease_until' => $clockNow + 300, 'sent_at' => null,
|
|
|
|
|
'last_error' => '', 'created_at' => $now, 'updated_at' => $now,
|
|
|
|
|
]);
|
|
|
|
|
$mailClaim = $execution->claimOne(3);
|
|
|
|
|
$assert(is_array($mailClaim),
|
|
|
|
|
'Mail-fenced compensation item was not claimed.');
|
|
|
|
|
$mailDeferred = $execution->applyClaim(
|
|
|
|
|
(int) $mailClaim['item_id'],
|
|
|
|
|
(string) $mailClaim['token']
|
|
|
|
|
);
|
|
|
|
|
$mailWaiting = CloudServiceCompensationItem::findOrFail(3);
|
|
|
|
|
$assert(($mailDeferred['state'] ?? '') === 'deferred'
|
|
|
|
|
&& ($mailDeferred['last_error_code'] ?? '')
|
|
|
|
|
=== 'renewal_mail_sending'
|
|
|
|
|
&& (int) $mailWaiting->attempts === 0
|
|
|
|
|
&& (int) $mailWaiting->available_at === $clockNow + 300
|
|
|
|
|
&& (int) CloudInstance::findOrFail(3)->expires_at === $mailExpiry
|
|
|
|
|
&& (string) Capsule::table('cloud_invoices')
|
|
|
|
|
->where('id', 3)->value('status') === 'open'
|
|
|
|
|
&& (string) Capsule::table('cloud_mail_outbox')
|
|
|
|
|
->where('invoice_id', 3)->value('status') === 'sending',
|
|
|
|
|
'Sending renewal reminder was not a recoverable compensation fence.');
|
|
|
|
|
|
|
|
|
|
for ($wait = 1; $wait < 10; $wait++) {
|
|
|
|
|
$mailWaiting = CloudServiceCompensationItem::findOrFail(3);
|
|
|
|
|
$clockNow = (int) $mailWaiting->available_at;
|
|
|
|
|
$repeatMailClaim = $execution->claimOne(3);
|
|
|
|
|
$assert(is_array($repeatMailClaim),
|
|
|
|
|
'Long-running renewal mail stopped being claimable for recheck.');
|
|
|
|
|
$execution->applyClaim(
|
|
|
|
|
(int) $repeatMailClaim['item_id'],
|
|
|
|
|
(string) $repeatMailClaim['token']
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$mailWaiting = CloudServiceCompensationItem::findOrFail(3);
|
|
|
|
|
$assert((string) $mailWaiting->state === 'deferred'
|
|
|
|
|
&& (int) $mailWaiting->attempts === 0
|
|
|
|
|
&& (string) $mailWaiting->last_error_code
|
|
|
|
|
=== 'renewal_mail_sending',
|
|
|
|
|
'Long-running renewal mail exhausted compensation retry budget.');
|
|
|
|
|
|
|
|
|
|
Capsule::table('cloud_mail_outbox')->where('invoice_id', 3)->update([
|
|
|
|
|
'status' => 'failed', 'lease_token' => null, 'lease_until' => 0,
|
|
|
|
|
'available_at' => 0,
|
|
|
|
|
'last_error' => '邮件投递结果已核对', 'updated_at' => $clockNow,
|
|
|
|
|
]);
|
|
|
|
|
$clockNow = (int) $mailWaiting->available_at;
|
|
|
|
|
$mailResumedClaim = $execution->claimOne(3);
|
|
|
|
|
$assert(is_array($mailResumedClaim),
|
|
|
|
|
'Resolved renewal-mail fence did not make compensation claimable.');
|
|
|
|
|
$mailApplied = $execution->applyClaim(
|
|
|
|
|
(int) $mailResumedClaim['item_id'],
|
|
|
|
|
(string) $mailResumedClaim['token']
|
|
|
|
|
);
|
|
|
|
|
$replacementInvoiceId = (int) CloudServiceCompensationItem::findOrFail(3)
|
|
|
|
|
->replacement_invoice_id;
|
|
|
|
|
$assert(($mailApplied['state'] ?? '') === 'applied'
|
|
|
|
|
&& (int) CloudInstance::findOrFail(3)->expires_at
|
|
|
|
|
=== $mailExpiry + 3600
|
|
|
|
|
&& (string) Capsule::table('cloud_invoices')
|
|
|
|
|
->where('id', 3)->value('status') === 'void'
|
|
|
|
|
&& $replacementInvoiceId > 0
|
|
|
|
|
&& Capsule::table('cloud_mail_outbox')
|
|
|
|
|
->where('invoice_id', $replacementInvoiceId)
|
|
|
|
|
->where('kind', 'renewal_due')->where('status', 'pending')
|
|
|
|
|
->exists(),
|
|
|
|
|
'Resolved renewal-mail fence did not replace the invoice exactly once.');
|
|
|
|
|
|
|
|
|
|
echo "Service compensation execution tests passed.\n";
|
|
|
|
|
}
|
|
|
|
|
|