Skip to content

Commit 7f340d1

Browse files
author
Willem Stuursma-Ruwen
authored
Merge pull request #38 from php-lock/php-7.2
Upgrade to require minimum of PHP 7.2 and PHPUnit 9
2 parents 3f3788b + 3a2a292 commit 7f340d1

22 files changed

+236
-221
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/.idea
2+
/.php_cs
3+
/.phpunit.result.cache
24
/vendor
35
/composer.lock
46
/phpunit.xml
5-
/.php_cs

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ cache:
55
- $HOME/.composer/cache
66

77
php:
8-
- 7.1
98
- 7.2
109
- 7.3
1110
- 7.4

classes/mutex/PgAdvisoryLockMutex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __construct(\PDO $PDO, string $name)
3636
throw new RuntimeException('Unable to hash the key, sha256 algorithm is not supported.');
3737
}
3838

39-
list($bytes1, $bytes2) = str_split($hashed_name, 4);
39+
[$bytes1, $bytes2] = str_split($hashed_name, 4);
4040

4141
$this->key1 = unpack('i', $bytes1)[1];
4242
$this->key2 = unpack('i', $bytes2)[1];

classes/mutex/SpinlockMutex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ abstract class SpinlockMutex extends LockMutex
3030
private $timeout;
3131

3232
/**
33-
* @var \malkusch\lock\util\Loop The loop.
33+
* @var Loop The loop.
3434
*/
3535
private $loop;
3636

classes/util/Loop.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ class Loop
2222
*
2323
* @var double
2424
*/
25-
private const MINIMUM_WAIT_US = 1e4;
25+
private const MINIMUM_WAIT_US = 1e4; // 0.01 seconds
2626

2727
/**
2828
* Maximum time that we want to wait, between lock checks. In micro seconds.
2929
*
3030
* @var double
3131
*/
32-
private const MAXIMUM_WAIT_US = 1e6;
32+
private const MAXIMUM_WAIT_US = 5e5; // 0.50 seconds
3333

3434
/**
3535
* @var int The timeout in seconds.
@@ -110,7 +110,7 @@ public function execute(callable $code)
110110
}
111111

112112
$min = min(
113-
(int) self::MINIMUM_WAIT_US * 1.5 ** $i,
113+
(int) self::MINIMUM_WAIT_US * 1.25 ** $i,
114114
self::MAXIMUM_WAIT_US
115115
);
116116
$max = min($min * 2, self::MAXIMUM_WAIT_US);

composer.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,25 @@
4242
}
4343
},
4444
"require": {
45-
"php": ">=7.1",
45+
"php": "^7.2",
4646
"psr/log": "^1"
4747
},
4848
"require-dev": {
4949
"ext-memcached": "*",
5050
"ext-pcntl": "*",
51+
"ext-pdo": "*",
5152
"ext-pdo_mysql": "*",
5253
"ext-pdo_sqlite": "*",
5354
"ext-sysvsem": "*",
5455
"eloquent/liberator": "^2.0",
55-
"friendsofphp/php-cs-fixer": "^2.14",
56+
"friendsofphp/php-cs-fixer": "^2.16",
5657
"johnkary/phpunit-speedtrap": "^3.0",
57-
"kriswallsmith/spork": "^0.3",
5858
"mikey179/vfsstream": "^1.6",
5959
"php-mock/php-mock-phpunit": "^2.1",
60-
"phpunit/phpunit": "^7.4",
60+
"phpunit/phpunit": "^9.4",
6161
"predis/predis": "^1.1",
62-
"squizlabs/php_codesniffer": "^3.3",
63-
"symfony/event-dispatcher": "^2.8"
62+
"spatie/async": "^1.5",
63+
"squizlabs/php_codesniffer": "^3.3"
6464
},
6565
"suggest": {
6666
"ext-igbinary": "To use this library with PHP Redis igbinary serializer enabled.",

phpunit.xml.dist

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.4/phpunit.xsd"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.4/phpunit.xsd"
44
backupGlobals="false"
55
backupStaticAttributes="false"
66
bootstrap="vendor/autoload.php"
@@ -15,11 +15,6 @@
1515
<directory suffix="Test.php">./tests</directory>
1616
</testsuite>
1717
</testsuites>
18-
<filter>
19-
<whitelist processUncoveredFilesFromWhitelist="true">
20-
<directory suffix=".php">./classes</directory>
21-
</whitelist>
22-
</filter>
2318
<listeners>
2419
<listener class="JohnKary\PHPUnit\Listener\SpeedTrapListener" />
2520
</listeners>

tests/mutex/CASMutexTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace malkusch\lock\mutex;
44

5+
use malkusch\lock\exception\LockAcquireException;
56
use phpmock\environment\SleepEnvironmentBuilder;
67
use phpmock\phpunit\PHPMock;
78
use PHPUnit\Framework\TestCase;
@@ -18,7 +19,7 @@ class CASMutexTest extends TestCase
1819
{
1920
use PHPMock;
2021

21-
protected function setUp()
22+
protected function setUp(): void
2223
{
2324
parent::setUp();
2425

@@ -33,11 +34,11 @@ protected function setUp()
3334

3435
/**
3536
* Tests exceeding the execution timeout.
36-
*
37-
* @expectedException malkusch\lock\exception\LockAcquireException
3837
*/
3938
public function testExceedTimeout()
4039
{
40+
$this->expectException(LockAcquireException::class);
41+
4142
$mutex = new CASMutex(1);
4243
$mutex->synchronized(function (): void {
4344
sleep(2);
@@ -46,11 +47,11 @@ public function testExceedTimeout()
4647

4748
/**
4849
* Tests that an exception would stop any further iteration.
49-
*
50-
* @expectedException \DomainException
5150
*/
5251
public function testExceptionStopsIteration()
5352
{
53+
$this->expectException(\DomainException::class);
54+
5455
$mutex = new CASMutex();
5556
$mutex->synchronized(function () {
5657
throw new \DomainException();

tests/mutex/FlockMutexTest.php

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

55
use Eloquent\Liberator\Liberator;
6+
use malkusch\lock\exception\DeadlineException;
7+
use malkusch\lock\exception\TimeoutException;
68
use malkusch\lock\util\PcntlTimeout;
79
use PHPUnit\Framework\TestCase;
810

@@ -24,15 +26,15 @@ class FlockMutexTest extends TestCase
2426
*/
2527
private $file;
2628

27-
protected function setUp()
29+
protected function setUp(): void
2830
{
2931
parent::setUp();
3032

3133
$this->file = tempnam(sys_get_temp_dir(), 'flock-');
3234
$this->mutex = Liberator::liberate(new FlockMutex(fopen($this->file, 'r'), 1));
3335
}
3436

35-
protected function tearDown()
37+
protected function tearDown(): void
3638
{
3739
unlink($this->file);
3840

@@ -54,12 +56,13 @@ public function testCodeExecutedOutsideLockIsNotThrown(int $strategy)
5456
}
5557

5658
/**
57-
* @expectedException \malkusch\lock\exception\TimeoutException
58-
* @expectedExceptionMessage Timeout of 1 seconds exceeded.
5959
* @dataProvider dpTimeoutableStrategies
6060
*/
6161
public function testTimeoutOccurs(int $strategy)
6262
{
63+
$this->expectException(TimeoutException::class);
64+
$this->expectExceptionMessage('Timeout of 1 seconds exceeded.');
65+
6366
$another_resource = fopen($this->file, 'r');
6467
flock($another_resource, LOCK_EX);
6568

@@ -84,19 +87,18 @@ public function dpTimeoutableStrategies()
8487
];
8588
}
8689

87-
/**
88-
* @expectedException \malkusch\lock\exception\DeadlineException
89-
*/
9090
public function testNoTimeoutWaitsForever()
9191
{
92+
$this->expectException(DeadlineException::class);
93+
9294
$another_resource = fopen($this->file, 'r');
9395
flock($another_resource, LOCK_EX);
9496

9597
$this->mutex->strategy = FlockMutex::STRATEGY_BLOCK;
9698

9799
$timebox = new PcntlTimeout(1);
98100
$timebox->timeBoxed(function () {
99-
$this->mutex->synchronized(function () {
101+
$this->mutex->synchronized(function (): void {
100102
$this->fail('Did not expect code execution.');
101103
});
102104
});

tests/mutex/LockMutexTest.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class LockMutexTest extends TestCase
2121
*/
2222
private $mutex;
2323

24-
protected function setUp()
24+
protected function setUp(): void
2525
{
2626
parent::setUp();
2727

@@ -30,23 +30,22 @@ protected function setUp()
3030

3131
/**
3232
* Tests lock() fails and the code is not executed.
33-
*
34-
* @expectedException malkusch\lock\exception\LockAcquireException
3533
*/
3634
public function testLockFails()
3735
{
36+
$this->expectException(LockAcquireException::class);
37+
3838
$this->mutex->expects($this->once())
3939
->method('lock')
4040
->willThrowException(new LockAcquireException());
4141

42-
$this->mutex->synchronized(function () {
42+
$this->mutex->synchronized(function (): void {
4343
$this->fail('Should not execute code.');
4444
});
4545
}
4646

4747
/**
4848
* Tests unlock() is called after the code was executed.
49-
*
5049
*/
5150
public function testUnlockAfterCode()
5251
{
@@ -74,11 +73,11 @@ public function testUnlockAfterException()
7473

7574
/**
7675
* Tests unlock() fails after the code was executed.
77-
*
78-
* @expectedException malkusch\lock\exception\LockReleaseException
7976
*/
8077
public function testUnlockFailsAfterCode()
8178
{
79+
$this->expectException(LockReleaseException::class);
80+
8281
$this->mutex->expects($this->once())
8382
->method('unlock')
8483
->willThrowException(new LockReleaseException());
@@ -89,11 +88,11 @@ public function testUnlockFailsAfterCode()
8988

9089
/**
9190
* Tests unlock() fails after the code threw an exception.
92-
*
93-
* @expectedException malkusch\lock\exception\LockReleaseException
9491
*/
9592
public function testUnlockFailsAfterException()
9693
{
94+
$this->expectException(LockReleaseException::class);
95+
9796
$this->mutex->expects($this->once())
9897
->method('unlock')
9998
->willThrowException(new LockReleaseException());

0 commit comments

Comments
 (0)