Skip to content

Commit 234444e

Browse files
JoostbJoost
andauthored
[10.x] Allow checking if lock succesfully restored (#49272)
* Add function to validate if the lock is owned by the current owner. * make Lock->isOwnedByCurrentProcess public remove change to contract and add tests * fix styling --------- Co-authored-by: Joost <[email protected]>
1 parent c3696ca commit 234444e

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

src/Illuminate/Cache/Lock.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function owner()
148148
*
149149
* @return bool
150150
*/
151-
protected function isOwnedByCurrentProcess()
151+
public function isOwnedByCurrentProcess()
152152
{
153153
return $this->getCurrentOwner() === $this->owner;
154154
}

tests/Integration/Cache/FileCacheLockTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,27 @@ public function testLocksCanBeReleasedUsingOwnerToken()
9595

9696
$this->assertTrue(Cache::lock('foo')->get());
9797
}
98+
99+
public function testOwnerStatusCanBeCheckedAfterRestoringLock()
100+
{
101+
Cache::lock('foo')->forceRelease();
102+
103+
$firstLock = Cache::lock('foo', 10);
104+
$this->assertTrue($firstLock->get());
105+
$owner = $firstLock->owner();
106+
107+
$secondLock = Cache::store('file')->restoreLock('foo', $owner);
108+
$this->assertTrue($secondLock->isOwnedByCurrentProcess());
109+
}
110+
111+
public function testOtherOwnerDoesNotOwnLockAfterRestore()
112+
{
113+
Cache::lock('foo')->forceRelease();
114+
115+
$firstLock = Cache::lock('foo', 10);
116+
$this->assertTrue($firstLock->get());
117+
118+
$secondLock = Cache::store('file')->restoreLock('foo', 'other_owner');
119+
$this->assertFalse($secondLock->isOwnedByCurrentProcess());
120+
}
98121
}

tests/Integration/Cache/MemcachedCacheLockTestCase.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,27 @@ public function testMemcachedLocksCanBeReleasedUsingOwnerToken()
8080

8181
$this->assertTrue(Cache::store('memcached')->lock('foo')->get());
8282
}
83+
84+
public function testOwnerStatusCanBeCheckedAfterRestoringLock()
85+
{
86+
Cache::store('memcached')->lock('foo')->forceRelease();
87+
88+
$firstLock = Cache::store('memcached')->lock('foo', 10);
89+
$this->assertTrue($firstLock->get());
90+
$owner = $firstLock->owner();
91+
92+
$secondLock = Cache::store('memcached')->restoreLock('foo', $owner);
93+
$this->assertTrue($secondLock->isOwnedByCurrentProcess());
94+
}
95+
96+
public function testOtherOwnerDoesNotOwnLockAfterRestore()
97+
{
98+
Cache::store('memcached')->lock('foo')->forceRelease();
99+
100+
$firstLock = Cache::store('memcached')->lock('foo', 10);
101+
$this->assertTrue($firstLock->get());
102+
103+
$secondLock = Cache::store('memcached')->restoreLock('foo', 'other_owner');
104+
$this->assertFalse($secondLock->isOwnedByCurrentProcess());
105+
}
83106
}

tests/Integration/Cache/RedisCacheLockTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,27 @@ public function testRedisLocksCanBeReleasedUsingOwnerToken()
108108

109109
$this->assertTrue(Cache::store('redis')->lock('foo')->get());
110110
}
111+
112+
public function testOwnerStatusCanBeCheckedAfterRestoringLock()
113+
{
114+
Cache::store('redis')->lock('foo')->forceRelease();
115+
116+
$firstLock = Cache::store('redis')->lock('foo', 10);
117+
$this->assertTrue($firstLock->get());
118+
$owner = $firstLock->owner();
119+
120+
$secondLock = Cache::store('redis')->restoreLock('foo', $owner);
121+
$this->assertTrue($secondLock->isOwnedByCurrentProcess());
122+
}
123+
124+
public function testOtherOwnerDoesNotOwnLockAfterRestore()
125+
{
126+
Cache::store('redis')->lock('foo')->forceRelease();
127+
128+
$firstLock = Cache::store('redis')->lock('foo', 10);
129+
$this->assertTrue($firstLock->get());
130+
131+
$secondLock = Cache::store('redis')->restoreLock('foo', 'other_owner');
132+
$this->assertFalse($secondLock->isOwnedByCurrentProcess());
133+
}
111134
}

0 commit comments

Comments
 (0)