|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace NormCache\Tests\Integration\Cache; |
| 4 | + |
| 5 | +use Illuminate\Support\Facades\DB; |
| 6 | +use NormCache\Planning\TableIdentityResolver; |
| 7 | +use NormCache\Tests\Fixtures\Models\Author; |
| 8 | +use NormCache\Tests\Fixtures\Models\RawPost; |
| 9 | +use NormCache\Tests\TestCase; |
| 10 | +use NormCache\Values\TableIdentity; |
| 11 | + |
| 12 | +/** |
| 13 | + * A write that lands while a miss is fetching from the database must not be overwritten |
| 14 | + * by the publish that follows it. The guard is inside publish_canonical / |
| 15 | + * publish_versioned_entries, so these cover it without a PHP-side pre-check. |
| 16 | + */ |
| 17 | +final class PublishRaceTest extends TestCase |
| 18 | +{ |
| 19 | + private int $authorId; |
| 20 | + |
| 21 | + protected function setUp(): void |
| 22 | + { |
| 23 | + parent::setUp(); |
| 24 | + |
| 25 | + $this->authorId = (int) Author::query()->create(['name' => 'Author'])->getKey(); |
| 26 | + |
| 27 | + $rows = []; |
| 28 | + |
| 29 | + for ($index = 1; $index <= 20; $index++) { |
| 30 | + $rows[] = [ |
| 31 | + 'id' => $index, |
| 32 | + 'title' => "Post {$index}", |
| 33 | + 'views' => $index, |
| 34 | + 'published' => true, |
| 35 | + 'author_id' => $this->authorId, |
| 36 | + 'created_at' => now(), |
| 37 | + 'updated_at' => now(), |
| 38 | + ]; |
| 39 | + } |
| 40 | + |
| 41 | + RawPost::query()->toBase()->insert($rows); |
| 42 | + } |
| 43 | + |
| 44 | + private function tableIdentity(string $table): TableIdentity |
| 45 | + { |
| 46 | + $identity = $this->app->make(TableIdentityResolver::class) |
| 47 | + ->resolve(DB::connection(), $table); |
| 48 | + |
| 49 | + $this->assertNotNull($identity); |
| 50 | + |
| 51 | + return $identity; |
| 52 | + } |
| 53 | + |
| 54 | + /** Bumps the table version once, while the given select is in flight. */ |
| 55 | + private function bumpVersionDuring(string $sqlNeedle, callable $callback): mixed |
| 56 | + { |
| 57 | + $bumped = false; |
| 58 | + $versionKey = $this->cacheKeys()->version($this->tableIdentity('posts')); |
| 59 | + |
| 60 | + DB::listen(function ($query) use (&$bumped, $sqlNeedle, $versionKey): void { |
| 61 | + if ($bumped || !str_contains($query->sql, $sqlNeedle)) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + $bumped = true; |
| 66 | + $this->cacheStore()->increment($versionKey); |
| 67 | + }); |
| 68 | + |
| 69 | + $result = $callback(); |
| 70 | + |
| 71 | + $this->assertTrue($bumped, 'expected the miss to reach the database'); |
| 72 | + |
| 73 | + return $result; |
| 74 | + } |
| 75 | + |
| 76 | + public function test_a_version_bump_during_the_build_leaves_no_canonical_entry(): void |
| 77 | + { |
| 78 | + $rows = $this->bumpVersionDuring( |
| 79 | + 'select * from "posts"', |
| 80 | + fn() => RawPost::query()->toBase()->get(), |
| 81 | + ); |
| 82 | + |
| 83 | + $this->assertCount(20, $rows, 'the caller still gets its rows'); |
| 84 | + $this->assertSame( |
| 85 | + [], |
| 86 | + $this->cacheQueryKeysWithField('m'), |
| 87 | + 'the membership must not be published against a version that already moved', |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + public function test_the_read_after_a_raced_build_is_a_miss_not_stale_data(): void |
| 92 | + { |
| 93 | + $this->bumpVersionDuring( |
| 94 | + 'select * from "posts"', |
| 95 | + fn() => RawPost::query()->toBase()->get(), |
| 96 | + ); |
| 97 | + |
| 98 | + RawPost::query()->toBase()->where('id', 3)->update(['title' => 'changed']); |
| 99 | + |
| 100 | + $rows = collect(RawPost::query()->toBase()->get()); |
| 101 | + |
| 102 | + $this->assertSame('changed', $rows->firstWhere('id', 3)->title); |
| 103 | + } |
| 104 | + |
| 105 | + public function test_a_version_bump_during_a_result_build_leaves_no_entry(): void |
| 106 | + { |
| 107 | + $count = $this->bumpVersionDuring( |
| 108 | + 'select count(*)', |
| 109 | + fn() => RawPost::query()->toBase()->count(), |
| 110 | + ); |
| 111 | + |
| 112 | + $this->assertSame(20, $count); |
| 113 | + $this->assertSame( |
| 114 | + [], |
| 115 | + $this->cacheQueryKeysWithField('r'), |
| 116 | + 'the result payload must not be published against a moved version', |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + public function test_the_lease_is_released_when_the_publish_guard_rejects(): void |
| 121 | + { |
| 122 | + $this->bumpVersionDuring( |
| 123 | + 'select * from "posts"', |
| 124 | + fn() => RawPost::query()->toBase()->get(), |
| 125 | + ); |
| 126 | + |
| 127 | + $this->assertSame( |
| 128 | + [], |
| 129 | + $this->cacheKeysMatching(':build:'), |
| 130 | + 'a rejected publish must not leak the build lease', |
| 131 | + ); |
| 132 | + } |
| 133 | +} |
0 commit comments