Skip to content

Commit ef186b6

Browse files
committed
Add cache reconnection test
1 parent 27ae55c commit ef186b6

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

tests/e2e/Adapter/Scopes/GeneralTests.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Exception;
66
use Throwable;
7+
use Utopia\Cache\Adapter\Redis as RedisAdapter;
8+
use Utopia\Cache\Cache;
79
use Utopia\CLI\Console;
810
use Utopia\Database\Database;
911
use Utopia\Database\Document;
@@ -697,6 +699,72 @@ public function testCacheFallback(): void
697699
$this->assertCount(1, $database->find('testRedisFallback', [Query::equal('string', ['text📝'])]));
698700
}
699701

702+
public function testCacheReconnect(): void
703+
{
704+
/** @var Database $database */
705+
$database = $this->getDatabase();
706+
707+
if (!$database->getAdapter()->getSupportForCacheSkipOnFailure()) {
708+
$this->expectNotToPerformAssertions();
709+
return;
710+
}
711+
712+
// Create new cache with reconnection enabled
713+
$redis = new \Redis();
714+
$redis->connect('redis', 6379);
715+
$cache = new Cache((new RedisAdapter($redis))->setMaxRetries(2));
716+
$database->setCache($cache);
717+
718+
$database->getAuthorization()->cleanRoles();
719+
$database->getAuthorization()->addRole(Role::any()->toString());
720+
721+
$database->createCollection('testCacheReconnect', attributes: [
722+
new Document([
723+
'$id' => ID::custom('title'),
724+
'type' => Database::VAR_STRING,
725+
'size' => 255,
726+
'required' => true,
727+
])
728+
], permissions: [
729+
Permission::read(Role::any()),
730+
Permission::create(Role::any()),
731+
Permission::update(Role::any()),
732+
Permission::delete(Role::any())
733+
]);
734+
735+
$database->createDocument('testCacheReconnect', new Document([
736+
'$id' => 'reconnect_doc',
737+
'title' => 'Test Document',
738+
]));
739+
740+
// Cache the document
741+
$doc = $database->getDocument('testCacheReconnect', 'reconnect_doc');
742+
$this->assertEquals('Test Document', $doc->getAttribute('title'));
743+
744+
// Bring down Redis
745+
$stdout = '';
746+
$stderr = '';
747+
Console::execute('docker ps -a --filter "name=utopia-redis" --format "{{.Names}}" | xargs -r docker stop', "", $stdout, $stderr);
748+
sleep(1);
749+
750+
// Bring back Redis
751+
Console::execute('docker ps -a --filter "name=utopia-redis" --format "{{.Names}}" | xargs -r docker start', "", $stdout, $stderr);
752+
sleep(3);
700753

754+
// Cache should reconnect - read should work
755+
$doc = $database->getDocument('testCacheReconnect', 'reconnect_doc');
756+
$this->assertEquals('Test Document', $doc->getAttribute('title'));
701757

758+
// Update should work after reconnect
759+
$database->updateDocument('testCacheReconnect', 'reconnect_doc', new Document([
760+
'$id' => 'reconnect_doc',
761+
'title' => 'Updated Title',
762+
]));
763+
764+
$doc = $database->getDocument('testCacheReconnect', 'reconnect_doc');
765+
$this->assertEquals('Updated Title', $doc->getAttribute('title'));
766+
767+
// Cleanup
768+
$database->deleteCollection('testCacheReconnect');
769+
}
702770
}

0 commit comments

Comments
 (0)