Skip to content

Commit bf7046f

Browse files
gazbentaylorotwell
andauthored
[11.x] Add isCurrentlyOwnedBy function to lock (#51393)
* Add isCurrentlyOwnedBy function to lock * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 6727464 commit bf7046f

File tree

5 files changed

+33
-1
lines changed

5 files changed

+33
-1
lines changed

src/Illuminate/Cache/Lock.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,18 @@ public function owner()
150150
*/
151151
public function isOwnedByCurrentProcess()
152152
{
153-
return $this->getCurrentOwner() === $this->owner;
153+
return $this->isOwnedBy($this->owner);
154+
}
155+
156+
/**
157+
* Determine whether this lock is owned by the given identifier.
158+
*
159+
* @param string|null $owner
160+
* @return bool
161+
*/
162+
public function isOwnedBy($owner)
163+
{
164+
return $this->getCurrentOwner() === $owner;
154165
}
155166

156167
/**

tests/Integration/Cache/FileCacheLockTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,12 @@ public function testOtherOwnerDoesNotOwnLockAfterRestore()
104104
Cache::lock('foo')->forceRelease();
105105

106106
$firstLock = Cache::lock('foo', 10);
107+
$this->assertTrue($firstLock->isOwnedBy(null));
107108
$this->assertTrue($firstLock->get());
109+
$this->assertTrue($firstLock->isOwnedBy($firstLock->owner()));
108110

109111
$secondLock = Cache::store('file')->restoreLock('foo', 'other_owner');
112+
$this->assertTrue($secondLock->isOwnedBy($firstLock->owner()));
110113
$this->assertFalse($secondLock->isOwnedByCurrentProcess());
111114
}
112115
}

tests/Integration/Cache/MemcachedCacheLockTestCase.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,12 @@ public function testOtherOwnerDoesNotOwnLockAfterRestore()
9797
Cache::store('memcached')->lock('foo')->forceRelease();
9898

9999
$firstLock = Cache::store('memcached')->lock('foo', 10);
100+
$this->assertTrue($firstLock->isOwnedBy(null));
100101
$this->assertTrue($firstLock->get());
102+
$this->assertTrue($firstLock->isOwnedBy($firstLock->owner()));
101103

102104
$secondLock = Cache::store('memcached')->restoreLock('foo', 'other_owner');
105+
$this->assertTrue($secondLock->isOwnedBy($firstLock->owner()));
103106
$this->assertFalse($secondLock->isOwnedByCurrentProcess());
104107
}
105108
}

tests/Integration/Cache/RedisCacheLockTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,12 @@ public function testOtherOwnerDoesNotOwnLockAfterRestore()
126126
Cache::store('redis')->lock('foo')->forceRelease();
127127

128128
$firstLock = Cache::store('redis')->lock('foo', 10);
129+
$this->assertTrue($firstLock->isOwnedBy(null));
129130
$this->assertTrue($firstLock->get());
131+
$this->assertTrue($firstLock->isOwnedBy($firstLock->owner()));
130132

131133
$secondLock = Cache::store('redis')->restoreLock('foo', 'other_owner');
134+
$this->assertTrue($secondLock->isOwnedBy($firstLock->owner()));
132135
$this->assertFalse($secondLock->isOwnedByCurrentProcess());
133136
}
134137
}

tests/Integration/Database/DatabaseLockTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,16 @@ public function testExpiredLockCanBeRetrieved()
5656

5757
$otherLock->release();
5858
}
59+
60+
public function testOtherOwnerDoesNotOwnLockAfterRestore()
61+
{
62+
$firstLock = Cache::store('database')->lock('foo');
63+
$this->assertTrue($firstLock->isOwnedBy(null));
64+
$this->assertTrue($firstLock->get());
65+
$this->assertTrue($firstLock->isOwnedBy($firstLock->owner()));
66+
67+
$secondLock = Cache::store('database')->restoreLock('foo', 'other_owner');
68+
$this->assertTrue($secondLock->isOwnedBy($firstLock->owner()));
69+
$this->assertFalse($secondLock->isOwnedByCurrentProcess());
70+
}
5971
}

0 commit comments

Comments
 (0)