Repository navigation
Expand file tree
/
Copy pathQueryCacheTest.php
More file actions
696 lines (557 loc) · 26.1 KB
/
Copy pathQueryCacheTest.php
File metadata and controls
696 lines (557 loc) · 26.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
<?php
namespace Tests\Unit\Cache;
use PHPUnit\Framework\MockObject\Stub;
use PHPUnit\Framework\TestCase;
use Utopia\Cache\Adapter\Memory;
use Utopia\Cache\Cache;
use Utopia\Database\Cache\Entry;
use Utopia\Database\Cache\Invalidator;
use Utopia\Database\Cache\QueryCache;
use Utopia\Database\Cache\Region;
use Utopia\Database\Cache\Scope;
use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Database\Event;
use Utopia\Database\Query;
class QueryCacheTest extends TestCase
{
private QueryCache $queryCache;
private Cache&Stub $cache;
protected function setUp(): void
{
$this->cache = self::createCache();
$this->queryCache = new QueryCache($this->cache);
}
public function testConstructorWithDefaults(): void
{
$queryCache = new QueryCache(self::createCache());
$this->assertNotNull($queryCache->getEntry(new Scope(), 'any_collection', []));
}
public function testConstructorWithCustomName(): void
{
$queryCache = new QueryCache(self::createCache(), 'custom');
$entry = $queryCache->getEntry(new Scope(), 'users', []);
$this->assertNotNull($entry);
$this->assertStringStartsWith('custom:', $entry->key);
}
public function testSetRegionAndGetRegion(): void
{
$region = new Region(ttl: 600, enabled: false);
$this->queryCache->setRegion('users', $region);
$this->assertSame($region, $this->queryCache->getRegion('users'));
}
public function testGetRegionReturnsDefaultForUnknownCollection(): void
{
$region = $this->queryCache->getRegion('unknown');
$this->assertSame(3600, $region->ttl);
$this->assertTrue($region->enabled);
}
public function testEntryKeysAreStable(): void
{
$queries = [Query::equal('status', ['active'])];
$scope = new Scope(namespace: 'ns', tenant: 1);
$first = $this->queryCache->getEntry($scope, 'users', $queries);
$second = $this->queryCache->getEntry($scope, 'users', $queries);
$this->assertNotNull($first);
$this->assertNotNull($second);
$this->assertSame($first->key, $second->key);
}
public function testCollectionKeysSeparateEveryScopeField(): void
{
$key = $this->queryCache->getCollectionKey(new Scope('host', 'database', 'namespace', 1), 'users');
foreach ([
new Scope('other', 'database', 'namespace', 1),
new Scope('host', 'other', 'namespace', 1),
new Scope('host', 'database', 'other', 1),
new Scope('host', 'database', 'namespace', 2),
new Scope('host', 'database', 'namespace', null),
] as $scope) {
$this->assertNotSame($key, $this->queryCache->getCollectionKey($scope, 'users'));
}
}
public function testCollectionKeysPreserveTenantType(): void
{
$this->assertNotSame(
$this->queryCache->getCollectionKey(new Scope(tenant: 1), 'users'),
$this->queryCache->getCollectionKey(new Scope(tenant: '1'), 'users'),
);
}
public function testCollectionKeysSeparateCollectionsThatDifferOnlyInCase(): void
{
$this->assertNotSame(
\strtolower($this->queryCache->getCollectionKey(new Scope(), 'Users')),
\strtolower($this->queryCache->getCollectionKey(new Scope(), 'users')),
'Cache keys are case-insensitive by default, so the collection must be part of the scope hash',
);
}
public function testDifferentQueriesProduceDifferentEntries(): void
{
$first = $this->queryCache->getEntry(new Scope(), 'users', [Query::equal('a', [1])]);
$second = $this->queryCache->getEntry(new Scope(), 'users', [Query::equal('b', [2])]);
$this->assertNotNull($first);
$this->assertNotNull($second);
$this->assertNotSame($first->key, $second->key);
}
public function testDifferentCollectionsProduceDifferentEntries(): void
{
$users = $this->queryCache->getEntry(new Scope(), 'users', []);
$posts = $this->queryCache->getEntry(new Scope(), 'posts', []);
$this->assertNotNull($users);
$this->assertNotNull($posts);
$this->assertNotSame($users->key, $posts->key);
}
public function testGetReturnsNullForCacheMiss(): void
{
$this->cache->method('load')->willReturn(false);
$this->assertNull($this->queryCache->get(new Entry('some-key', 'users')));
}
public function testGetReturnsNullForNullData(): void
{
$this->cache->method('load')->willReturn(null);
$this->assertNull($this->queryCache->get(new Entry('some-key', 'users')));
}
public function testGetReturnsDocumentArrayForCacheHit(): void
{
$this->cache->method('load')->willReturn([
'version' => 1,
'documents' => [
['$id' => 'doc1', 'name' => 'Alice'],
['$id' => 'doc2', 'name' => 'Bob'],
],
]);
$result = $this->queryCache->get(new Entry('some-key', 'users'));
$this->assertNotNull($result);
$this->assertCount(2, $result);
$this->assertSame('doc1', $result[0]->getId());
}
public function testGetHandlesDocumentObjectsInCache(): void
{
$document = new Document(['$id' => 'doc1', 'name' => 'Alice']);
$this->cache->method('load')->willReturn([
'version' => 1,
'documents' => [$document],
]);
$result = $this->queryCache->get(new Entry('some-key', 'users'));
$this->assertNotNull($result);
$this->assertCount(1, $result);
$this->assertSame($document, $result[0]);
}
public function testGetPropagatesMalformedPayloadPurgeFailure(): void
{
$this->cache->method('load')->willReturn('not-an-array');
$this->expectException(\RuntimeException::class);
$this->queryCache->get(new Entry('some-key', 'users'));
}
public function testEntriesExpireWithTheRegionButEpochsNeverDo(): void
{
$cache = $this->createMock(Cache::class);
$queryCache = new QueryCache($cache);
$queryCache->setRegion('users', new Region(ttl: 120));
$scope = new Scope(namespace: 'ns');
$key = $queryCache->getCollectionKey($scope, 'users');
$cache->expects($this->exactly(2))
->method('load')
->willReturnCallback(function (string $cacheKey, int $ttl) use ($key): string|false {
return match (true) {
$cacheKey === $key.'#epoch' && $ttl === \PHP_INT_MAX => 'active:epoch@0',
\str_starts_with($cacheKey, $key.'#active:epoch:') && $ttl === 120 => false,
default => throw new \LogicException("Unexpected load of '{$cacheKey}' for {$ttl} seconds"),
};
});
$cache->method('getGeneration')->willReturn('0');
$entry = $queryCache->getEntry($scope, 'users', [Query::limit(10)]);
$this->assertNotNull($entry);
$this->assertStringStartsWith($key.'#active:epoch:', $entry->key);
$this->assertNull($queryCache->get($entry));
}
public function testGetPurgesMalformedPayload(): void
{
$cache = $this->createMock(Cache::class);
$queryCache = new QueryCache($cache);
$cache->method('load')->willReturn(['version' => 1, 'documents' => ['invalid']]);
$cache->expects($this->once())
->method('purge')
->with('entry-key')
->willReturn(true);
$this->assertNull($queryCache->get(new Entry('entry-key', 'users')));
}
public function testSetSerializesDocuments(): void
{
$cache = $this->createMock(Cache::class);
$queryCache = new QueryCache($cache);
$cache->expects($this->once())
->method('saveWithLease')
->with(
'entry-key',
$this->callback(function (array $data): bool {
$documents = $data['documents'] ?? null;
return ($data['version'] ?? null) === 1
&& \is_array($documents)
&& \is_array($documents[0] ?? null)
&& ($documents[0]['$id'] ?? null) === 'doc1';
}),
'',
'7',
)
->willReturnArgument(1);
$this->assertTrue($queryCache->set(
new Entry('entry-key', 'users'),
[new Document(['$id' => 'doc1', 'name' => 'Alice'])],
'7',
));
}
public function testInvalidateCollectionBlocksThenPublishesAFreshEpoch(): void
{
$cache = new InvalidationCache();
$queryCache = new QueryCache($cache);
$queryCache->invalidateCollection(new Scope(), 'users');
$this->assertInvalidated($cache, $queryCache->getCollectionKey(new Scope(), 'users'));
}
public function testEntriesResolveByDefault(): void
{
$this->assertNotNull($this->queryCache->getEntry(new Scope(), 'any', []));
}
public function testEntriesDoNotResolveWhenTheRegionIsDisabled(): void
{
$this->queryCache->setRegion('users', new Region(enabled: false));
$this->assertNull($this->queryCache->getEntry(new Scope(), 'users', []));
}
public function testFlushDelegatesToCacheFlush(): void
{
$cache = $this->createMock(Cache::class);
$queryCache = new QueryCache($cache);
$cache->expects($this->once())
->method('flush')
->willReturn(true);
$queryCache->flush();
}
public function testRegionDefaults(): void
{
$region = new Region();
$this->assertSame(3600, $region->ttl);
$this->assertTrue($region->enabled);
}
public function testRegionCustomValues(): void
{
$region = new Region(ttl: 120, enabled: false);
$this->assertSame(120, $region->ttl);
$this->assertFalse($region->enabled);
}
public function testInvalidatorInvalidatesOnDocumentCreate(): void
{
$this->assertInvalidatorInvalidates(Event::DocumentCreate, new Document(['$id' => 'doc1', '$collection' => 'users']), ['users']);
}
public function testInvalidatorInvalidatesOnDocumentUpdate(): void
{
$this->assertInvalidatorInvalidates(Event::DocumentUpdate, new Document(['$id' => 'doc1', '$collection' => 'posts']), ['posts']);
}
public function testInvalidatorInvalidatesOnDocumentDelete(): void
{
$this->assertInvalidatorInvalidates(Event::DocumentDelete, new Document(['$id' => 'doc1', '$collection' => 'users']), ['users']);
}
public function testInvalidatorIgnoresNonWriteEvents(): void
{
$cache = $this->createMock(Cache::class);
$invalidator = new Invalidator(new QueryCache($cache));
$cache->expects($this->never())->method('purge');
$invalidator->handle(Event::DocumentFind, new Document(['$id' => 'doc1', '$collection' => 'users']));
}
public function testInvalidatorExtractsCollectionFromDocument(): void
{
$this->assertInvalidatorInvalidates(Event::DocumentCreate, new Document(['$id' => 'doc1', '$collection' => 'orders']), ['orders']);
}
public function testInvalidatorHandlesStringData(): void
{
$this->assertInvalidatorInvalidates(Event::DocumentCreate, 'products', ['products']);
}
public function testInvalidatorIgnoresEmptyCollection(): void
{
$cache = $this->createMock(Cache::class);
$invalidator = new Invalidator(new QueryCache($cache));
$cache->expects($this->never())->method('purge');
$invalidator->handle(Event::DocumentCreate, new Document(['$id' => 'doc1']));
}
public function testInvalidatorInvalidatesBothRelationshipCollections(): void
{
$this->assertInvalidatorInvalidates(Event::AttributeCreate, new Document([
'$collection' => 'posts',
'options' => [
'relatedCollection' => 'authors',
],
]), ['posts', 'authors']);
}
public function testInvalidatorUsesCollectionIdentityForCollectionMutations(): void
{
$this->assertInvalidatorInvalidates(Event::CollectionUpdate, new Document([
'$id' => 'users',
'$collection' => Database::METADATA,
]), ['users']);
}
public function testInvalidatorInvalidatesTheScopeItIsGiven(): void
{
$cache = new InvalidationCache();
$queryCache = new QueryCache($cache);
$scope = new Scope('host', 'database', 'namespace', 7);
(new Invalidator($queryCache))->invalidate(Event::DocumentCreate, 'users', $scope);
$this->assertInvalidated($cache, $queryCache->getCollectionKey($scope, 'users'));
$this->assertSame(0, $cache->getPurges($queryCache->getCollectionKey(new Scope(), 'users').'#started'));
}
public function testInvalidatorHandlesEventsInTheScopeItWasGiven(): void
{
$cache = new InvalidationCache();
$queryCache = new QueryCache($cache);
$scope = new Scope(namespace: 'namespace', tenant: 'tenant');
(new Invalidator($queryCache, $scope))->handle(Event::DocumentCreate, 'users');
$this->assertInvalidated($cache, $queryCache->getCollectionKey($scope, 'users'));
}
public function testInvalidatorKeysTokensByTheScopedCollection(): void
{
$queryCache = new QueryCache(new InvalidationCache());
$scope = new Scope(namespace: 'namespace', tenant: 7);
$tokens = (new Invalidator($queryCache))->tokens(Event::DocumentCreate, 'users', $scope);
$this->assertSame([$queryCache->getCollectionKey($scope, 'users')], \array_keys($tokens));
}
public function testMemoryAdapterKeepsPhysicalVariantsIsolated(): void
{
$queryCache = new QueryCache(new Cache(new Memory()));
$first = $queryCache->getEntry(new Scope(namespace: 'ns'), 'users', [['limit' => 1]], 'role:user-a');
$second = $queryCache->getEntry(new Scope(namespace: 'ns'), 'users', [['limit' => 2]], 'role:user-b');
$this->assertNotNull($first);
$this->assertNotNull($second);
$this->assertTrue($queryCache->set($first, [new Document(['$id' => 'private-a'])], $queryCache->getGeneration($first)));
$this->assertTrue($queryCache->set($second, [new Document(['$id' => 'private-b'])], $queryCache->getGeneration($second)));
$this->assertSame(['private-a'], $this->ids($queryCache->get($first) ?? []));
$this->assertSame(['private-b'], $this->ids($queryCache->get($second) ?? []));
}
public function testMemoryAdapterStaysBlockedAfterInvalidation(): void
{
$queryCache = new QueryCache(new Cache(new Memory()));
$scope = new Scope(namespace: 'ns');
$entry = $queryCache->getEntry($scope, 'users', []);
$this->assertNotNull($entry);
$this->assertTrue($queryCache->set($entry, [new Document(['$id' => 'old'])], $queryCache->getGeneration($entry)));
$this->assertSame(['old'], $this->ids($queryCache->get($entry) ?? []));
$queryCache->invalidateCollection($scope, 'users');
$this->assertNull(
$queryCache->getEntry($scope, 'users', []),
'A cache without generations cannot prove a new epoch fresh, so it stays blocked',
);
}
public function testConcurrentOwnersCannotEnableCacheEarly(): void
{
$scope = new Scope(namespace: 'ns');
for ($iteration = 0; $iteration < 10; $iteration++) {
$adapter = new OwnershipCache();
$first = new QueryCache(new Cache($adapter));
$second = new QueryCache(new Cache($adapter));
$reader = new QueryCache(new Cache($adapter));
$key = $reader->getCollectionKey($scope, 'users');
$firstToken = 'first-'.$iteration;
$secondToken = 'second-'.$iteration;
$entry = $reader->getEntry($scope, 'users', []);
$this->assertNotNull($entry);
$this->assertTrue($reader->set($entry, [new Document(['$id' => 'old'])], $reader->getGeneration($entry)));
$this->assertSame(['old'], $this->ids((new QueryCache(new Cache($adapter)))->get($entry) ?? []));
$first->blockCollection($key, $firstToken);
$adapter->pauseNextActivation(function () use ($adapter, $reader, $second, $scope, $key, $secondToken): void {
$second->blockCollection($key, $secondToken);
$this->assertNull($reader->getEntry($scope, 'users', []));
$this->assertTrue($adapter->has($key.'#owner:'.$secondToken));
});
$first->activateCollection($key, $firstToken);
$this->assertFalse($adapter->has($key.'#owner:'.$firstToken));
$this->assertTrue($adapter->has($key.'#owner:'.$secondToken));
$this->assertNull($reader->getEntry($scope, 'users', []));
$second->activateCollection($key, $secondToken);
$this->assertFalse($adapter->has($key.'#owner:'.$secondToken));
$fresh = $reader->getEntry($scope, 'users', []);
$this->assertNotNull($fresh);
$this->assertNull($reader->get($fresh));
$this->assertTrue($reader->set($fresh, [new Document(['$id' => 'fresh'])], $reader->getGeneration($fresh)));
$this->assertSame(['fresh'], $this->ids((new QueryCache(new Cache($adapter)))->get($fresh) ?? []));
}
}
public function testAnEpochPublishedAfterALaterFinishStaysUsable(): void
{
$adapter = new OwnershipCache();
$queryCache = new QueryCache(new Cache($adapter));
$scope = new Scope(namespace: 'ns');
$key = $queryCache->getCollectionKey($scope, 'users');
$queryCache->blockCollection($key, 'first');
$adapter->pauseNextActivation(function () use ($queryCache, $key): void {
$queryCache->blockCollection($key, 'second');
$queryCache->activateCollection($key, 'second');
});
$queryCache->activateCollection($key, 'first');
$entry = $queryCache->getEntry($scope, 'users', []);
$this->assertNotNull($entry, 'Once every writer has finished, an epoch published late must still be usable');
$this->assertTrue($queryCache->set($entry, [new Document(['$id' => 'fresh'])], $queryCache->getGeneration($entry)));
$this->assertSame(['fresh'], $this->ids($queryCache->get($entry) ?? []));
}
public function testATombstoneOlderThanItsRegionStillBlocksWhileItsWriterIsInFlight(): void
{
$adapter = new OwnershipCache();
$queryCache = new QueryCache(new Cache($adapter));
$queryCache->setRegion('users', new Region(ttl: 0));
$key = $queryCache->getCollectionKey(new Scope(), 'users');
$queryCache->blockCollection($key, 'writer');
$this->assertNull(
$queryCache->getEntry(new Scope(), 'users', []),
'A transaction that outlives the region TTL must keep readers off the cache until it activates',
);
$queryCache->activateCollection($key, 'writer');
$this->assertStringStartsWith('active:', $this->epochOf($adapter, $key), 'The writer must still own its tombstone and publish a fresh epoch');
}
public function testATombstoneOnACacheWithoutGenerationsLapsesWithItsRegion(): void
{
$queryCache = new QueryCache(new Cache(new Memory()));
$queryCache->setRegion('users', new Region(ttl: 0));
$queryCache->invalidateCollection(new Scope(), 'users');
$this->assertNotNull(
$queryCache->getEntry(new Scope(), 'users', []),
'Without generations a tombstone is the only guard, and it must lapse with its region',
);
}
public function testCacheFlushDuringActivationDoesNotFailInvalidation(): void
{
$adapter = new OwnershipCache();
$queryCache = new QueryCache(new Cache($adapter));
$key = $queryCache->getCollectionKey(new Scope(), 'users');
$queryCache->blockCollection($key, 'owner');
$adapter->flushDuringActivation();
$queryCache->activateCollection($key, 'owner');
$this->assertNotNull($queryCache->getEntry(new Scope(), 'users', []));
}
public function testCacheFlushBeforeActivationDoesNotFailInvalidation(): void
{
$adapter = new OwnershipCache();
$queryCache = new QueryCache(new Cache($adapter));
$key = $queryCache->getCollectionKey(new Scope(), 'users');
$queryCache->blockCollection($key, 'owner');
$this->assertTrue($adapter->flush());
$queryCache->activateCollection($key, 'owner');
$this->assertNotNull($queryCache->getEntry(new Scope(), 'users', []));
}
public function testActivationPurgeFailureStillPropagates(): void
{
$adapter = new OwnershipCache();
$queryCache = new QueryCache(new Cache($adapter));
$key = $queryCache->getCollectionKey(new Scope(), 'users');
$queryCache->blockCollection($key, 'owner');
$adapter->failDuringActivation();
try {
$queryCache->activateCollection($key, 'owner');
$this->fail('Query cache activation purge failure was not propagated');
} catch (\RuntimeException $error) {
$this->assertStringContainsString('finish query cache invalidation', $error->getMessage());
}
$this->assertNull($queryCache->getEntry(new Scope(), 'users', []));
}
public function testInvalidationsDoNotAddKeysToACacheThatKeepsPurgedKeys(): void
{
$adapter = new RedisLeasableCache();
$queryCache = new QueryCache(new Cache($adapter));
$scope = new Scope(namespace: 'ns');
$queryCache->invalidateCollection($scope, 'users');
$keys = $adapter->keys();
for ($invalidation = 0; $invalidation < 20; $invalidation++) {
$queryCache->invalidateCollection($scope, 'users');
}
$this->assertSame($keys, $adapter->keys(), 'A purged key stays behind in Redis, so an invalidation must not leave a key of its own');
$this->assertNotNull($queryCache->getEntry($scope, 'users', []));
}
public function testOverlappingInvalidationsSucceedOnACacheWithoutFields(): void
{
$queryCache = new QueryCache(new Cache(new Memory()));
$key = $queryCache->getCollectionKey(new Scope(), 'users');
$queryCache->blockCollection($key, 'first');
$queryCache->blockCollection($key, 'second');
$queryCache->activateCollection($key, 'first');
$this->assertNull($queryCache->getEntry(new Scope(), 'users', []), 'The second writer is still in flight');
$queryCache->activateCollection($key, 'second');
$this->assertNull(
$queryCache->getEntry(new Scope(), 'users', []),
'A cache without generations cannot prove a new epoch fresh, so it stays blocked',
);
}
public function testActivationRejectsACorruptedOwnerRegistration(): void
{
$adapter = new RedisLeasableCache();
$queryCache = new QueryCache(new Cache($adapter));
$key = $queryCache->getCollectionKey(new Scope(), 'users');
$adapter->corruptFieldWrites();
$queryCache->blockCollection($key, 'owner');
try {
$queryCache->activateCollection($key, 'owner');
$this->fail('A corrupted owner registration was accepted');
} catch (\RuntimeException $error) {
$this->assertStringContainsString('Invalid query cache owner', $error->getMessage());
}
$this->assertNull($queryCache->getEntry(new Scope(), 'users', []));
}
public function testActivationPropagatesAnOwnerReleaseFailure(): void
{
$adapter = new RedisLeasableCache();
$queryCache = new QueryCache(new Cache($adapter));
$key = $queryCache->getCollectionKey(new Scope(), 'users');
$queryCache->blockCollection($key, 'owner');
$adapter->failFieldPurges();
try {
$queryCache->activateCollection($key, 'owner');
$this->fail('An owner release failure was not propagated');
} catch (\RuntimeException $error) {
$this->assertStringContainsString('Failed to release query cache owner', $error->getMessage());
}
$this->assertNull($queryCache->getEntry(new Scope(), 'users', []));
}
public function testInvalidationPropagatesACacheWriteFailure(): void
{
$cache = new InvalidationCache();
$queryCache = new QueryCache($cache);
$cache->fail($queryCache->getCollectionKey(new Scope(), 'users').'#epoch');
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Failed to block query cache epoch');
$queryCache->invalidateCollection(new Scope(), 'users');
}
/**
* @param array<string> $collections
*/
private function assertInvalidatorInvalidates(Event $event, mixed $data, array $collections): void
{
$cache = new InvalidationCache();
$queryCache = new QueryCache($cache);
(new Invalidator($queryCache))->handle($event, $data);
foreach ($collections as $collection) {
$this->assertInvalidated($cache, $queryCache->getCollectionKey(new Scope(), $collection));
}
}
private function assertInvalidated(InvalidationCache $cache, string $key): void
{
$this->assertSame(1, $cache->getPurges($key.'#started'), 'The invalidation must block the collection once');
$this->assertSame(1, $cache->getPurges($key.'#finished'), 'The invalidation must finish what it blocked');
$this->assertStringStartsWith('active:', $cache->values[$key.'#epoch'] ?? '', 'The invalidation must publish a fresh epoch');
}
private static function createCache(): Cache&Stub
{
$cache = self::createStub(Cache::class);
$cache->method('getGeneration')->willReturn('0');
return $cache;
}
private function epochOf(OwnershipCache $adapter, string $key): string
{
$epoch = $adapter->load($key.'#epoch', \PHP_INT_MAX);
$this->assertIsString($epoch);
return $epoch;
}
/**
* @param array<Document> $documents
* @return array<string>
*/
private function ids(array $documents): array
{
return \array_map(
static fn (Document $document): string => $document->getId(),
$documents,
);
}
}