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

124 lines
4.8 KiB
PHTML

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudWorkerService;
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();
$capsule->schema('default')->create('cloud_jobs',
static function (Blueprint $table): void {
$table->increments('id');
$table->string('operation_id')->unique();
$table->string('request_idempotency_key');
$table->string('request_fingerprint');
$table->integer('parent_job_id')->nullable();
$table->integer('order_id')->nullable();
$table->integer('invoice_id')->nullable();
$table->integer('instance_id')->nullable();
$table->integer('snapshot_id')->nullable();
$table->integer('user_id');
$table->integer('host_id')->nullable();
$table->string('kind');
$table->string('execution_mode');
$table->string('phase');
$table->string('outcome')->nullable();
$table->string('status');
$table->text('sealed_payload');
$table->integer('attempts')->default(0);
$table->integer('dispatch_attempts')->default(0);
$table->integer('dispatch_deadline_at');
$table->integer('dispatch_started_at')->nullable();
$table->integer('agent_accepted_at')->nullable();
$table->integer('receipt_deadline_at')->nullable();
$table->integer('terminal_at')->nullable();
$table->boolean('effect_may_have_applied')->nullable();
$table->boolean('safe_to_resubmit')->nullable();
$table->string('effect_resolution')->default('not_applicable');
$table->integer('effect_resolved_at')->nullable();
$table->integer('effect_resolved_by')->nullable();
$table->string('effect_resolution_reason')->default('');
$table->string('completion_event_id')->nullable();
$table->integer('read_at')->nullable();
$table->integer('dispatched_at')->nullable();
$table->string('lease_token')->nullable();
$table->integer('lease_until')->default(0);
$table->integer('available_at')->default(0);
$table->string('error_code')->nullable();
$table->boolean('finalization_incident')->default(0);
$table->string('finalization_error_code')->nullable();
$table->text('result_json')->nullable();
$table->integer('created_at');
$table->integer('updated_at');
});
$now = time();
$base = [
'request_fingerprint' => str_repeat('f', 64),
'parent_job_id' => null,
'order_id' => null,
'invoice_id' => null,
'instance_id' => null,
'snapshot_id' => null,
'user_id' => 1,
'host_id' => null,
'outcome' => null,
'sealed_payload' => 'intentionally-invalid-test-envelope',
'attempts' => 0,
'dispatch_attempts' => 0,
'dispatch_deadline_at' => $now + 300,
'receipt_deadline_at' => null,
'lease_until' => 0,
'available_at' => 0,
'created_at' => $now,
'updated_at' => $now,
];
Capsule::table('cloud_jobs')->insert(array_merge($base, [
'id' => 1,
'operation_id' => str_repeat('a', 32),
'request_idempotency_key' => str_repeat('a', 32),
'kind' => 'template_import',
'execution_mode' => 'aggregate',
'phase' => 'awaiting_receipt',
'status' => 'running',
'receipt_deadline_at' => $now + 8400,
]));
Capsule::table('cloud_jobs')->insert(array_merge($base, [
'id' => 2,
'operation_id' => str_repeat('b', 32),
'request_idempotency_key' => str_repeat('b', 32),
'kind' => 'payment_create',
'execution_mode' => 'local',
'phase' => 'accepted',
'status' => 'queued',
]));
$worker = new CloudWorkerService();
$assert($worker->tick() === true,
'The host-null local payment task was not claimed.');
$local = Capsule::table('cloud_jobs')->where('id', 2)->first();
$aggregate = Capsule::table('cloud_jobs')->where('id', 1)->first();
$assert($local !== null && $local->phase === 'terminal'
&& $local->outcome === 'failed' && $local->status === 'failed'
&& (int) $local->dispatch_attempts === 0,
'A local payment failure did not close once without an Agent dispatch.');
$assert($aggregate !== null && $aggregate->phase === 'awaiting_receipt'
&& $aggregate->outcome === null && $aggregate->status === 'running'
&& (int) $aggregate->dispatch_attempts === 0,
'The worker incorrectly claimed an aggregate parent task.');
$assert($worker->tick() === false,
'The terminal local failure or aggregate parent remained worker-claimable.');
echo "Explicit execution-mode worker claim tests passed.\n";