diff --git a/src/Illuminate/Cache/Lock.php b/src/Illuminate/Cache/Lock.php index ccd1c6474a5f..4868fdf82a50 100644 --- a/src/Illuminate/Cache/Lock.php +++ b/src/Illuminate/Cache/Lock.php @@ -148,7 +148,7 @@ public function owner() * * @return bool */ - protected function isOwnedByCurrentProcess() + public function isOwnedByCurrentProcess() { return $this->getCurrentOwner() === $this->owner; } diff --git a/tests/Integration/Cache/FileCacheLockTest.php b/tests/Integration/Cache/FileCacheLockTest.php index 98ef814d212d..c00409fe9e0e 100644 --- a/tests/Integration/Cache/FileCacheLockTest.php +++ b/tests/Integration/Cache/FileCacheLockTest.php @@ -95,4 +95,27 @@ 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()); + } }