Skip to content

Commit

Permalink
[11.x] Add isCurrentlyOwnedBy function to lock (#51393)
Browse files Browse the repository at this point in the history
* Add isCurrentlyOwnedBy function to lock

* formatting

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
gazben and taylorotwell authored May 17, 2024
1 parent 6727464 commit bf7046f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Illuminate/Cache/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions tests/Integration/Cache/FileCacheLockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
3 changes: 3 additions & 0 deletions tests/Integration/Cache/MemcachedCacheLockTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
3 changes: 3 additions & 0 deletions tests/Integration/Cache/RedisCacheLockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
12 changes: 12 additions & 0 deletions tests/Integration/Database/DatabaseLockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit bf7046f

Please sign in to comment.