Skip to content

Commit

Permalink
Add isCurrentlyOwnedBy function to lock
Browse files Browse the repository at this point in the history
  • Loading branch information
gazben committed May 13, 2024
1 parent fd6cc89 commit 4e69b25
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Illuminate/Cache/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ public function owner()
return $this->owner;
}

/**
* Determines whether this lock is owned by the given identifier.
*
* @return bool
*/
public function isCurrentlyOwnedBy($owner = null)
{
return $this->getCurrentOwner() === $owner;
}

/**
* Determines whether this lock is allowed to release the lock in the driver.
*
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->isCurrentlyOwnedBy(null));
$this->assertTrue($firstLock->get());
$this->assertTrue($firstLock->isCurrentlyOwnedBy($firstLock->owner()));

$secondLock = Cache::store('file')->restoreLock('foo', 'other_owner');
$this->assertFalse($secondLock->isCurrentlyOwnedBy($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->isCurrentlyOwnedBy(null));
$this->assertTrue($firstLock->get());
$this->assertTrue($firstLock->isCurrentlyOwnedBy($firstLock->owner()));

$secondLock = Cache::store('memcached')->restoreLock('foo', 'other_owner');
$this->assertFalse($secondLock->isCurrentlyOwnedBy($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->isCurrentlyOwnedBy(null));
$this->assertTrue($firstLock->get());
$this->assertTrue($firstLock->isCurrentlyOwnedBy($firstLock->owner()));

$secondLock = Cache::store('redis')->restoreLock('foo', 'other_owner');
$this->assertFalse($secondLock->isCurrentlyOwnedBy($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->isCurrentlyOwnedBy(null));
$this->assertTrue($firstLock->get());
$this->assertTrue($firstLock->isCurrentlyOwnedBy($firstLock->owner()));

$secondLock = Cache::store('database')->restoreLock('foo', 'other_owner');
$this->assertFalse($secondLock->isCurrentlyOwnedBy($firstLock->owner()));
$this->assertFalse($secondLock->isOwnedByCurrentProcess());
}
}

0 comments on commit 4e69b25

Please sign in to comment.