Skip to content

Commit 6ff3c37

Browse files
author
Willem Stuursma-Ruwen
authored
Merge pull request #48 from php-lock/issue-47
#47 fix edge case where the lock is acquired during the deadline
2 parents b68e0aa + 949c0bd commit 6ff3c37

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

classes/util/Loop.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff 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
}

tests/mutex/MutexConcurrencyTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace malkusch\lock\mutex;
44

55
use Eloquent\Liberator\Liberator;
6+
use PHPUnit\Framework\Constraint\IsType;
67
use PHPUnit\Framework\TestCase;
78
use Predis\Client;
89
use 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
}],

tests/util/LoopTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff 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 {

0 commit comments

Comments
 (0)