Skip to content

Commit 99debba

Browse files
authored
[12.x] Improve Cache Tests (#55670)
* move cleanup to setup * fake sleep to speed test up by 2s * add missing test for exception
1 parent 67f3f84 commit 99debba

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

tests/Integration/Cache/FileCacheLockTest.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,25 @@
33
namespace Illuminate\Tests\Integration\Cache;
44

55
use Exception;
6+
use Illuminate\Contracts\Cache\LockTimeoutException;
67
use Illuminate\Support\Facades\Cache;
8+
use Illuminate\Support\Sleep;
79
use Orchestra\Testbench\Attributes\WithConfig;
810
use Orchestra\Testbench\TestCase;
911

1012
#[WithConfig('cache.default', 'file')]
1113
class FileCacheLockTest extends TestCase
1214
{
13-
public function testLocksCanBeAcquiredAndReleased()
15+
protected function setUp(): void
1416
{
17+
parent::setUp();
18+
19+
// flush lock from previous tests
1520
Cache::lock('foo')->forceRelease();
21+
}
1622

23+
public function testLocksCanBeAcquiredAndReleased()
24+
{
1725
$lock = Cache::lock('foo', 10);
1826
$this->assertTrue($lock->get());
1927
$this->assertFalse(Cache::lock('foo', 10)->get());
@@ -27,7 +35,6 @@ public function testLocksCanBeAcquiredAndReleased()
2735

2836
public function testLocksCanBlockForSeconds()
2937
{
30-
Cache::lock('foo')->forceRelease();
3138
$this->assertSame('taylor', Cache::lock('foo', 10)->block(1, function () {
3239
return 'taylor';
3340
}));
@@ -38,11 +45,11 @@ public function testLocksCanBlockForSeconds()
3845

3946
public function testConcurrentLocksAreReleasedSafely()
4047
{
41-
Cache::lock('foo')->forceRelease();
48+
Sleep::fake(syncWithCarbon: true);
4249

4350
$firstLock = Cache::lock('foo', 1);
4451
$this->assertTrue($firstLock->get());
45-
sleep(2);
52+
Sleep::for(2)->seconds();
4653

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

5562
public function testLocksWithFailedBlockCallbackAreReleased()
5663
{
57-
Cache::lock('foo')->forceRelease();
58-
5964
$firstLock = Cache::lock('foo', 10);
6065

6166
try {
@@ -75,8 +80,6 @@ public function testLocksWithFailedBlockCallbackAreReleased()
7580

7681
public function testLocksCanBeReleasedUsingOwnerToken()
7782
{
78-
Cache::lock('foo')->forceRelease();
79-
8083
$firstLock = Cache::lock('foo', 10);
8184
$this->assertTrue($firstLock->get());
8285
$owner = $firstLock->owner();
@@ -89,8 +92,6 @@ public function testLocksCanBeReleasedUsingOwnerToken()
8992

9093
public function testOwnerStatusCanBeCheckedAfterRestoringLock()
9194
{
92-
Cache::lock('foo')->forceRelease();
93-
9495
$firstLock = Cache::lock('foo', 10);
9596
$this->assertTrue($firstLock->get());
9697
$owner = $firstLock->owner();
@@ -101,8 +102,6 @@ public function testOwnerStatusCanBeCheckedAfterRestoringLock()
101102

102103
public function testOtherOwnerDoesNotOwnLockAfterRestore()
103104
{
104-
Cache::lock('foo')->forceRelease();
105-
106105
$firstLock = Cache::lock('foo', 10);
107106
$this->assertTrue($firstLock->isOwnedBy(null));
108107
$this->assertTrue($firstLock->get());
@@ -112,4 +111,16 @@ public function testOtherOwnerDoesNotOwnLockAfterRestore()
112111
$this->assertTrue($secondLock->isOwnedBy($firstLock->owner()));
113112
$this->assertFalse($secondLock->isOwnedByCurrentProcess());
114113
}
114+
115+
public function testExceptionIfBlockCanNotAcquireLock()
116+
{
117+
Sleep::fake(syncWithCarbon: true);
118+
119+
// acquire and not release lock
120+
Cache::lock('foo', 10)->get();
121+
122+
// try to get lock and hit block timeout
123+
$this->expectException(LockTimeoutException::class);
124+
Cache::lock('foo', 10)->block(5);
125+
}
115126
}

0 commit comments

Comments
 (0)