22
33namespace Tests \E2E \Adapter \Scopes ;
44
5- use PHPUnit \Framework \Attributes \Group ;
6- use Utopia \Console ;
75use Utopia \Database \Attribute ;
86use Utopia \Database \Capability ;
97use Utopia \Database \Database ;
@@ -337,8 +335,7 @@ public function testSharedTablesTenantPerDocument(): void
337335 ->setDatabase ($ schema );
338336 }
339337
340- #[Group('redis-destructive ' )]
341- public function testCacheFallback (): void
338+ public function testCacheFallbackOnFailure (): void
342339 {
343340 /** @var Database $database */
344341 $ database = $ this ->getDatabase ();
@@ -349,59 +346,42 @@ public function testCacheFallback(): void
349346 return ;
350347 }
351348
352- $ this ->getDatabase ()->getAuthorization ()->cleanRoles ();
353- $ this ->getDatabase ()->getAuthorization ()->addRole (Role::any ()->toString ());
349+ $ collection = 'cacheFallback_ ' .uniqid ();
354350
355- // Write mock data
356- $ database ->createCollection ('testRedisFallback ' , attributes: [
357- new Attribute (key: 'string ' , type: ColumnType::String, size: 767 , required: true ),
351+ $ database ->createCollection ($ collection , attributes: [
352+ new Attribute (key: 'title ' , type: ColumnType::String, size: 128 , required: true ),
358353 ], permissions: [
359354 Permission::read (Role::any ()),
360355 Permission::create (Role::any ()),
361- Permission::update (Role::any ()),
362- Permission::delete (Role::any ()),
363356 ]);
364357
365- $ database ->createDocument (' testRedisFallback ' , new Document ([
358+ $ database ->createDocument ($ collection , new Document ([
366359 '$id ' => 'doc1 ' ,
367- 'string ' => 'text📝 ' ,
360+ 'title ' => 'hello ' ,
368361 ]));
369362
370- $ database ->createIndex ('testRedisFallback ' , new Index (key: 'index1 ' , type: IndexType::Key, attributes: ['string ' ]));
371- $ this ->assertCount (1 , $ database ->find ('testRedisFallback ' , [Query::equal ('string ' , ['text📝 ' ])]));
372-
373- // Bring down Redis
374- $ stdout = '' ;
375- $ stderr = '' ;
376- Console::execute ('docker ps -a --filter "name=utopia-redis" --format "{{.Names}}" | xargs -r docker stop -t 0 ' , '' , $ stdout , $ stderr );
363+ $ this ->assertCount (1 , $ database ->find ($ collection ));
377364
378- // Check we can read data still
379- $ this ->assertCount (1 , $ database ->find ('testRedisFallback ' , [Query::equal ('string ' , ['text📝 ' ])]));
380- $ this ->assertFalse (($ database ->getDocument ('testRedisFallback ' , 'doc1 ' ))->isEmpty ());
365+ $ brokenRedis = $ this ->createMock (\Redis::class);
366+ $ brokenRedis ->method ('get ' )->willThrowException (new \RedisException ('gone ' ));
367+ $ brokenRedis ->method ('set ' )->willThrowException (new \RedisException ('gone ' ));
368+ $ brokenRedis ->method ('del ' )->willThrowException (new \RedisException ('gone ' ));
369+ $ brokenRedis ->method ('expire ' )->willThrowException (new \RedisException ('gone ' ));
381370
382- // Check we cannot modify data (error message varies: "went away", DNS failure, connection refused)
383- try {
384- $ database ->updateDocument ('testRedisFallback ' , 'doc1 ' , new Document ([
385- 'string ' => 'text📝 updated ' ,
386- ]));
387- $ this ->fail ('Failed to throw exception ' );
388- } catch (\Throwable $ e ) {
389- $ this ->assertInstanceOf (\RedisException::class, $ e );
390- }
371+ $ brokenAdapter = new \Utopia \Cache \Adapter \Redis ($ brokenRedis );
372+ $ brokenAdapter ->setMaxRetries (0 );
373+ $ originalCache = $ database ->getCache ();
374+ $ database ->setCache (new \Utopia \Cache \Cache ($ brokenAdapter ));
391375
392- try {
393- $ database ->deleteDocument ('testRedisFallback ' , 'doc1 ' );
394- $ this ->fail ('Failed to throw exception ' );
395- } catch (\Throwable $ e ) {
396- $ this ->assertInstanceOf (\RedisException::class, $ e );
397- }
376+ $ doc = $ database ->getDocument ($ collection , 'doc1 ' );
377+ $ this ->assertFalse ($ doc ->isEmpty ());
378+ $ this ->assertEquals ('hello ' , $ doc ->getAttribute ('title ' ));
398379
399- // Restart Redis containers
400- Console::execute ('docker ps -a --filter "name=utopia-redis" --format "{{.Names}}" | xargs -r docker start ' , '' , $ stdout , $ stderr );
401- sleep (2 );
402- $ this ->reconnectCache ();
380+ $ results = $ database ->find ($ collection );
381+ $ this ->assertCount (1 , $ results );
403382
404- $ this ->assertCount (1 , $ database ->find ('testRedisFallback ' , [Query::equal ('string ' , ['text📝 ' ])]));
383+ $ database ->setCache ($ originalCache );
384+ $ database ->deleteCollection ($ collection );
405385 }
406386
407387 /**
@@ -624,15 +604,4 @@ public function testNestedTransactionState(): void
624604 /**
625605 * Wait for Redis to be ready with a readiness probe
626606 */
627- private function reconnectCache (): void
628- {
629- $ redis = new \Redis ();
630- $ redis ->connect ('redis ' , 6379 , 2.0 );
631- $ redis ->setOption (\Redis::OPT_READ_TIMEOUT , 5 );
632- $ redis ->select (0 );
633- $ adapter = new \Utopia \Cache \Adapter \Redis ($ redis );
634- $ adapter ->setMaxRetries (3 );
635- $ this ->getDatabase ()->setCache (new \Utopia \Cache \Cache ($ adapter ));
636- }
637-
638607}
0 commit comments