Skip to content

Commit 0932294

Browse files
committed
Make ResultCacheManagerCollectedDataTest independent of the second boundary
saveFullAnalysis() restored the cache with one ResultCacheManager and saved it with another, so the "fresh" save never had the stat signatures the restore recorded and always wrote none. The partial and warm saves record them as soon as the files are older than the second the save begins, so the comparisons failed whenever a second boundary fell between the saves. Restore and save with the same manager, and wait until the files are in the past before saving, like DirectoryWalkerTest and OptimizedDirectorySourceLocatorFactoryTest do, so every save signs every file. Amp-Thread-ID: https://ampcode.com/threads/T-01a1976c-1bde-72f8-b96c-072279109d59
1 parent 92a35f5 commit 0932294

1 file changed

Lines changed: 28 additions & 1 deletion

File tree

‎tests/PHPStan/Analyser/ResultCache/ResultCacheManagerCollectedDataTest.php‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,25 @@
2626
use Symfony\Component\Console\Style\SymfonyStyle as SymfonyConsoleStyle;
2727
use function array_keys;
2828
use function basename;
29+
use function clearstatcache;
2930
use function count;
3031
use function file_get_contents;
3132
use function file_put_contents;
3233
use function is_file;
34+
use function max;
3335
use function md5_file;
3436
use function mkdir;
3537
use function ord;
3638
use function rmdir;
39+
use function stat;
3740
use function strlen;
3841
use function strpos;
3942
use function substr;
4043
use function sys_get_temp_dir;
44+
use function time;
4145
use function uniqid;
4246
use function unlink;
47+
use function usleep;
4348

4449
class ResultCacheManagerCollectedDataTest extends PHPStanTestCase
4550
{
@@ -101,6 +106,7 @@ public function testCachedEntriesAreIndexedAndReadBack(): void
101106

102107
public function testWarmSaveCopiesEveryEntryByteForByte(): void
103108
{
109+
$this->waitUntilTheFilesAreInThePast();
104110
$this->saveFullAnalysis($this->collectedData(['a', 'b', 'c']));
105111
$before = md5_file($this->cacheFilePath());
106112

@@ -114,9 +120,11 @@ public function testWarmSaveCopiesEveryEntryByteForByte(): void
114120

115121
public function testPartialSaveEqualsFreshSaveOfMergedData(): void
116122
{
123+
$this->waitUntilTheFilesAreInThePast();
117124
$this->saveFullAnalysis($this->collectedData(['a', 'b', 'c']));
118125

119126
file_put_contents($this->files[1], "<?php class Cacheb { public function m(): void {} }\n");
127+
$this->waitUntilTheFilesAreInThePast();
120128
$manager = $this->createManager();
121129
$resultCache = $manager->restore($this->files, false, false, null, $this->nullOutput());
122130
$this->assertSame([$this->files[1]], $resultCache->getFilesToAnalyse());
@@ -185,6 +193,25 @@ public function testPreviousEntryFramingIsDiscarded(): void
185193
$this->assertTrue($resultCache->isFullAnalysis());
186194
}
187195

196+
/**
197+
* A save keeps the stat signature only of a file not modified in the second the save began,
198+
* so without waiting, saves on both sides of a second boundary differ in their signatures.
199+
*/
200+
private function waitUntilTheFilesAreInThePast(): void
201+
{
202+
clearstatcache();
203+
$latest = 0;
204+
foreach ($this->files as $file) {
205+
$stat = stat($file);
206+
$this->assertIsArray($stat);
207+
$latest = max($latest, $stat['mtime'], $stat['ctime']);
208+
}
209+
210+
while (time() <= $latest) {
211+
usleep(50_000);
212+
}
213+
}
214+
188215
/**
189216
* @param list<string> $names
190217
* @return array<string, array<class-string<MethodWithoutImpurePointsCollector>, list<string>>>
@@ -206,7 +233,7 @@ private function saveFullAnalysis(array $collectedData): void
206233
{
207234
@unlink($this->cacheFilePath());
208235
$manager = $this->createManager();
209-
$resultCache = $this->createManager()->restore($this->files, false, false, null, $this->nullOutput());
236+
$resultCache = $manager->restore($this->files, false, false, null, $this->nullOutput());
210237
$this->assertTrue($resultCache->isFullAnalysis());
211238

212239
$processed = $manager->process($this->analyserResult($collectedData), $resultCache, $this->nullOutput(), false, true);

0 commit comments

Comments
 (0)