From 07151b20b048e0f44cd124bb750d7ae18236737e Mon Sep 17 00:00:00 2001 From: Joost Date: Tue, 5 Dec 2023 13:29:04 +0100 Subject: [PATCH 1/3] Add function to validate if the lock is owned by the current owner. --- src/Illuminate/Cache/Lock.php | 11 +++++++++++ src/Illuminate/Contracts/Cache/Lock.php | 7 +++++++ 2 files changed, 18 insertions(+) diff --git a/src/Illuminate/Cache/Lock.php b/src/Illuminate/Cache/Lock.php index ccd1c6474a5f..247f825de423 100644 --- a/src/Illuminate/Cache/Lock.php +++ b/src/Illuminate/Cache/Lock.php @@ -153,6 +153,17 @@ protected function isOwnedByCurrentProcess() return $this->getCurrentOwner() === $this->owner; } + + /** + * Determine if this lock is owned by the current owner. + * + * @return bool + */ + public function isOwner() + { + return $this->isOwnedByCurrentProcess(); + } + /** * Specify the number of milliseconds to sleep in between blocked lock acquisition attempts. * diff --git a/src/Illuminate/Contracts/Cache/Lock.php b/src/Illuminate/Contracts/Cache/Lock.php index 03f633a07a21..adf078057143 100644 --- a/src/Illuminate/Contracts/Cache/Lock.php +++ b/src/Illuminate/Contracts/Cache/Lock.php @@ -35,6 +35,13 @@ public function release(); */ public function owner(); + /** + * Determine if this lock is owned by the current owner. + * + * @return bool + */ + public function isOwner(); + /** * Releases this lock in disregard of ownership. * From cd036db71d30b000b66a014a290fc798912f645b Mon Sep 17 00:00:00 2001 From: Joost Date: Thu, 7 Dec 2023 09:46:34 +0100 Subject: [PATCH 2/3] make Lock->isOwnedByCurrentProcess public remove change to contract and add tests --- src/Illuminate/Cache/Lock.php | 13 +--------- src/Illuminate/Contracts/Cache/Lock.php | 7 ------ tests/Integration/Cache/FileCacheLockTest.php | 24 +++++++++++++++++++ .../Cache/MemcachedCacheLockTestCase.php | 23 ++++++++++++++++++ .../Integration/Cache/RedisCacheLockTest.php | 23 ++++++++++++++++++ 5 files changed, 71 insertions(+), 19 deletions(-) diff --git a/src/Illuminate/Cache/Lock.php b/src/Illuminate/Cache/Lock.php index 247f825de423..4868fdf82a50 100644 --- a/src/Illuminate/Cache/Lock.php +++ b/src/Illuminate/Cache/Lock.php @@ -148,22 +148,11 @@ public function owner() * * @return bool */ - protected function isOwnedByCurrentProcess() + public function isOwnedByCurrentProcess() { return $this->getCurrentOwner() === $this->owner; } - - /** - * Determine if this lock is owned by the current owner. - * - * @return bool - */ - public function isOwner() - { - return $this->isOwnedByCurrentProcess(); - } - /** * Specify the number of milliseconds to sleep in between blocked lock acquisition attempts. * diff --git a/src/Illuminate/Contracts/Cache/Lock.php b/src/Illuminate/Contracts/Cache/Lock.php index adf078057143..03f633a07a21 100644 --- a/src/Illuminate/Contracts/Cache/Lock.php +++ b/src/Illuminate/Contracts/Cache/Lock.php @@ -35,13 +35,6 @@ public function release(); */ public function owner(); - /** - * Determine if this lock is owned by the current owner. - * - * @return bool - */ - public function isOwner(); - /** * Releases this lock in disregard of ownership. * diff --git a/tests/Integration/Cache/FileCacheLockTest.php b/tests/Integration/Cache/FileCacheLockTest.php index 98ef814d212d..341eb911d9f5 100644 --- a/tests/Integration/Cache/FileCacheLockTest.php +++ b/tests/Integration/Cache/FileCacheLockTest.php @@ -95,4 +95,28 @@ public function testLocksCanBeReleasedUsingOwnerToken() $this->assertTrue(Cache::lock('foo')->get()); } + + public function testOwnerStatusCanBeCheckedAfterRestoringLock() + { + Cache::lock('foo')->forceRelease(); + + $firstLock = Cache::lock('foo', 10); + $this->assertTrue($firstLock->get()); + $owner = $firstLock->owner(); + + $secondLock = Cache::store('file')->restoreLock('foo', $owner); + $this->assertTrue($secondLock->isOwnedByCurrentProcess()); + } + + public function testOtherOwnerDoesNotOwnLockAfterRestore() + { + Cache::lock('foo')->forceRelease(); + + $firstLock = Cache::lock('foo', 10); + $this->assertTrue($firstLock->get()); + + $secondLock = Cache::store('file')->restoreLock('foo', 'other_owner'); + $this->assertFalse($secondLock->isOwnedByCurrentProcess()); + + } } diff --git a/tests/Integration/Cache/MemcachedCacheLockTestCase.php b/tests/Integration/Cache/MemcachedCacheLockTestCase.php index 7f3e93f8c7c0..8dbbd481f17e 100644 --- a/tests/Integration/Cache/MemcachedCacheLockTestCase.php +++ b/tests/Integration/Cache/MemcachedCacheLockTestCase.php @@ -80,4 +80,27 @@ public function testMemcachedLocksCanBeReleasedUsingOwnerToken() $this->assertTrue(Cache::store('memcached')->lock('foo')->get()); } + + public function testOwnerStatusCanBeCheckedAfterRestoringLock() + { + Cache::store('memcached')->lock('foo')->forceRelease(); + + $firstLock = Cache::store('memcached')->lock('foo', 10); + $this->assertTrue($firstLock->get()); + $owner = $firstLock->owner(); + + $secondLock = Cache::store('memcached')->restoreLock('foo', $owner); + $this->assertTrue($secondLock->isOwnedByCurrentProcess()); + } + + public function testOtherOwnerDoesNotOwnLockAfterRestore() + { + Cache::store('memcached')->lock('foo')->forceRelease(); + + $firstLock = Cache::store('memcached')->lock('foo', 10); + $this->assertTrue($firstLock->get()); + + $secondLock = Cache::store('memcached')->restoreLock('foo', 'other_owner'); + $this->assertFalse($secondLock->isOwnedByCurrentProcess()); + } } diff --git a/tests/Integration/Cache/RedisCacheLockTest.php b/tests/Integration/Cache/RedisCacheLockTest.php index a225f47a0780..b5de1eb58891 100644 --- a/tests/Integration/Cache/RedisCacheLockTest.php +++ b/tests/Integration/Cache/RedisCacheLockTest.php @@ -108,4 +108,27 @@ public function testRedisLocksCanBeReleasedUsingOwnerToken() $this->assertTrue(Cache::store('redis')->lock('foo')->get()); } + + public function testOwnerStatusCanBeCheckedAfterRestoringLock() + { + Cache::store('redis')->lock('foo')->forceRelease(); + + $firstLock = Cache::store('redis')->lock('foo', 10); + $this->assertTrue($firstLock->get()); + $owner = $firstLock->owner(); + + $secondLock = Cache::store('redis')->restoreLock('foo', $owner); + $this->assertTrue($secondLock->isOwnedByCurrentProcess()); + } + + public function testOtherOwnerDoesNotOwnLockAfterRestore() + { + Cache::store('redis')->lock('foo')->forceRelease(); + + $firstLock = Cache::store('redis')->lock('foo', 10); + $this->assertTrue($firstLock->get()); + + $secondLock = Cache::store('redis')->restoreLock('foo', 'other_owner'); + $this->assertFalse($secondLock->isOwnedByCurrentProcess()); + } } From ad9ca70b9319c0b1151827b7cac7ad1a6449a0dc Mon Sep 17 00:00:00 2001 From: Joost Date: Thu, 7 Dec 2023 09:50:44 +0100 Subject: [PATCH 3/3] fix styling --- tests/Integration/Cache/FileCacheLockTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Integration/Cache/FileCacheLockTest.php b/tests/Integration/Cache/FileCacheLockTest.php index 341eb911d9f5..c00409fe9e0e 100644 --- a/tests/Integration/Cache/FileCacheLockTest.php +++ b/tests/Integration/Cache/FileCacheLockTest.php @@ -117,6 +117,5 @@ public function testOtherOwnerDoesNotOwnLockAfterRestore() $secondLock = Cache::store('file')->restoreLock('foo', 'other_owner'); $this->assertFalse($secondLock->isOwnedByCurrentProcess()); - - } + } }