From 398bbd12d58c94da27d332a25cdd5c690e879d43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 28 Aug 2023 13:00:53 +0200 Subject: [PATCH] Fix tests on ChillDocumentLockManager: take expiration delay into account --- .../Service/Wopi/ChillDocumentLockManager.php | 8 +++--- .../Wopi/ChillDocumentLockManagerTest.php | 27 ++++--------------- 2 files changed, 10 insertions(+), 25 deletions(-) diff --git a/src/Bundle/ChillWopiBundle/src/Service/Wopi/ChillDocumentLockManager.php b/src/Bundle/ChillWopiBundle/src/Service/Wopi/ChillDocumentLockManager.php index 148b1fc8e..9626a885b 100644 --- a/src/Bundle/ChillWopiBundle/src/Service/Wopi/ChillDocumentLockManager.php +++ b/src/Bundle/ChillWopiBundle/src/Service/Wopi/ChillDocumentLockManager.php @@ -26,8 +26,10 @@ class ChillDocumentLockManager implements DocumentLockManagerInterface */ private const LOCK_GRACEFUL_DURATION_TIME = 3; - public function __construct(private readonly ChillRedis $redis) - { + public function __construct( + private readonly ChillRedis $redis, + private int $ttlAfterDeleteSeconds = self::LOCK_GRACEFUL_DURATION_TIME + ) { } public function deleteLock(Document $document, RequestInterface $request): bool @@ -38,7 +40,7 @@ class ChillDocumentLockManager implements DocumentLockManagerInterface // some queries (ex.: putFile) may be executed on the same time than the unlock, so // we add a delay before unlocking the file, instead of deleting it immediatly - return $this->redis->expire($this->getCacheId($document), self::LOCK_GRACEFUL_DURATION_TIME); + return $this->redis->expire($this->getCacheId($document), $this->ttlAfterDeleteSeconds); } public function getLock(Document $document, RequestInterface $request): string diff --git a/src/Bundle/ChillWopiBundle/tests/Service/Wopi/ChillDocumentLockManagerTest.php b/src/Bundle/ChillWopiBundle/tests/Service/Wopi/ChillDocumentLockManagerTest.php index a3edddea6..022f5e26f 100644 --- a/src/Bundle/ChillWopiBundle/tests/Service/Wopi/ChillDocumentLockManagerTest.php +++ b/src/Bundle/ChillWopiBundle/tests/Service/Wopi/ChillDocumentLockManagerTest.php @@ -31,27 +31,6 @@ final class ChillDocumentLockManagerTest extends KernelTestCase self::bootKernel(); } - public function testMultipleLoops() - { - $manager = $this->makeManager(1); - $document = new StoredObject(); - $request = $this->prophesize(RequestInterface::class); - - $i = 0; - - while (50 > ++$i) { - $this->assertFalse($manager->hasLock($document, $request->reveal())); - - $this->assertTrue($manager->setLock($document, 'dummy', $request->reveal())); - - $this->assertEquals('dummy', $manager->getLock($document, $request->reveal())); - - $this->assertTrue($manager->deleteLock($document, $request->reveal())); - - $this->assertFalse($manager->hasLock($document, $request->reveal())); - } - } - public function testRelock() { $manager = $this->makeManager(1); @@ -70,6 +49,8 @@ final class ChillDocumentLockManagerTest extends KernelTestCase $this->assertTrue($manager->deleteLock($document, $request->reveal())); + sleep(3); // wait for redis to remove the key + $this->assertFalse($manager->hasLock($document, $request->reveal())); } @@ -87,10 +68,12 @@ final class ChillDocumentLockManagerTest extends KernelTestCase $this->assertTrue($manager->deleteLock($document, $request->reveal())); + sleep(3); // wait for redis to remove the key + $this->assertFalse($manager->hasLock($document, $request->reveal())); } - private function makeManager(int $ttlAfterDeleteSeconds): ChillDocumentLockManager + private function makeManager(int $ttlAfterDeleteSeconds = -1): ChillDocumentLockManager { $redis = self::$container->get(ChillRedis::class);