Skip to content

Commit 33db073

Browse files
Add tests for FlockMutex timeout conditions.
1 parent 82672fd commit 33db073

File tree

8 files changed

+87
-25
lines changed

8 files changed

+87
-25
lines changed

classes/mutex/FlockMutex.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,6 @@ class FlockMutex extends LockMutex
5151
*/
5252
private $strategy;
5353

54-
/**
55-
* @var float|null
56-
*/
57-
private $acquired;
58-
5954
/**
6055
* Sets the file handle.
6156
*
@@ -118,7 +113,6 @@ private function lockBusy()
118113
$loop = new Loop($this->timeout);
119114
$loop->execute(function () use ($loop) {
120115
if ($this->acquireNonBlockingLock()) {
121-
$this->acquired = \microtime(true);
122116
$loop->end();
123117
}
124118
});
@@ -165,19 +159,11 @@ protected function lock()
165159

166160
/**
167161
* @throws LockReleaseException
168-
* @throws ExecutionOutsideLockException
169162
*/
170163
protected function unlock()
171164
{
172165
if (!flock($this->fileHandle, LOCK_UN)) {
173166
throw new LockReleaseException("Failed to unlock the file.");
174167
}
175-
176-
if ($this->acquired !== null) {
177-
$elapsed_time = \microtime(true) - $this->acquired;
178-
if ($elapsed_time > $this->timeout) {
179-
throw ExecutionOutsideLockException::create($elapsed_time, $this->timeout);
180-
}
181-
}
182168
}
183169
}

tests/mutex/CASMutexTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace malkusch\lock\mutex;
44

5-
use phpmock\phpunit\PHPMock;
65
use phpmock\environment\SleepEnvironmentBuilder;
6+
use phpmock\phpunit\PHPMock;
77

88
/**
99
* Tests for CASMutex.

tests/mutex/FlockMutexTest.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace malkusch\lock\mutex;
4+
5+
use Eloquent\Liberator\Liberator;
6+
7+
/**
8+
* @author Willem Stuursma-Ruwen <[email protected]>
9+
* @link bitcoin:1P5FAZ4QhXCuwYPnLZdk3PJsqePbu1UDDA Donations
10+
* @license WTFPL
11+
* @see CASMutex
12+
*/
13+
class FlockMutexTest extends \PHPUnit_Framework_TestCase
14+
{
15+
/**
16+
* @var FlockMutex
17+
*/
18+
private $mutex;
19+
20+
/**
21+
* @var resource
22+
*/
23+
private $file;
24+
25+
protected function setUp()
26+
{
27+
parent::setUp();
28+
29+
$this->file = tempnam(sys_get_temp_dir(), "flock-");
30+
$this->mutex = Liberator::liberate(new FlockMutex(fopen($this->file, "r"), 1));
31+
}
32+
33+
protected function tearDown()
34+
{
35+
unlink($this->file);
36+
37+
parent::tearDown();
38+
}
39+
40+
/**
41+
* @dataProvider dpTimeoutableStrategies
42+
*/
43+
public function testCodeExecutedOutsideLockIsNotThrown($strategy)
44+
{
45+
$this->mutex->strategy = $strategy;
46+
47+
$this->assertTrue($this->mutex->synchronized(function () {
48+
usleep(1.1e6);
49+
return true;
50+
}));
51+
}
52+
53+
/**
54+
* @expectedException \malkusch\lock\exception\TimeoutException
55+
* @dataProvider dpTimeoutableStrategies
56+
*/
57+
public function testTimeoutOccurs($strategy)
58+
{
59+
$another_resource = fopen($this->file, "r");
60+
flock($another_resource, LOCK_EX);
61+
62+
$this->mutex->strategy = $strategy;
63+
64+
try {
65+
$this->mutex->synchronized(
66+
function () {
67+
$this->fail("Did not expect code to be executed");
68+
}
69+
);
70+
} finally {
71+
fclose($another_resource);
72+
}
73+
}
74+
75+
public function dpTimeoutableStrategies()
76+
{
77+
return [
78+
[FlockMutex::STRATEGY_PCNTL],
79+
[FlockMutex::STRATEGY_BUSY],
80+
];
81+
}
82+
}

tests/mutex/MutexConcurrencyTest.php

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

55
use Eloquent\Liberator\Liberator;
6+
use ezcSystemInfo;
67
use Predis\Client;
78
use Redis;
8-
use ezcSystemInfo;
99
use Spork\ProcessManager;
1010

1111
/**

tests/mutex/MutexTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
namespace malkusch\lock\mutex;
44

55
use Eloquent\Liberator\Liberator;
6+
use Memcached;
67
use org\bovigo\vfs\vfsStream;
78
use Predis\Client;
89
use Redis;
9-
use Memcached;
10-
use Spork\ProcessManager;
1110

1211
/**
1312
* Tests for Mutex.

tests/mutex/PgAdvisoryLockMutexTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
namespace malkusch\lock\mutex;
44

5-
use malkusch\lock\exception\LockAcquireException;
6-
use malkusch\lock\exception\LockReleaseException;
7-
85
/**
96
* @author Willem Stuursma-Ruwen <[email protected]>
107
* @link bitcoin:1P5FAZ4QhXCuwYPnLZdk3PJsqePbu1UDDA Donations

tests/mutex/RedisMutexTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44

55
use malkusch\lock\exception\LockAcquireException;
66
use malkusch\lock\exception\LockReleaseException;
7-
use phpmock\phpunit\PHPMock;
87
use phpmock\environment\SleepEnvironmentBuilder;
9-
use Spork\ProcessManager;
10-
use Spork\Fork;
8+
use phpmock\phpunit\PHPMock;
119

1210
/**
1311
* Tests for RedisMutex.

tests/util/LoopTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace malkusch\lock\util;
44

5-
use phpmock\phpunit\PHPMock;
65
use phpmock\environment\SleepEnvironmentBuilder;
6+
use phpmock\phpunit\PHPMock;
77

88
/**
99
* Tests for Loop.

0 commit comments

Comments
 (0)