Files
compras/tests/Feature/ReplenishmentProcessRecoveryTest.php
2026-11-03 11:51:44 +02:00

31 lines
1.3 KiB
PHP

<?php
use App\Services\ReplenishmentProcessLock;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Symfony\Component\Process\Process;
test('terminated external worker releases the native operating system lock without cleanup', function () {
$originalStorage = app()->storagePath();
$temporary = $originalStorage.'/framework/testing/process-recovery-'.Str::uuid();
File::ensureDirectoryExists($temporary.'/framework');
app()->useStoragePath($temporary);
$path = storage_path('framework/replenishment-command.lock');
$worker = new Process([PHP_BINARY, '-r', '$h = fopen($argv[1], "c"); if (!$h || !flock($h, LOCK_EX | LOCK_NB)) { exit(2); } echo "LOCKED"; fflush(STDOUT); sleep(30);', $path], timeout: 10);
$contender = new ReplenishmentProcessLock;
try {
$worker->start();
expect($worker->waitUntil(fn ($type, $output) => str_contains($output, 'LOCKED')))->toBeTrue();
expect($contender->acquire())->toBeFalse();
$worker->stop(0);
expect($worker->isRunning())->toBeFalse();
expect(File::exists($path))->toBeTrue();
expect($contender->acquire())->toBeTrue();
} finally {
$worker->stop(0);
$contender->release();
app()->useStoragePath($originalStorage);
File::deleteDirectory($temporary);
}
});