diff --git a/src/Illuminate/Cache/Lock.php b/src/Illuminate/Cache/Lock.php index 4868fdf82a50..435df78f384a 100644 --- a/src/Illuminate/Cache/Lock.php +++ b/src/Illuminate/Cache/Lock.php @@ -150,7 +150,18 @@ public function owner() */ public function isOwnedByCurrentProcess() { - return $this->getCurrentOwner() === $this->owner; + return $this->isOwnedBy($this->owner); + } + + /** + * Determine whether this lock is owned by the given identifier. + * + * @param string|null $owner + * @return bool + */ + public function isOwnedBy($owner) + { + return $this->getCurrentOwner() === $owner; } /** diff --git a/tests/Integration/Cache/FileCacheLockTest.php b/tests/Integration/Cache/FileCacheLockTest.php index 594471806393..c9eff5ced0e9 100644 --- a/tests/Integration/Cache/FileCacheLockTest.php +++ b/tests/Integration/Cache/FileCacheLockTest.php @@ -104,9 +104,12 @@ public function testOtherOwnerDoesNotOwnLockAfterRestore() Cache::lock('foo')->forceRelease(); $firstLock = Cache::lock('foo', 10); + $this->assertTrue($firstLock->isOwnedBy(null)); $this->assertTrue($firstLock->get()); + $this->assertTrue($firstLock->isOwnedBy($firstLock->owner())); $secondLock = Cache::store('file')->restoreLock('foo', 'other_owner'); + $this->assertTrue($secondLock->isOwnedBy($firstLock->owner())); $this->assertFalse($secondLock->isOwnedByCurrentProcess()); } } diff --git a/tests/Integration/Cache/MemcachedCacheLockTestCase.php b/tests/Integration/Cache/MemcachedCacheLockTestCase.php index e4fc82dca804..d819fb9fd73d 100644 --- a/tests/Integration/Cache/MemcachedCacheLockTestCase.php +++ b/tests/Integration/Cache/MemcachedCacheLockTestCase.php @@ -97,9 +97,12 @@ public function testOtherOwnerDoesNotOwnLockAfterRestore() Cache::store('memcached')->lock('foo')->forceRelease(); $firstLock = Cache::store('memcached')->lock('foo', 10); + $this->assertTrue($firstLock->isOwnedBy(null)); $this->assertTrue($firstLock->get()); + $this->assertTrue($firstLock->isOwnedBy($firstLock->owner())); $secondLock = Cache::store('memcached')->restoreLock('foo', 'other_owner'); + $this->assertTrue($secondLock->isOwnedBy($firstLock->owner())); $this->assertFalse($secondLock->isOwnedByCurrentProcess()); } } diff --git a/tests/Integration/Cache/RedisCacheLockTest.php b/tests/Integration/Cache/RedisCacheLockTest.php index b5de1eb58891..064e84344a40 100644 --- a/tests/Integration/Cache/RedisCacheLockTest.php +++ b/tests/Integration/Cache/RedisCacheLockTest.php @@ -126,9 +126,12 @@ public function testOtherOwnerDoesNotOwnLockAfterRestore() Cache::store('redis')->lock('foo')->forceRelease(); $firstLock = Cache::store('redis')->lock('foo', 10); + $this->assertTrue($firstLock->isOwnedBy(null)); $this->assertTrue($firstLock->get()); + $this->assertTrue($firstLock->isOwnedBy($firstLock->owner())); $secondLock = Cache::store('redis')->restoreLock('foo', 'other_owner'); + $this->assertTrue($secondLock->isOwnedBy($firstLock->owner())); $this->assertFalse($secondLock->isOwnedByCurrentProcess()); } } diff --git a/tests/Integration/Database/DatabaseLockTest.php b/tests/Integration/Database/DatabaseLockTest.php index 08f848e22543..2424539d3391 100644 --- a/tests/Integration/Database/DatabaseLockTest.php +++ b/tests/Integration/Database/DatabaseLockTest.php @@ -56,4 +56,16 @@ public function testExpiredLockCanBeRetrieved() $otherLock->release(); } + + public function testOtherOwnerDoesNotOwnLockAfterRestore() + { + $firstLock = Cache::store('database')->lock('foo'); + $this->assertTrue($firstLock->isOwnedBy(null)); + $this->assertTrue($firstLock->get()); + $this->assertTrue($firstLock->isOwnedBy($firstLock->owner())); + + $secondLock = Cache::store('database')->restoreLock('foo', 'other_owner'); + $this->assertTrue($secondLock->isOwnedBy($firstLock->owner())); + $this->assertFalse($secondLock->isOwnedByCurrentProcess()); + } }