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
39
39
/**
40
40
* @var bool True while code execution is repeating.
41
41
*/
42
- private $ looping ;
42
+ private $ looping = false ;
43
43
44
44
/**
45
45
* Sets the timeout. The default is 3 seconds.
@@ -57,7 +57,6 @@ public function __construct(int $timeout = 3)
57
57
}
58
58
59
59
$ this ->timeout = $ timeout ;
60
- $ this ->looping = false ;
61
60
}
62
61
63
62
/**
@@ -98,7 +97,10 @@ public function execute(callable $code)
98
97
for ($ i = 0 ; $ this ->looping && microtime (true ) < $ deadline ; ++$ i ) {
99
98
$ result = $ code ();
100
99
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 ;
102
104
}
103
105
104
106
// Calculate max time remaining, don't sleep any longer than that.
@@ -120,10 +122,6 @@ public function execute(callable $code)
120
122
usleep ($ usecToSleep );
121
123
}
122
124
123
- if (microtime (true ) >= $ deadline ) {
124
- throw TimeoutException::create ($ this ->timeout );
125
- }
126
-
127
- return $ result ;
125
+ throw TimeoutException::create ($ this ->timeout );
128
126
}
129
127
}
Original file line number Diff line number Diff line change 3
3
namespace malkusch \lock \mutex ;
4
4
5
5
use Eloquent \Liberator \Liberator ;
6
+ use PHPUnit \Framework \Constraint \IsType ;
6
7
use PHPUnit \Framework \TestCase ;
7
8
use Predis \Client ;
8
9
use Redis ;
@@ -252,7 +253,13 @@ public function provideMutexFactories()
252
253
253
254
'semaphore ' => [function ($ timeout = 3 ) use ($ filename ): Mutex {
254
255
$ 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
+ );
256
263
257
264
return new SemaphoreMutex ($ semaphore );
258
265
}],
Original file line number Diff line number Diff line change @@ -58,10 +58,9 @@ public function testExecutionWithinTimeout()
58
58
/**
59
59
* Tests exceeding the execution timeout.
60
60
*/
61
- public function testExceedTimeout ()
61
+ public function testExceedTimeoutIsAcceptableIfEndWasCalled ()
62
62
{
63
- $ this ->expectException (TimeoutException::class);
64
- $ this ->expectExceptionMessage ('Timeout of 1 seconds exceeded. ' );
63
+ $ this ->expectNotToPerformAssertions ();
65
64
66
65
$ loop = new Loop (1 );
67
66
$ loop ->execute (function () use ($ loop ): void {
You can’t perform that action at this time.
0 commit comments