|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Unit; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Utopia\Cache\Adapter\Memory as CacheMemory; |
| 7 | +use Utopia\Cache\Cache; |
| 8 | +use Utopia\Database\Adapter\Memory as DatabaseMemory; |
| 9 | +use Utopia\Database\Database; |
| 10 | +use Utopia\Database\Document; |
| 11 | +use Utopia\Database\Helpers\Permission; |
| 12 | +use Utopia\Database\Helpers\Role; |
| 13 | + |
| 14 | +class ForUpdateCacheTest extends TestCase |
| 15 | +{ |
| 16 | + private DatabaseMemory $adapter; |
| 17 | + |
| 18 | + private Database $database; |
| 19 | + |
| 20 | + protected function setUp(): void |
| 21 | + { |
| 22 | + $this->adapter = new DatabaseMemory(); |
| 23 | + $this->database = new Database($this->adapter, new Cache(new CacheMemory())); |
| 24 | + $this->database |
| 25 | + ->setDatabase('utopiaTests') |
| 26 | + ->setNamespace('for_update_' . \uniqid()); |
| 27 | + |
| 28 | + $this->database->create(); |
| 29 | + $this->database->createCollection('projects'); |
| 30 | + $this->database->createAttribute('projects', 'name', Database::VAR_STRING, 255, false); |
| 31 | + $this->database->createAttribute('projects', 'description', Database::VAR_STRING, 255, false); |
| 32 | + $this->database->createDocument('projects', new Document([ |
| 33 | + '$id' => 'project', |
| 34 | + '$permissions' => [ |
| 35 | + Permission::read(Role::any()), |
| 36 | + Permission::update(Role::any()), |
| 37 | + ], |
| 38 | + 'name' => 'stale', |
| 39 | + 'description' => 'stale', |
| 40 | + ])); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Write to the row through the adapter, bypassing Database and therefore |
| 45 | + * the cache purge — the cache now holds a stale copy, exactly as if a |
| 46 | + * concurrent reader had re-cached the old row after a writer's purge. |
| 47 | + */ |
| 48 | + private function staleCache(string $attribute, string $value): void |
| 49 | + { |
| 50 | + $collection = $this->database->getCollection('projects'); |
| 51 | + $document = $this->adapter->getDocument($collection, 'project'); |
| 52 | + $document->setAttribute($attribute, $value); |
| 53 | + $this->adapter->updateDocument($collection, 'project', $document, true); |
| 54 | + } |
| 55 | + |
| 56 | + public function testForUpdateReadBypassesStaleCache(): void |
| 57 | + { |
| 58 | + $cached = $this->database->getDocument('projects', 'project'); |
| 59 | + $this->assertSame('stale', $cached->getAttribute('name')); |
| 60 | + |
| 61 | + $this->staleCache('name', 'fresh'); |
| 62 | + |
| 63 | + $cached = $this->database->getDocument('projects', 'project'); |
| 64 | + $this->assertSame('stale', $cached->getAttribute('name')); |
| 65 | + |
| 66 | + $fresh = $this->database->getDocument('projects', 'project', forUpdate: true); |
| 67 | + $this->assertSame('fresh', $fresh->getAttribute('name')); |
| 68 | + } |
| 69 | + |
| 70 | + public function testForUpdateReadDoesNotPopulateCache(): void |
| 71 | + { |
| 72 | + $this->database->getDocument('projects', 'project'); |
| 73 | + $this->staleCache('name', 'fresh'); |
| 74 | + |
| 75 | + $this->database->getDocument('projects', 'project', forUpdate: true); |
| 76 | + |
| 77 | + // The locking read happens inside an open transaction; caching what it |
| 78 | + // saw would publish a pre-commit row. The cached copy must be untouched. |
| 79 | + $cached = $this->database->getDocument('projects', 'project'); |
| 80 | + $this->assertSame('stale', $cached->getAttribute('name')); |
| 81 | + } |
| 82 | + |
| 83 | + public function testUpdateDocumentDoesNotResurrectStaleCachedAttributes(): void |
| 84 | + { |
| 85 | + $this->database->getDocument('projects', 'project'); |
| 86 | + $this->staleCache('name', 'fresh'); |
| 87 | + |
| 88 | + // updateDocument merges the changes into its locking read of the current |
| 89 | + // row. Served from the stale cache, that merge would durably revert |
| 90 | + // name to 'stale' — the lost-update behind flaky project config tests. |
| 91 | + $this->database->updateDocument('projects', 'project', new Document([ |
| 92 | + 'description' => 'updated', |
| 93 | + ])); |
| 94 | + |
| 95 | + $collection = $this->database->getCollection('projects'); |
| 96 | + $row = $this->adapter->getDocument($collection, 'project'); |
| 97 | + $this->assertSame('fresh', $row->getAttribute('name')); |
| 98 | + $this->assertSame('updated', $row->getAttribute('description')); |
| 99 | + |
| 100 | + $document = $this->database->getDocument('projects', 'project'); |
| 101 | + $this->assertSame('fresh', $document->getAttribute('name')); |
| 102 | + $this->assertSame('updated', $document->getAttribute('description')); |
| 103 | + } |
| 104 | +} |
0 commit comments