File tree Expand file tree Collapse file tree 3 files changed +16
-12
lines changed
Expand file tree Collapse file tree 3 files changed +16
-12
lines changed Original file line number Diff line number Diff line change @@ -39,7 +39,7 @@ class Loop
3939 /**
4040 * @var bool True while code execution is repeating.
4141 */
42- private $ looping ;
42+ private $ looping = false ;
4343
4444 /**
4545 * Sets the timeout. The default is 3 seconds.
@@ -57,7 +57,6 @@ public function __construct(int $timeout = 3)
5757 }
5858
5959 $ this ->timeout = $ timeout ;
60- $ this ->looping = false ;
6160 }
6261
6362 /**
@@ -98,7 +97,10 @@ public function execute(callable $code)
9897 for ($ i = 0 ; $ this ->looping && microtime (true ) < $ deadline ; ++$ i ) {
9998 $ result = $ code ();
10099 if (!$ this ->looping ) { // @phpstan-ignore-line
101- break ;
100+ /*
101+ * The $code callback has called $this->end() and the lock has been acquired.
102+ */
103+ return $ result ;
102104 }
103105
104106 // Calculate max time remaining, don't sleep any longer than that.
@@ -120,10 +122,6 @@ public function execute(callable $code)
120122 usleep ($ usecToSleep );
121123 }
122124
123- if (microtime (true ) >= $ deadline ) {
124- throw TimeoutException::create ($ this ->timeout );
125- }
126-
127- return $ result ;
125+ throw TimeoutException::create ($ this ->timeout );
128126 }
129127}
Original file line number Diff line number Diff line change 33namespace malkusch \lock \mutex ;
44
55use Eloquent \Liberator \Liberator ;
6+ use PHPUnit \Framework \Constraint \IsType ;
67use PHPUnit \Framework \TestCase ;
78use Predis \Client ;
89use Redis ;
@@ -252,7 +253,13 @@ public function provideMutexFactories()
252253
253254 'semaphore ' => [function ($ timeout = 3 ) use ($ filename ): Mutex {
254255 $ semaphore = sem_get (ftok ($ filename , 'b ' ));
255- $ this ->assertTrue ($ semaphore instanceof \SysvSemaphore || is_resource ($ semaphore )); // @phpstan-ignore-line
256+ $ this ->assertThat (
257+ $ semaphore ,
258+ $ this ->logicalOr (
259+ $ this ->isInstanceOf (\SysvSemaphore::class),
260+ new IsType (IsType::TYPE_RESOURCE )
261+ )
262+ );
256263
257264 return new SemaphoreMutex ($ semaphore );
258265 }],
Original file line number Diff line number Diff line change @@ -58,10 +58,9 @@ public function testExecutionWithinTimeout()
5858 /**
5959 * Tests exceeding the execution timeout.
6060 */
61- public function testExceedTimeout ()
61+ public function testExceedTimeoutIsAcceptableIfEndWasCalled ()
6262 {
63- $ this ->expectException (TimeoutException::class);
64- $ this ->expectExceptionMessage ('Timeout of 1 seconds exceeded. ' );
63+ $ this ->expectNotToPerformAssertions ();
6564
6665 $ loop = new Loop (1 );
6766 $ loop ->execute (function () use ($ loop ): void {
You can’t perform that action at this time.
0 commit comments