74 lines
3.6 KiB
PHP
74 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Deployment;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use Symfony\Component\Process\Process;
|
|
|
|
class FinanceBatchIsolationTest extends TestCase
|
|
{
|
|
public function test_busy_sync_lock_prevents_starting_a_second_heavy_job(): void
|
|
{
|
|
$directory = sys_get_temp_dir() . '/caiwu-batch-' . bin2hex(random_bytes(6));
|
|
mkdir($directory);
|
|
$lock = fopen($directory . '/sync.lock', 'c');
|
|
flock($lock, LOCK_EX);
|
|
try {
|
|
$process = $this->runBatch($directory, ['jushuitan:sync-shipping', '--days=3']);
|
|
$this->assertSame(75, $process->getExitCode());
|
|
$this->assertFileDoesNotExist($directory . '/called');
|
|
flock($lock, LOCK_UN);
|
|
$process = $this->runBatch($directory, ['jushuitan:sync-shipping', '--days=3']);
|
|
$this->assertTrue($process->isSuccessful(), $process->getErrorOutput());
|
|
$arguments = file($directory . '/called', FILE_IGNORE_NEW_LINES);
|
|
$this->assertContains('batch', $arguments);
|
|
$this->assertSame(['jushuitan:sync-shipping', '--days=3'], array_slice($arguments, -2));
|
|
$this->assertContains('--no-deps', $arguments);
|
|
$this->assertSame('image=123456789abc', $arguments[0]);
|
|
} finally {
|
|
fclose($lock);
|
|
foreach (glob($directory . '/*') as $path) { unlink($path); }
|
|
rmdir($directory);
|
|
}
|
|
}
|
|
|
|
private function runBatch(string $directory, array $arguments): Process
|
|
{
|
|
file_put_contents($directory . '/docker', '#!/bin/sh' . "\n"
|
|
. 'if [ "$1" = inspect ]; then case "$4" in *Mounts*) echo "$CAIWU_STORAGE_ROOT" ;; *) echo caiwu_akaunting:123456789abc ;; esac; exit 0; fi' . "\n"
|
|
. 'printf "%s\n" "image=$IMAGE_TAG" "$@" > "$CAIWU_CAPTURE"' . "\n");
|
|
chmod($directory . '/docker', 0700);
|
|
$process = new Process(array_merge(['sh', dirname(__DIR__, 3) . '/deploy/run-finance-batch.sh'], $arguments), $directory, [
|
|
'PATH' => $directory . ':' . getenv('PATH'), 'CAIWU_REMOTE_DIR' => $directory,
|
|
'CAIWU_UPSTREAM_LOCK' => $directory . '/sync.lock', 'CAIWU_BATCH_LOCK_WAIT' => '0.1',
|
|
'CAIWU_CAPTURE' => $directory . '/called',
|
|
'CAIWU_STORAGE_ROOT' => $directory . '/storage',
|
|
]);
|
|
$process->run();
|
|
|
|
return $process;
|
|
}
|
|
|
|
public function test_exports_skip_idle_containers_and_can_read_while_upstream_writer_holds_lock(): void
|
|
{
|
|
$directory = sys_get_temp_dir() . '/caiwu-export-batch-' . bin2hex(random_bytes(6));
|
|
mkdir($directory . '/storage/app/account-bills-exports/1', 0777, true);
|
|
$lock = fopen($directory . '/sync.lock', 'c');
|
|
flock($lock, LOCK_EX);
|
|
$task = $directory . '/storage/app/account-bills-exports/1/task.json';
|
|
try {
|
|
$this->assertTrue($this->runBatch($directory, ['account-bills:exports:run'])->isSuccessful());
|
|
$this->assertFileDoesNotExist($directory . '/called');
|
|
file_put_contents($task, json_encode(['status' => 'queued']));
|
|
$this->assertTrue($this->runBatch($directory, ['account-bills:exports:run'])->isSuccessful());
|
|
$this->assertFileExists($directory . '/called');
|
|
} finally {
|
|
fclose($lock);
|
|
if (is_file($task)) unlink($task);
|
|
foreach (['/storage/app/account-bills-exports/1', '/storage/app/account-bills-exports', '/storage/app', '/storage'] as $path) rmdir($directory . $path);
|
|
foreach (glob($directory . '/*') as $path) unlink($path);
|
|
rmdir($directory);
|
|
}
|
|
}
|
|
}
|