Skip to content

[Tests] Isolate ApplicationFileProcessorTest cache dir to survive parallel chunk clears - #8361

Closed
TomasVotruba wants to merge 1 commit into
mainfrom
isolate-app-file-processor-cache-dir
Closed

[Tests] Isolate ApplicationFileProcessorTest cache dir to survive parallel chunk clears#8361
TomasVotruba wants to merge 1 commit into
mainfrom
isolate-app-file-processor-cache-dir

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

ApplicationFileProcessorTest asserts real cache persistence (save then load) against the shared cache directory sys_get_temp_dir() . '/rector_cached_files'. Under fastunit -tia, test chunks run as parallel processes that all share that one directory, and FileCacheStorage::clear() deletes the whole directory:

public function clear(): void
{
    FileSystem::delete($this->directory);
}

So a sibling chunk can wipe this test's cache entry between the save and the load, making hasFileChanged() find nothing and return true ("be defensive and assume it's changed"). The three cache-scope tests then fail with Failed asserting that true is false. — non-deterministically, depending on chunk timing.

Fix

Bind a dedicated cache directory for this test only, so no other chunk's clear() can touch it:

     protected function setUp(): void
     {
         parent::setUp();

+        // isolate the cache directory to this test - the default directory is shared across all
+        // parallel test chunks, and FileCacheStorage::clear() deletes the whole directory, so a
+        // sibling chunk can wipe this test's cache between save and load and flip the assertions
+        $rectorConfig = self::getContainer();
+        SimpleParameterProvider::setParameter(
+            Option::CACHE_DIR,
+            sys_get_temp_dir() . '/rector_test_application_file_processor'
+        );
+        $rectorConfig->singleton(
+            Cache::class,
+            static fn (Container $container): Cache => $container->make(CacheFactory::class)->create()
+        );
+
         $this->applicationFileProcessor = $this->make(ApplicationFileProcessor::class);
         $this->changedFilesDetector = $this->make(ChangedFilesDetector::class);
     }

The Cache singleton is rebound so it is rebuilt from the isolated directory (it is otherwise created once per process from the default directory). Test-only change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant