3
3
namespace Illuminate \Tests \Integration \Cache ;
4
4
5
5
use Exception ;
6
+ use Illuminate \Contracts \Cache \LockTimeoutException ;
6
7
use Illuminate \Support \Facades \Cache ;
8
+ use Illuminate \Support \Sleep ;
7
9
use Orchestra \Testbench \Attributes \WithConfig ;
8
10
use Orchestra \Testbench \TestCase ;
9
11
10
12
#[WithConfig('cache.default ' , 'file ' )]
11
13
class FileCacheLockTest extends TestCase
12
14
{
13
- public function testLocksCanBeAcquiredAndReleased ()
15
+ protected function setUp (): void
14
16
{
17
+ parent ::setUp ();
18
+
19
+ // flush lock from previous tests
15
20
Cache::lock ('foo ' )->forceRelease ();
21
+ }
16
22
23
+ public function testLocksCanBeAcquiredAndReleased ()
24
+ {
17
25
$ lock = Cache::lock ('foo ' , 10 );
18
26
$ this ->assertTrue ($ lock ->get ());
19
27
$ this ->assertFalse (Cache::lock ('foo ' , 10 )->get ());
@@ -27,7 +35,6 @@ public function testLocksCanBeAcquiredAndReleased()
27
35
28
36
public function testLocksCanBlockForSeconds ()
29
37
{
30
- Cache::lock ('foo ' )->forceRelease ();
31
38
$ this ->assertSame ('taylor ' , Cache::lock ('foo ' , 10 )->block (1 , function () {
32
39
return 'taylor ' ;
33
40
}));
@@ -38,11 +45,11 @@ public function testLocksCanBlockForSeconds()
38
45
39
46
public function testConcurrentLocksAreReleasedSafely ()
40
47
{
41
- Cache:: lock ( ' foo ' )-> forceRelease ( );
48
+ Sleep:: fake (syncWithCarbon: true );
42
49
43
50
$ firstLock = Cache::lock ('foo ' , 1 );
44
51
$ this ->assertTrue ($ firstLock ->get ());
45
- sleep ( 2 );
52
+ Sleep:: for ( 2 )-> seconds ( );
46
53
47
54
$ secondLock = Cache::lock ('foo ' , 10 );
48
55
$ this ->assertTrue ($ secondLock ->get ());
@@ -54,8 +61,6 @@ public function testConcurrentLocksAreReleasedSafely()
54
61
55
62
public function testLocksWithFailedBlockCallbackAreReleased ()
56
63
{
57
- Cache::lock ('foo ' )->forceRelease ();
58
-
59
64
$ firstLock = Cache::lock ('foo ' , 10 );
60
65
61
66
try {
@@ -75,8 +80,6 @@ public function testLocksWithFailedBlockCallbackAreReleased()
75
80
76
81
public function testLocksCanBeReleasedUsingOwnerToken ()
77
82
{
78
- Cache::lock ('foo ' )->forceRelease ();
79
-
80
83
$ firstLock = Cache::lock ('foo ' , 10 );
81
84
$ this ->assertTrue ($ firstLock ->get ());
82
85
$ owner = $ firstLock ->owner ();
@@ -89,8 +92,6 @@ public function testLocksCanBeReleasedUsingOwnerToken()
89
92
90
93
public function testOwnerStatusCanBeCheckedAfterRestoringLock ()
91
94
{
92
- Cache::lock ('foo ' )->forceRelease ();
93
-
94
95
$ firstLock = Cache::lock ('foo ' , 10 );
95
96
$ this ->assertTrue ($ firstLock ->get ());
96
97
$ owner = $ firstLock ->owner ();
@@ -101,8 +102,6 @@ public function testOwnerStatusCanBeCheckedAfterRestoringLock()
101
102
102
103
public function testOtherOwnerDoesNotOwnLockAfterRestore ()
103
104
{
104
- Cache::lock ('foo ' )->forceRelease ();
105
-
106
105
$ firstLock = Cache::lock ('foo ' , 10 );
107
106
$ this ->assertTrue ($ firstLock ->isOwnedBy (null ));
108
107
$ this ->assertTrue ($ firstLock ->get ());
@@ -112,4 +111,16 @@ public function testOtherOwnerDoesNotOwnLockAfterRestore()
112
111
$ this ->assertTrue ($ secondLock ->isOwnedBy ($ firstLock ->owner ()));
113
112
$ this ->assertFalse ($ secondLock ->isOwnedByCurrentProcess ());
114
113
}
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
+ }
115
126
}
0 commit comments