|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Copyright 2025 Google Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +namespace Google\Auth\Tests\Cache; |
| 19 | + |
| 20 | +use Google\Auth\Cache\FileSystemCacheItemPool; |
| 21 | +use Google\Auth\Cache\MemoryCacheItemPool; |
| 22 | +use Google\Auth\Cache\SysVCacheItemPool; |
| 23 | +use PHPUnit\Framework\TestCase; |
| 24 | +use Psr\Cache\CacheItemPoolInterface; |
| 25 | +use Symfony\Component\Filesystem\Filesystem; |
| 26 | + |
| 27 | +class RaceConditionTest extends TestCase |
| 28 | +{ |
| 29 | + private static string $cachePath; |
| 30 | + private static Filesystem $filesystem; |
| 31 | + |
| 32 | + public static function setUpBeforeClass(): void |
| 33 | + { |
| 34 | + self::$cachePath = sys_get_temp_dir() . '/google_auth_php_test/'; |
| 35 | + self::$filesystem = new Filesystem(); |
| 36 | + self::$filesystem->remove(self::$cachePath); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @runInSeparateProcess |
| 41 | + * @dataProvider provideRaceCondition |
| 42 | + */ |
| 43 | + public function testRaceCondition(string $cacheClass) |
| 44 | + { |
| 45 | + if (!function_exists('pcntl_fork')) { |
| 46 | + $this->markTestSkipped('pcntl_fork is not available'); |
| 47 | + } |
| 48 | + for ($i = 0; $i < 100; $i++) { |
| 49 | + |
| 50 | + $pids = []; |
| 51 | + for ($j = 0; $j < 4; $j++) { |
| 52 | + $pid = pcntl_fork(); |
| 53 | + if ($pid == -1) { |
| 54 | + $this->fail('Could not fork'); |
| 55 | + } |
| 56 | + $pool = $this->createCacheItemPool($cacheClass); |
| 57 | + $item = $pool->getItem('foo'); |
| 58 | + $item->set('bar'); |
| 59 | + $pool->save($item); |
| 60 | + |
| 61 | + if ($pid) { |
| 62 | + // parent |
| 63 | + $pids[] = $pid; |
| 64 | + } else { |
| 65 | + // child |
| 66 | + exit(0); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // parent |
| 71 | + $pool->save($item); |
| 72 | + |
| 73 | + foreach ($pids as $pid) { |
| 74 | + pcntl_waitpid($pid, $status); |
| 75 | + $this->assertEquals(0, $status); |
| 76 | + } |
| 77 | + |
| 78 | + $this->assertTrue($pool->hasItem('foo')); |
| 79 | + $cachedItem = $pool->getItem('foo'); |
| 80 | + $this->assertEquals('bar', $cachedItem->get()); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public function createCacheItemPool(string $cacheClass): CacheItemPoolInterface |
| 85 | + { |
| 86 | + switch ($cacheClass) { |
| 87 | + case FileSystemCacheItemPool::class: |
| 88 | + $cachePath = self::$cachePath . '/google_auth_php_test-' . rand(); |
| 89 | + return new FileSystemCacheItemPool($cachePath); |
| 90 | + case MemoryCacheItemPool::class: |
| 91 | + return new MemoryCacheItemPool(); |
| 92 | + case SysVCacheItemPool::class: |
| 93 | + return new SysVCacheItemPool(); |
| 94 | + } |
| 95 | + |
| 96 | + throw new \Exception('Unrecognized cache class: ' . $cacheClass); |
| 97 | + } |
| 98 | + |
| 99 | + public function provideRaceCondition() |
| 100 | + { |
| 101 | + return [ |
| 102 | + [FileSystemCacheItemPool::class], |
| 103 | + [MemoryCacheItemPool::class], |
| 104 | + [SysVCacheItemPool::class], |
| 105 | + ]; |
| 106 | + } |
| 107 | + |
| 108 | + public static function tearDownAfterClass(): void |
| 109 | + { |
| 110 | + // remove all files generated from the filecaches |
| 111 | + self::$filesystem->remove(self::$cachePath); |
| 112 | + } |
| 113 | +} |
0 commit comments