@@ -711,7 +711,7 @@ public function testCacheReconnect(): void
711711 }
712712
713713 // Wait for Redis to be fully healthy after previous test
714- sleep ( 3 );
714+ $ this -> waitForRedis ( );
715715
716716 // Create new cache with reconnection enabled
717717 $ redis = new \Redis ();
@@ -733,53 +733,80 @@ public function testCacheReconnect(): void
733733 $ database ->getAuthorization ()->cleanRoles ();
734734 $ database ->getAuthorization ()->addRole (Role::any ()->toString ());
735735
736- $ database ->createCollection ('testCacheReconnect ' , attributes: [
737- new Document ([
738- '$id ' => ID ::custom ('title ' ),
739- 'type ' => Database::VAR_STRING ,
740- 'size ' => 255 ,
741- 'required ' => true ,
742- ])
743- ], permissions: [
744- Permission::read (Role::any ()),
745- Permission::create (Role::any ()),
746- Permission::update (Role::any ()),
747- Permission::delete (Role::any ())
748- ]);
736+ try {
737+ $ database ->createCollection ('testCacheReconnect ' , attributes: [
738+ new Document ([
739+ '$id ' => ID ::custom ('title ' ),
740+ 'type ' => Database::VAR_STRING ,
741+ 'size ' => 255 ,
742+ 'required ' => true ,
743+ ])
744+ ], permissions: [
745+ Permission::read (Role::any ()),
746+ Permission::create (Role::any ()),
747+ Permission::update (Role::any ()),
748+ Permission::delete (Role::any ())
749+ ]);
749750
750- $ database ->createDocument ('testCacheReconnect ' , new Document ([
751- '$id ' => 'reconnect_doc ' ,
752- 'title ' => 'Test Document ' ,
753- ]));
751+ $ database ->createDocument ('testCacheReconnect ' , new Document ([
752+ '$id ' => 'reconnect_doc ' ,
753+ 'title ' => 'Test Document ' ,
754+ ]));
754755
755- // Cache the document
756- $ doc = $ database ->getDocument ('testCacheReconnect ' , 'reconnect_doc ' );
757- $ this ->assertEquals ('Test Document ' , $ doc ->getAttribute ('title ' ));
756+ // Cache the document
757+ $ doc = $ database ->getDocument ('testCacheReconnect ' , 'reconnect_doc ' );
758+ $ this ->assertEquals ('Test Document ' , $ doc ->getAttribute ('title ' ));
758759
759- // Bring down Redis
760- $ stdout = '' ;
761- $ stderr = '' ;
762- Console::execute ('docker ps -a --filter "name=utopia-redis" --format "{{.Names}}" | xargs -r docker stop ' , "" , $ stdout , $ stderr );
763- sleep (1 );
760+ // Bring down Redis
761+ $ stdout = '' ;
762+ $ stderr = '' ;
763+ Console::execute ('docker ps -a --filter "name=utopia-redis" --format "{{.Names}}" | xargs -r docker stop ' , "" , $ stdout , $ stderr );
764+ sleep (1 );
764765
765- // Bring back Redis
766- Console::execute ('docker ps -a --filter "name=utopia-redis" --format "{{.Names}}" | xargs -r docker start ' , "" , $ stdout , $ stderr );
767- sleep ( 3 );
766+ // Bring back Redis
767+ Console::execute ('docker ps -a --filter "name=utopia-redis" --format "{{.Names}}" | xargs -r docker start ' , "" , $ stdout , $ stderr );
768+ $ this -> waitForRedis ( );
768769
769- // Cache should reconnect - read should work
770- $ doc = $ database ->getDocument ('testCacheReconnect ' , 'reconnect_doc ' );
771- $ this ->assertEquals ('Test Document ' , $ doc ->getAttribute ('title ' ));
770+ // Cache should reconnect - read should work
771+ $ doc = $ database ->getDocument ('testCacheReconnect ' , 'reconnect_doc ' );
772+ $ this ->assertEquals ('Test Document ' , $ doc ->getAttribute ('title ' ));
772773
773- // Update should work after reconnect
774- $ database ->updateDocument ('testCacheReconnect ' , 'reconnect_doc ' , new Document ([
775- '$id ' => 'reconnect_doc ' ,
776- 'title ' => 'Updated Title ' ,
777- ]));
774+ // Update should work after reconnect
775+ $ database ->updateDocument ('testCacheReconnect ' , 'reconnect_doc ' , new Document ([
776+ '$id ' => 'reconnect_doc ' ,
777+ 'title ' => 'Updated Title ' ,
778+ ]));
778779
779- $ doc = $ database ->getDocument ('testCacheReconnect ' , 'reconnect_doc ' );
780- $ this ->assertEquals ('Updated Title ' , $ doc ->getAttribute ('title ' ));
780+ $ doc = $ database ->getDocument ('testCacheReconnect ' , 'reconnect_doc ' );
781+ $ this ->assertEquals ('Updated Title ' , $ doc ->getAttribute ('title ' ));
782+ } finally {
783+ // Ensure Redis is running
784+ $ stdout = '' ;
785+ $ stderr = '' ;
786+ Console::execute ('docker ps -a --filter "name=utopia-redis" --format "{{.Names}}" | xargs -r docker start ' , "" , $ stdout , $ stderr );
787+ $ this ->waitForRedis ();
788+
789+ // Cleanup collection if it exists
790+ if ($ database ->exists () && !$ database ->getCollection ('testCacheReconnect ' )->isEmpty ()) {
791+ $ database ->deleteCollection ('testCacheReconnect ' );
792+ }
793+ }
794+ }
781795
782- // Cleanup
783- $ database ->deleteCollection ('testCacheReconnect ' );
796+ /**
797+ * Wait for Redis to be ready with a readiness probe
798+ */
799+ private function waitForRedis (int $ maxRetries = 10 , int $ delayMs = 500 ): void
800+ {
801+ for ($ i = 0 ; $ i < $ maxRetries ; $ i ++) {
802+ try {
803+ $ redis = new \Redis ();
804+ $ redis ->connect ('redis ' , 6379 );
805+ $ redis ->ping ();
806+ return ;
807+ } catch (\RedisException $ e ) {
808+ usleep ($ delayMs * 1000 );
809+ }
810+ }
784811 }
785812}
0 commit comments