44
55namespace Malkusch \Lock \Tests \Mutex ;
66
7+ use Eloquent \Liberator \Liberator ;
8+ use Malkusch \Lock \Exception \LockAcquireTimeoutException ;
79use Malkusch \Lock \Mutex \PostgreSQLMutex ;
810use PHPUnit \Framework \Constraint \IsType ;
911use PHPUnit \Framework \MockObject \MockObject ;
@@ -24,7 +26,7 @@ protected function setUp(): void
2426
2527 $ this ->pdo = $ this ->createMock (\PDO ::class);
2628
27- $ this ->mutex = new PostgreSQLMutex ($ this ->pdo , 'test-one-negative-key ' );
29+ $ this ->mutex = Liberator:: liberate ( new PostgreSQLMutex ($ this ->pdo , 'test-one-negative-key ' )); // @phpstan-ignore assign.propertyType
2830 }
2931
3032 private function isPhpunit9x (): bool
@@ -97,4 +99,45 @@ public function testReleaseLock(): void
9799
98100 \Closure::bind (static fn ($ mutex ) => $ mutex ->unlock (), null , PostgreSQLMutex::class)($ this ->mutex );
99101 }
102+
103+ public function testAcquireTimeoutOccurs (): void
104+ {
105+ $ statement = $ this ->createMock (\PDOStatement::class);
106+
107+ $ this ->pdo ->expects (self ::atLeastOnce ())
108+ ->method ('prepare ' )
109+ ->with ('SELECT pg_try_advisory_lock(?, ?) ' )
110+ ->willReturn ($ statement );
111+
112+ $ statement ->expects (self ::atLeastOnce ())
113+ ->method ('execute ' )
114+ ->with (self ::logicalAnd (
115+ new IsType (IsType::TYPE_ARRAY ),
116+ self ::countOf (2 ),
117+ self ::callback (function (...$ arguments ) {
118+ if ($ this ->isPhpunit9x ()) { // https://github.com/sebastianbergmann/phpunit/issues/5891
119+ $ arguments = $ arguments [0 ];
120+ }
121+
122+ foreach ($ arguments as $ v ) {
123+ self ::assertLessThan (1 << 32 , $ v );
124+ self ::assertGreaterThanOrEqual (-(1 << 32 ), $ v );
125+ self ::assertIsInt ($ v );
126+ }
127+
128+ return true ;
129+ }),
130+ [533558444 , -1716795572 ]
131+ ));
132+
133+ $ statement ->expects (self ::atLeastOnce ())
134+ ->method ('fetchColumn ' )
135+ ->willReturn (false );
136+
137+ $ this ->mutex ->acquireTimeout = 1.0 ; // @phpstan-ignore property.private
138+
139+ $ this ->expectException (LockAcquireTimeoutException::class);
140+ $ this ->expectExceptionMessage ('Lock acquire timeout of 1.0 seconds has been exceeded ' );
141+ \Closure::bind (static fn ($ mutex ) => $ mutex ->lock (), null , PostgreSQLMutex::class)($ this ->mutex );
142+ }
100143}
0 commit comments