Skip to content

Commit 1ebd25c

Browse files
authored
Improve abstract mutex classes naming (#68)
1 parent bbc300c commit 1ebd25c

19 files changed

+73
-62
lines changed

Diff for: README.md

+9-11
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ This library uses the namespace `Malkusch\Lock`.
4848

4949
### Mutex
5050

51-
The [`Malkusch\Lock\Mutex\Mutex`][5] class is an abstract class and provides the
52-
base API for this library.
51+
The [`Malkusch\Lock\Mutex\Mutex`][5] interface provides the base API for this library.
5352

5453
#### Mutex::synchronized()
5554

@@ -125,9 +124,9 @@ if ($newBalance === false) {
125124
}
126125
```
127126

128-
### Extracting code result after lock release exception
127+
#### Extracting code result after lock release exception
129128

130-
Mutex implementations based on [`Malkush\Lock\Mutex\LockMutex`][10] will throw
129+
Mutex implementations based on [`Malkush\Lock\Mutex\AbstractLockMutex`][10] will throw
131130
[`Malkusch\Lock\Exception\LockReleaseException`][11] in case of lock release
132131
problem, but the synchronized code block will be already executed at this point.
133132
In order to read the code result (or an exception thrown there),
@@ -160,9 +159,8 @@ try {
160159

161160
### Implementations
162161

163-
Because the [`Malkusch\Lock\Mutex\Mutex`](#mutex) class is an abstract class,
164-
you can choose from one of the provided implementations or create/extend your
165-
own implementation.
162+
You can choose from one of the provided [`Malkusch\Lock\Mutex\Mutex`](#mutex) interface
163+
implementations or create/extend your own implementation.
166164

167165
- [`FlockMutex`](#flockmutex)
168166
- [`MemcachedMutex`](#memcachedmutex)
@@ -171,7 +169,7 @@ own implementation.
171169
- [`SemaphoreMutex`](#semaphoremutex)
172170
- [`TransactionalMutex`](#transactionalmutex)
173171
- [`MySQLMutex`](#mysqlmutex)
174-
- [`PgAdvisoryLockMutex`](#pgadvisorylockmutex)
172+
- [`PostgreSQLMutex`](#PostgreSQLMutex)
175173

176174
#### FlockMutex
177175

@@ -342,9 +340,9 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
342340
});
343341
```
344342

345-
#### PgAdvisoryLockMutex
343+
#### PostgreSQLMutex
346344

347-
The **PgAdvisoryLockMutex** uses PostgreSQL's
345+
The **PostgreSQLMutex** uses PostgreSQL's
348346
[advisory locking](https://www.postgresql.org/docs/9.4/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS)
349347
functions.
350348

@@ -357,7 +355,7 @@ interrupted, the lock is automatically released.
357355
```php
358356
$pdo = new \PDO('pgsql:host=localhost;dbname=test', 'username');
359357

360-
$mutex = new PgAdvisoryLockMutex($pdo, 'balance');
358+
$mutex = new PostgreSQLMutex($pdo, 'balance');
361359
$mutex->synchronized(function () use ($bankAccount, $amount) {
362360
$balance = $bankAccount->getBalance();
363361
$balance -= $amount;

Diff for: src/Exception/ExecutionOutsideLockException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
* Should only be used in contexts where the is being released.
1616
*
17-
* @see \Malkusch\Lock\Mutex\SpinlockMutex::unlock()
17+
* @see \Malkusch\Lock\Mutex\AbstractSpinlockMutex::unlock()
1818
*/
1919
class ExecutionOutsideLockException extends LockReleaseException
2020
{

Diff for: src/Mutex/LockMutex.php renamed to src/Mutex/AbstractLockMutex.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
* @internal
1414
*/
15-
abstract class LockMutex extends Mutex
15+
abstract class AbstractLockMutex extends AbstractMutex
1616
{
1717
/**
1818
* Acquires the lock.

Diff for: src/Mutex/AbstractMutex.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Malkusch\Lock\Mutex;
6+
7+
use Malkusch\Lock\Util\DoubleCheckedLocking;
8+
9+
abstract class AbstractMutex implements Mutex
10+
{
11+
#[\Override]
12+
public function check(callable $check): DoubleCheckedLocking
13+
{
14+
return new DoubleCheckedLocking($this, $check);
15+
}
16+
}

Diff for: src/Mutex/SpinlockMutex.php renamed to src/Mutex/AbstractSpinlockMutex.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
* @internal
1717
*/
18-
abstract class SpinlockMutex extends LockMutex
18+
abstract class AbstractSpinlockMutex extends AbstractLockMutex
1919
{
2020
/** @var float The timeout in seconds a lock may live */
2121
private $timeout;

Diff for: src/Mutex/FlockMutex.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* Flock() based mutex implementation.
1616
*/
17-
class FlockMutex extends LockMutex
17+
class FlockMutex extends AbstractLockMutex
1818
{
1919
public const INFINITE_TIMEOUT = -1.0;
2020

Diff for: src/Mutex/MemcachedMutex.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* Memcached based spinlock implementation.
99
*/
10-
class MemcachedMutex extends SpinlockMutex
10+
class MemcachedMutex extends AbstractSpinlockMutex
1111
{
1212
/** @var \Memcached */
1313
private $memcache;

Diff for: src/Mutex/Mutex.php

+4-7
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
use Malkusch\Lock\Util\DoubleCheckedLocking;
1111

1212
/**
13-
* The mutex provides methods for exclusive execution.
13+
* Mutex interface for exclusive execution.
1414
*/
15-
abstract class Mutex
15+
interface Mutex
1616
{
1717
/**
1818
* Executes a block of code exclusively.
@@ -35,7 +35,7 @@ abstract class Mutex
3535
* @throws LockReleaseException The mutex could not be released, the code was already executed
3636
* @throws ExecutionOutsideLockException Some code has been executed outside of the lock
3737
*/
38-
abstract public function synchronized(callable $code);
38+
public function synchronized(callable $code);
3939

4040
/**
4141
* Performs a double-checked locking pattern.
@@ -57,8 +57,5 @@ abstract public function synchronized(callable $code);
5757
*
5858
* @return DoubleCheckedLocking The double-checked locking pattern
5959
*/
60-
public function check(callable $check): DoubleCheckedLocking
61-
{
62-
return new DoubleCheckedLocking($this, $check);
63-
}
60+
public function check(callable $check): DoubleCheckedLocking;
6461
}

Diff for: src/Mutex/MySQLMutex.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Malkusch\Lock\Exception\TimeoutException;
99
use Malkusch\Lock\Util\LockUtil;
1010

11-
class MySQLMutex extends LockMutex
11+
class MySQLMutex extends AbstractLockMutex
1212
{
1313
/** @var \PDO */
1414
private $pdo;

Diff for: src/Mutex/NoMutex.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Synchronization is not provided! This mutex is just implementing the
1111
* interface without locking.
1212
*/
13-
class NoMutex extends Mutex
13+
class NoMutex extends AbstractMutex
1414
{
1515
#[\Override]
1616
public function synchronized(callable $code)

Diff for: src/Mutex/PgAdvisoryLockMutex.php renamed to src/Mutex/PostgreSQLMutex.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use Malkusch\Lock\Util\LockUtil;
88

9-
class PgAdvisoryLockMutex extends LockMutex
9+
class PostgreSQLMutex extends AbstractLockMutex
1010
{
1111
/** @var \PDO */
1212
private $pdo;

Diff for: src/Mutex/RedisMutex.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @see http://redis.io/topics/distlock
1818
*/
19-
abstract class RedisMutex extends SpinlockMutex implements LoggerAwareInterface
19+
abstract class RedisMutex extends AbstractSpinlockMutex implements LoggerAwareInterface
2020
{
2121
use LoggerAwareTrait;
2222

Diff for: src/Mutex/SemaphoreMutex.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* System V IPC based mutex implementation.
1212
*/
13-
class SemaphoreMutex extends LockMutex
13+
class SemaphoreMutex extends AbstractLockMutex
1414
{
1515
/** @var \SysvSemaphore|resource The semaphore id */
1616
private $semaphore;

Diff for: src/Mutex/TransactionalMutex.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
* A failing transaction will be replayed.
1717
*/
18-
class TransactionalMutex extends Mutex
18+
class TransactionalMutex extends AbstractMutex
1919
{
2020
/** @var \PDO */
2121
private $pdo;

Diff for: tests/Mutex/LockMutexTest.php renamed to tests/Mutex/AbstractLockMutexTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@
66

77
use Malkusch\Lock\Exception\LockAcquireException;
88
use Malkusch\Lock\Exception\LockReleaseException;
9-
use Malkusch\Lock\Mutex\LockMutex;
9+
use Malkusch\Lock\Mutex\AbstractLockMutex;
1010
use PHPUnit\Framework\MockObject\MockObject;
1111
use PHPUnit\Framework\TestCase;
1212

13-
class LockMutexTest extends TestCase
13+
class AbstractLockMutexTest extends TestCase
1414
{
15-
/** @var LockMutex&MockObject */
15+
/** @var AbstractLockMutex&MockObject */
1616
private $mutex;
1717

1818
#[\Override]
1919
protected function setUp(): void
2020
{
2121
parent::setUp();
2222

23-
$this->mutex = $this->getMockBuilder(LockMutex::class)
23+
$this->mutex = $this->getMockBuilder(AbstractLockMutex::class)
2424
->onlyMethods(['lock', 'unlock'])
2525
->getMock();
2626
}

Diff for: tests/Mutex/SpinlockMutexTest.php renamed to tests/Mutex/AbstractSpinlockMutexTest.php

+11-11
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
use Malkusch\Lock\Exception\LockAcquireException;
99
use Malkusch\Lock\Exception\LockReleaseException;
1010
use Malkusch\Lock\Exception\TimeoutException;
11-
use Malkusch\Lock\Mutex\SpinlockMutex;
11+
use Malkusch\Lock\Mutex\AbstractSpinlockMutex;
1212
use phpmock\environment\SleepEnvironmentBuilder;
1313
use phpmock\MockEnabledException;
1414
use phpmock\phpunit\PHPMock;
1515
use PHPUnit\Framework\MockObject\MockObject;
1616
use PHPUnit\Framework\TestCase;
1717

18-
class SpinlockMutexTest extends TestCase
18+
class AbstractSpinlockMutexTest extends TestCase
1919
{
2020
use PHPMock;
2121

@@ -39,11 +39,11 @@ protected function setUp(): void
3939
}
4040

4141
/**
42-
* @return SpinlockMutex&MockObject
42+
* @return AbstractSpinlockMutex&MockObject
4343
*/
44-
private function createSpinlockMutexMock(float $timeout = 3): SpinlockMutex
44+
private function createAbstractSpinlockMutexMock(float $timeout = 3): AbstractSpinlockMutex
4545
{
46-
return $this->getMockBuilder(SpinlockMutex::class)
46+
return $this->getMockBuilder(AbstractSpinlockMutex::class)
4747
->setConstructorArgs(['test', $timeout])
4848
->onlyMethods(['acquire', 'release'])
4949
->getMock();
@@ -56,7 +56,7 @@ public function testFailAcquireLock(): void
5656
{
5757
$this->expectException(LockAcquireException::class);
5858

59-
$mutex = $this->createSpinlockMutexMock();
59+
$mutex = $this->createAbstractSpinlockMutexMock();
6060
$mutex->expects(self::any())
6161
->method('acquire')
6262
->willThrowException(new LockAcquireException());
@@ -74,7 +74,7 @@ public function testAcquireTimesOut(): void
7474
$this->expectException(TimeoutException::class);
7575
$this->expectExceptionMessage('Timeout of 3.0 seconds exceeded');
7676

77-
$mutex = $this->createSpinlockMutexMock();
77+
$mutex = $this->createAbstractSpinlockMutexMock();
7878
$mutex->expects(self::atLeastOnce())
7979
->method('acquire')
8080
->willReturn(false);
@@ -89,7 +89,7 @@ public function testAcquireTimesOut(): void
8989
*/
9090
public function testExecuteTooLong(): void
9191
{
92-
$mutex = $this->createSpinlockMutexMock(0.5);
92+
$mutex = $this->createAbstractSpinlockMutexMock(0.5);
9393
$mutex->expects(self::any())
9494
->method('acquire')
9595
->willReturn(true);
@@ -114,7 +114,7 @@ public function testExecuteTooLong(): void
114114
*/
115115
public function testExecuteBarelySucceeds(): void
116116
{
117-
$mutex = $this->createSpinlockMutexMock(0.5);
117+
$mutex = $this->createAbstractSpinlockMutexMock(0.5);
118118
$mutex->expects(self::any())->method('acquire')->willReturn(true);
119119
$mutex->expects(self::once())->method('release')->willReturn(true);
120120

@@ -130,7 +130,7 @@ public function testFailReleasingLock(): void
130130
{
131131
$this->expectException(LockReleaseException::class);
132132

133-
$mutex = $this->createSpinlockMutexMock();
133+
$mutex = $this->createAbstractSpinlockMutexMock();
134134
$mutex->expects(self::any())->method('acquire')->willReturn(true);
135135
$mutex->expects(self::any())->method('release')->willReturn(false);
136136

@@ -142,7 +142,7 @@ public function testFailReleasingLock(): void
142142
*/
143143
public function testExecuteTimeoutLeavesOneSecondForKeyToExpire(): void
144144
{
145-
$mutex = $this->createSpinlockMutexMock(0.2);
145+
$mutex = $this->createAbstractSpinlockMutexMock(0.2);
146146
$mutex->expects(self::once())
147147
->method('acquire')
148148
->with(self::anything(), 1.2)

Diff for: tests/Mutex/MutexConcurrencyTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
use Malkusch\Lock\Mutex\MemcachedMutex;
1010
use Malkusch\Lock\Mutex\Mutex;
1111
use Malkusch\Lock\Mutex\MySQLMutex;
12-
use Malkusch\Lock\Mutex\PgAdvisoryLockMutex;
1312
use Malkusch\Lock\Mutex\PHPRedisMutex;
13+
use Malkusch\Lock\Mutex\PostgreSQLMutex;
1414
use Malkusch\Lock\Mutex\PredisMutex;
1515
use Malkusch\Lock\Mutex\SemaphoreMutex;
1616
use Malkusch\Lock\Mutex\TransactionalMutex;
@@ -325,11 +325,11 @@ static function (string $uri): \Redis {
325325
}
326326

327327
if (getenv('PGSQL_DSN')) {
328-
yield 'PgAdvisoryLockMutex' => [static function (): Mutex {
328+
yield 'PostgreSQLMutex' => [static function (): Mutex {
329329
$pdo = new \PDO(getenv('PGSQL_DSN'), getenv('PGSQL_USER'), getenv('PGSQL_PASSWORD'));
330330
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
331331

332-
return new PgAdvisoryLockMutex($pdo, 'test');
332+
return new PostgreSQLMutex($pdo, 'test');
333333
}];
334334
}
335335
}

0 commit comments

Comments
 (0)