ActualizeMutexTest.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. declare(strict_types=1);
  3. final class ActualizeMutexTest extends \PHPUnit\Framework\TestCase {
  4. public function testMutexFilesAreUniquePerDataPathAndUseTheConfiguredTemporaryPath(): void {
  5. $testPath = sys_get_temp_dir() . '/freshrss-actualize-mutex-' . bin2hex(random_bytes(8));
  6. $tmpPath = $testPath . '/tmp';
  7. $firstDataPath = $testPath . '/first/data';
  8. $secondDataPath = $testPath . '/second/data';
  9. mkdir($tmpPath, 0700, true);
  10. mkdir($firstDataPath, 0700, true);
  11. mkdir($secondDataPath, 0700, true);
  12. try {
  13. $firstMutex = actualize_mutex_file($tmpPath, $firstDataPath);
  14. $secondMutex = actualize_mutex_file($tmpPath, $secondDataPath);
  15. self::assertSame($firstMutex, actualize_mutex_file($tmpPath, $firstDataPath . '/.'));
  16. self::assertNotSame($firstMutex, $secondMutex);
  17. self::assertStringStartsWith($tmpPath . '/actualize.', $firstMutex);
  18. self::assertStringEndsWith('.freshrss.lock', $firstMutex);
  19. $firstHandle = fopen($firstMutex, 'x');
  20. $secondHandle = fopen($secondMutex, 'x');
  21. self::assertIsResource($firstHandle);
  22. self::assertIsResource($secondHandle);
  23. fclose($firstHandle);
  24. fclose($secondHandle);
  25. self::assertFalse(@fopen($firstMutex, 'x'));
  26. } finally {
  27. recursive_unlink($testPath);
  28. }
  29. }
  30. }