Skip to content

[12.x] Improve Cache Tests #55670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions tests/Integration/Cache/FileCacheLockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@
namespace Illuminate\Tests\Integration\Cache;

use Exception;
use Illuminate\Contracts\Cache\LockTimeoutException;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Sleep;
use Orchestra\Testbench\Attributes\WithConfig;
use Orchestra\Testbench\TestCase;

#[WithConfig('cache.default', 'file')]
class FileCacheLockTest extends TestCase
{
public function testLocksCanBeAcquiredAndReleased()
protected function setUp(): void
{
parent::setUp();

// flush lock from previous tests
Cache::lock('foo')->forceRelease();
}

public function testLocksCanBeAcquiredAndReleased()
{
$lock = Cache::lock('foo', 10);
$this->assertTrue($lock->get());
$this->assertFalse(Cache::lock('foo', 10)->get());
Expand All @@ -27,7 +35,6 @@ public function testLocksCanBeAcquiredAndReleased()

public function testLocksCanBlockForSeconds()
{
Cache::lock('foo')->forceRelease();
$this->assertSame('taylor', Cache::lock('foo', 10)->block(1, function () {
return 'taylor';
}));
Expand All @@ -38,11 +45,11 @@ public function testLocksCanBlockForSeconds()

public function testConcurrentLocksAreReleasedSafely()
{
Cache::lock('foo')->forceRelease();
Sleep::fake(syncWithCarbon: true);

$firstLock = Cache::lock('foo', 1);
$this->assertTrue($firstLock->get());
sleep(2);
Sleep::for(2)->seconds();

$secondLock = Cache::lock('foo', 10);
$this->assertTrue($secondLock->get());
Expand All @@ -54,8 +61,6 @@ public function testConcurrentLocksAreReleasedSafely()

public function testLocksWithFailedBlockCallbackAreReleased()
{
Cache::lock('foo')->forceRelease();

$firstLock = Cache::lock('foo', 10);

try {
Expand All @@ -75,8 +80,6 @@ public function testLocksWithFailedBlockCallbackAreReleased()

public function testLocksCanBeReleasedUsingOwnerToken()
{
Cache::lock('foo')->forceRelease();

$firstLock = Cache::lock('foo', 10);
$this->assertTrue($firstLock->get());
$owner = $firstLock->owner();
Expand All @@ -89,8 +92,6 @@ public function testLocksCanBeReleasedUsingOwnerToken()

public function testOwnerStatusCanBeCheckedAfterRestoringLock()
{
Cache::lock('foo')->forceRelease();

$firstLock = Cache::lock('foo', 10);
$this->assertTrue($firstLock->get());
$owner = $firstLock->owner();
Expand All @@ -101,8 +102,6 @@ public function testOwnerStatusCanBeCheckedAfterRestoringLock()

public function testOtherOwnerDoesNotOwnLockAfterRestore()
{
Cache::lock('foo')->forceRelease();

$firstLock = Cache::lock('foo', 10);
$this->assertTrue($firstLock->isOwnedBy(null));
$this->assertTrue($firstLock->get());
Expand All @@ -112,4 +111,16 @@ public function testOtherOwnerDoesNotOwnLockAfterRestore()
$this->assertTrue($secondLock->isOwnedBy($firstLock->owner()));
$this->assertFalse($secondLock->isOwnedByCurrentProcess());
}

public function testExceptionIfBlockCanNotAcquireLock()
{
Sleep::fake(syncWithCarbon: true);

// acquire and not release lock
Cache::lock('foo', 10)->get();

// try to get lock and hit block timeout
$this->expectException(LockTimeoutException::class);
Cache::lock('foo', 10)->block(5);
}
}