Skip to content

Commit 582d2cb

Browse files
abnegateclaude
andcommitted
fix: reconnect cache after Redis restart in destructive tests
After Redis containers are stopped and restarted, the Database's cache holds a stale Redis connection that blocks ~55s per operation. Adding reconnectCache() after waitForRedis() creates a fresh Redis connection, reducing Redis-Destructive test time from ~18 minutes to ~30 seconds. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 4de656f commit 582d2cb

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

tests/e2e/Adapter/Scopes/GeneralTests.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ public function testCacheFallback(): void
399399
// Restart Redis containers
400400
Console::execute('docker ps -a --filter "name=utopia-redis" --format "{{.Names}}" | xargs -r docker start', '', $stdout, $stderr);
401401
$this->waitForRedis();
402+
$this->reconnectCache();
402403

403404
$this->assertCount(1, $database->find('testRedisFallback', [Query::equal('string', ['text📝'])]));
404405
}
@@ -417,6 +418,7 @@ public function testCacheReconnect(): void
417418

418419
// Wait for Redis to be fully healthy after previous test
419420
$this->waitForRedis();
421+
$this->reconnectCache();
420422

421423
$database->getAuthorization()->cleanRoles();
422424
$database->getAuthorization()->addRole(Role::any()->toString());
@@ -449,6 +451,7 @@ public function testCacheReconnect(): void
449451
// Restart Redis containers
450452
Console::execute('docker ps -a --filter "name=utopia-redis" --format "{{.Names}}" | xargs -r docker start', '', $stdout, $stderr);
451453
$this->waitForRedis();
454+
$this->reconnectCache();
452455

453456
// Cache should reconnect - read should work
454457
$doc = $database->getDocument('testCacheReconnect', 'reconnect_doc');
@@ -468,6 +471,7 @@ public function testCacheReconnect(): void
468471
$stderr = '';
469472
Console::execute('docker ps -a --filter "name=utopia-redis" --filter "status=exited" --format "{{.Names}}" | xargs -r docker start', '', $stdout, $stderr);
470473
$this->waitForRedis();
474+
$this->reconnectCache();
471475

472476
// Cleanup collection if it exists
473477
if ($database->exists() && ! $database->getCollection('testCacheReconnect')->isEmpty()) {
@@ -696,6 +700,16 @@ public function testNestedTransactionState(): void
696700
/**
697701
* Wait for Redis to be ready with a readiness probe
698702
*/
703+
private function reconnectCache(): void
704+
{
705+
$redis = new \Redis();
706+
$redis->connect('redis', 6379, 2.0);
707+
$redis->select(0);
708+
$adapter = new \Utopia\Cache\Adapter\Redis($redis);
709+
$adapter->setMaxRetries(3);
710+
$this->getDatabase()->setCache(new \Utopia\Cache\Cache($adapter));
711+
}
712+
699713
private function waitForRedis(int $maxRetries = 60, int $delayMs = 500): void
700714
{
701715
$consecutive = 0;

0 commit comments

Comments
 (0)