13
13
14
14
use PHPUnit \Framework \TestCase ;
15
15
use Psr \Log \LoggerInterface ;
16
+ use Symfony \Component \Lock \BlockingSharedLockStoreInterface ;
16
17
use Symfony \Component \Lock \BlockingStoreInterface ;
17
18
use Symfony \Component \Lock \Exception \LockConflictedException ;
18
19
use Symfony \Component \Lock \Key ;
19
20
use Symfony \Component \Lock \Lock ;
20
21
use Symfony \Component \Lock \PersistingStoreInterface ;
22
+ use Symfony \Component \Lock \SharedLockStoreInterface ;
21
23
22
24
/**
23
25
* @author Jérémy Derussé <[email protected] >
@@ -40,7 +42,7 @@ public function testAcquireNoBlocking()
40
42
$ this ->assertTrue ($ lock ->acquire (false ));
41
43
}
42
44
43
- public function testAcquireNoBlockingStoreInterface ()
45
+ public function testAcquireNoBlockingWithPersistingStoreInterface ()
44
46
{
45
47
$ key = new Key (uniqid (__METHOD__ , true ));
46
48
$ store = $ this ->getMockBuilder (PersistingStoreInterface::class)->getMock ();
@@ -56,6 +58,44 @@ public function testAcquireNoBlockingStoreInterface()
56
58
$ this ->assertTrue ($ lock ->acquire (false ));
57
59
}
58
60
61
+ public function testAcquireBlockingWithPersistingStoreInterface ()
62
+ {
63
+ $ key = new Key (uniqid (__METHOD__ , true ));
64
+ $ store = $ this ->getMockBuilder (PersistingStoreInterface::class)->getMock ();
65
+ $ lock = new Lock ($ key , $ store );
66
+
67
+ $ store
68
+ ->expects ($ this ->once ())
69
+ ->method ('save ' );
70
+ $ store
71
+ ->method ('exists ' )
72
+ ->willReturnOnConsecutiveCalls (true , false );
73
+
74
+ $ this ->assertTrue ($ lock ->acquire (true ));
75
+ }
76
+
77
+ public function testAcquireBlockingRetryWithPersistingStoreInterface ()
78
+ {
79
+ $ key = new Key (uniqid (__METHOD__ , true ));
80
+ $ store = $ this ->getMockBuilder (PersistingStoreInterface::class)->getMock ();
81
+ $ lock = new Lock ($ key , $ store );
82
+
83
+ $ store
84
+ ->expects ($ this ->any ())
85
+ ->method ('save ' )
86
+ ->willReturnCallback (static function () {
87
+ if (1 === random_int (0 , 1 )) {
88
+ return ;
89
+ }
90
+ throw new LockConflictedException ('boom ' );
91
+ });
92
+ $ store
93
+ ->method ('exists ' )
94
+ ->willReturnOnConsecutiveCalls (true , false );
95
+
96
+ $ this ->assertTrue ($ lock ->acquire (true ));
97
+ }
98
+
59
99
public function testAcquireReturnsFalse ()
60
100
{
61
101
$ key = new Key (uniqid (__METHOD__ , true ));
@@ -90,7 +130,7 @@ public function testAcquireReturnsFalseStoreInterface()
90
130
$ this ->assertFalse ($ lock ->acquire (false ));
91
131
}
92
132
93
- public function testAcquireBlocking ()
133
+ public function testAcquireBlockingWithBlockingStoreInterface ()
94
134
{
95
135
$ key = new Key (uniqid (__METHOD__ , true ));
96
136
$ store = $ this ->createMock (BlockingStoreInterface::class);
@@ -372,4 +412,96 @@ public function provideExpiredDates()
372
412
yield [[0.1 ], false ];
373
413
yield [[-0.1 , null ], false ];
374
414
}
415
+
416
+ public function testAcquireReadNoBlockingWithSharedLockStoreInterface ()
417
+ {
418
+ $ key = new Key (uniqid (__METHOD__ , true ));
419
+ $ store = $ this ->createMock (SharedLockStoreInterface::class);
420
+ $ lock = new Lock ($ key , $ store );
421
+
422
+ $ store
423
+ ->expects ($ this ->once ())
424
+ ->method ('saveRead ' );
425
+ $ store
426
+ ->method ('exists ' )
427
+ ->willReturnOnConsecutiveCalls (true , false );
428
+
429
+ $ this ->assertTrue ($ lock ->acquireRead (false ));
430
+ }
431
+
432
+ public function testAcquireReadBlockingWithBlockingSharedLockStoreInterface ()
433
+ {
434
+ $ key = new Key (uniqid (__METHOD__ , true ));
435
+ $ store = $ this ->createMock (BlockingSharedLockStoreInterface::class);
436
+ $ lock = new Lock ($ key , $ store );
437
+
438
+ $ store
439
+ ->expects ($ this ->once ())
440
+ ->method ('waitAndSaveRead ' );
441
+ $ store
442
+ ->method ('exists ' )
443
+ ->willReturnOnConsecutiveCalls (true , false );
444
+
445
+ $ this ->assertTrue ($ lock ->acquireRead (true ));
446
+ }
447
+
448
+ public function testAcquireReadBlockingWithSharedLockStoreInterface ()
449
+ {
450
+ $ key = new Key (uniqid (__METHOD__ , true ));
451
+ $ store = $ this ->createMock (SharedLockStoreInterface::class);
452
+ $ lock = new Lock ($ key , $ store );
453
+
454
+ $ store
455
+ ->expects ($ this ->any ())
456
+ ->method ('saveRead ' )
457
+ ->willReturnCallback (static function () {
458
+ if (1 === random_int (0 , 1 )) {
459
+ return ;
460
+ }
461
+ throw new LockConflictedException ('boom ' );
462
+ });
463
+ $ store
464
+ ->method ('exists ' )
465
+ ->willReturnOnConsecutiveCalls (true , false );
466
+
467
+ $ this ->assertTrue ($ lock ->acquireRead (true ));
468
+ }
469
+
470
+ public function testAcquireReadBlockingWithBlockingLockStoreInterface ()
471
+ {
472
+ $ key = new Key (uniqid (__METHOD__ , true ));
473
+ $ store = $ this ->createMock (BlockingStoreInterface::class);
474
+ $ lock = new Lock ($ key , $ store );
475
+
476
+ $ store
477
+ ->expects ($ this ->once ())
478
+ ->method ('waitAndSave ' );
479
+ $ store
480
+ ->method ('exists ' )
481
+ ->willReturnOnConsecutiveCalls (true , false );
482
+
483
+ $ this ->assertTrue ($ lock ->acquireRead (true ));
484
+ }
485
+
486
+ public function testAcquireReadBlockingWithPersistingStoreInterface ()
487
+ {
488
+ $ key = new Key (uniqid (__METHOD__ , true ));
489
+ $ store = $ this ->createMock (PersistingStoreInterface::class);
490
+ $ lock = new Lock ($ key , $ store );
491
+
492
+ $ store
493
+ ->expects ($ this ->any ())
494
+ ->method ('save ' )
495
+ ->willReturnCallback (static function () {
496
+ if (1 === random_int (0 , 1 )) {
497
+ return ;
498
+ }
499
+ throw new LockConflictedException ('boom ' );
500
+ });
501
+ $ store
502
+ ->method ('exists ' )
503
+ ->willReturnOnConsecutiveCalls (true , false );
504
+
505
+ $ this ->assertTrue ($ lock ->acquireRead (true ));
506
+ }
375
507
}
0 commit comments