From 291d97a8243a431a850f68248eefce9f07a1b0e7 Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Thu, 26 Nov 2026 23:29:44 +0000 Subject: [PATCH 1/2] Give each parallel test worker its own storage directory Under pest --parallel every worker shared storage/. TestCase::tearDown deletes storage/framework/async after each test, so it kept deleting the async spool while a test in another worker was still using it. Held refreshes then found no payload and quietly never ran, and other tests lost their draft images or failed in tearDown itself. When tearDown threw before parent::tearDown(), the open transaction was never rolled back and every later test in that worker failed with "cannot start a transaction within a transaction". Each worker now sets LARAVEL_STORAGE_PATH to storage/framework/testing/parallel/{token} before the app boots, so the config built from storage_path() follows it. The directory is emptied when the worker starts. A serial run still uses storage/. tearDown also calls parent::tearDown() in a finally block, so one failed cleanup can't take down the rest of the worker. Co-Authored-By: Claude Opus 5.5 --- tests/TestCase.php | 69 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 4 deletions(-) diff --git a/tests/TestCase.php b/tests/TestCase.php index 54b4842d..ab6b6a3f 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -4,6 +4,8 @@ use App\Devnull\Account; use App\Devnull\Timezone; +use Illuminate\Filesystem\Filesystem; +use Illuminate\Foundation\Application; use Illuminate\Foundation\Testing\LazilyRefreshDatabase; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; use Illuminate\Support\Facades\File; @@ -19,6 +21,30 @@ abstract class TestCase extends BaseTestCase */ use LazilyRefreshDatabase; + /** + * The storage directory this worker has emptied and set up, once it has. + */ + protected static ?string $workerStorage = null; + + /** + * Under `pest --parallel` each worker gets a storage directory of its own, + * storage/framework/testing/parallel/{token}, so one worker's tests can't + * delete the async spool, draft images or cached files another worker's + * test is still using. It's emptied when the worker starts. A serial run + * uses storage/ as it always has. + * + * The path is set before the app boots, so the config built from + * storage_path() (disks, logs, compiled views) follows it. + */ + public function createApplication(): Application + { + if ($storage = static::workerStorage()) { + $_ENV['LARAVEL_STORAGE_PATH'] = $storage; + } + + return parent::createApplication(); + } + /** * Background refreshes run inline, so a screen has its fresh answer by its * first frame. A test that wants to see the frame before it calls @@ -38,12 +64,47 @@ protected function setUp(): void $this->app->instance(Timezone::class, new Timezone(localtime: '/nonexistent')); } + /** + * The framework's teardown runs even when this cleanup throws. It rolls + * back the test's transaction, and without that every later test in the + * process fails on the one left open. + */ protected function tearDown(): void { - AsyncTask::clearFake(); - System::rememberPlatform(null); - File::deleteDirectory(AsyncTaskTransport::directory()); + try { + AsyncTask::clearFake(); + System::rememberPlatform(null); + File::deleteDirectory(AsyncTaskTransport::directory()); + } finally { + parent::tearDown(); + } + } + + /** + * This parallel worker's storage directory, or null outside a parallel run. + * It runs before the first app exists, so it can't use the File facade. + */ + protected static function workerStorage(): ?string + { + $token = $_SERVER['TEST_TOKEN'] ?? getenv('TEST_TOKEN'); + + if ($token === false || $token === '') { + return null; + } + + if (static::$workerStorage === null) { + $storage = Application::inferBasePath().'/storage/framework/testing/parallel/'.$token; + $files = new Filesystem; + + $files->deleteDirectory($storage); + + foreach (['app/private', 'app/public', 'framework/cache/data', 'framework/sessions', 'framework/views', 'logs'] as $directory) { + $files->ensureDirectoryExists($storage.'/'.$directory); + } + + static::$workerStorage = $storage; + } - parent::tearDown(); + return static::$workerStorage; } } From 34d528e80197cfc2460817b35d2f08ea762bc710 Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Thu, 26 Nov 2026 23:34:17 +0000 Subject: [PATCH 2/2] Run the CI test suite in parallel Each worker now has its own storage directory, so the suite passes in parallel. Locally it takes about 10 seconds instead of 85. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/tests.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 9ae064fa..45943c96 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -77,7 +77,8 @@ jobs: # Compact prints a dot per passing test and the details of failures only. The runner's # terminal is 80 columns, which cuts the names of failed tests short, so widen it. + # Parallel runs a worker per CPU, each with its own storage directory (see tests/TestCase.php). - name: Execute tests - run: php artisan test --compact + run: php artisan test --compact --parallel env: COLUMNS: 200