47 lines
2.4 KiB
PHP
47 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\EcommerceFinance;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Modules\EcommerceFinance\Services\AccountBillsExportTasks;
|
|
use Tests\TestCase;
|
|
|
|
class AccountBillsExportQueueTest extends TestCase
|
|
{
|
|
public function test_contention_retains_queued_tasks_and_one_run_only_consumes_one(): void
|
|
{
|
|
config(['cache.stores.upstream_shared' => ['driver' => 'array'], 'upstream_pipeline.batch_worker_enabled' => true]);
|
|
$directory = sys_get_temp_dir() . '/caiwu-export-queue-' . bin2hex(random_bytes(6));
|
|
$tasks = new class($directory) extends AccountBillsExportTasks {
|
|
public function __construct(private string $directory) {}
|
|
public function rootDir(): string { return $this->directory; }
|
|
};
|
|
$this->app->instance(AccountBillsExportTasks::class, $tasks);
|
|
$a = $tasks->create(11, 'test', '测试', [], [['field' => 'test', 'label' => '测试']], 0);
|
|
$b = $tasks->create(11, 'test', '测试', [], [['field' => 'test', 'label' => '测试']], 0);
|
|
$a = $tasks->patch($a, ['planned_at' => '2026-10-01 00:00:00', 'cancel' => true]);
|
|
$b = $tasks->patch($b, ['planned_at' => '2026-10-02 00:00:00', 'cancel' => true]);
|
|
$lock = Cache::store('upstream_shared')->lock('account-bills:export-worker', 10800);
|
|
try {
|
|
$tasks->dispatch($a);
|
|
$this->assertSame('queued', $tasks->find(11, $a['token'])['status']);
|
|
$this->assertTrue($lock->get());
|
|
$this->artisan('account-bills:exports:run')->assertExitCode(0);
|
|
$this->assertSame('queued', $tasks->find(11, $a['token'])['status']);
|
|
$this->assertSame('queued', $tasks->find(11, $b['token'])['status']);
|
|
$lock->release();
|
|
$this->artisan('account-bills:exports:run')->assertExitCode(0);
|
|
$this->assertSame('cancelled', $tasks->find(11, $a['token'])['status']);
|
|
$this->assertSame('queued', $tasks->find(11, $b['token'])['status']);
|
|
$this->artisan('account-bills:exports:run')->assertExitCode(0);
|
|
$this->assertSame('cancelled', $tasks->find(11, $b['token'])['status']);
|
|
$this->assertNull($tasks->nextQueued());
|
|
} finally {
|
|
$lock->release();
|
|
foreach (glob($directory . '/11/*') ?: [] as $file) unlink($file);
|
|
rmdir($directory . '/11');
|
|
rmdir($directory);
|
|
}
|
|
}
|
|
}
|