-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implemented generic
LockedResource
objects
- Loading branch information
Showing
13 changed files
with
295 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CriticalSection\Exception; | ||
|
||
use PetrKnap\Shorts\Exception\CouldNotProcessData; | ||
|
||
/** | ||
* @template T of mixed | ||
* | ||
* @extends CouldNotProcessData<T> | ||
*/ | ||
final class CouldNotGetUnlockedResource extends CouldNotProcessData implements LockedResourceException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CriticalSection\Exception; | ||
|
||
use PetrKnap\Shorts\ExceptionWrapper; | ||
use RuntimeException; | ||
|
||
final class CouldNotLockResource extends RuntimeException implements LockableResourceException | ||
{ | ||
use ExceptionWrapper; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CriticalSection\Exception; | ||
|
||
use PetrKnap\Shorts\ExceptionWrapper; | ||
use RuntimeException; | ||
|
||
final class CouldNotUnlockResource extends RuntimeException implements LockableResourceException | ||
{ | ||
use ExceptionWrapper; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CriticalSection\Exception; | ||
|
||
use Throwable; | ||
|
||
interface LockableResourceException extends Throwable | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CriticalSection\Exception; | ||
|
||
interface LockedResourceException extends LockableResourceException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CriticalSection; | ||
|
||
use Symfony\Component\Lock\LockInterface; | ||
|
||
/** | ||
* @template T of mixed | ||
*/ | ||
abstract class LockableResource | ||
{ | ||
/** | ||
* @param T $resource | ||
* | ||
* @return LockedResource<T> | ||
*/ | ||
public static function create( | ||
mixed $resource, | ||
LockInterface $lock1, | ||
LockInterface ...$lockN, | ||
): LockedResource { | ||
return new Symfony\Lock\LockedResource($resource, $lock1, ...$lockN); | ||
} | ||
|
||
/** | ||
* @return T | ||
*/ | ||
abstract public function get(): mixed; | ||
|
||
abstract public function isLocked(): bool; | ||
|
||
/** | ||
* @throws Exception\CouldNotLockResource | ||
*/ | ||
abstract public function lock(): void; | ||
|
||
/** | ||
* @throws Exception\CouldNotUnlockResource | ||
*/ | ||
abstract public function unlock(): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CriticalSection; | ||
|
||
use LogicException; | ||
|
||
/** | ||
* @template T of mixed | ||
* | ||
* @extends LockableResource<T> | ||
*/ | ||
abstract class LockedResource extends LockableResource | ||
{ | ||
/** | ||
* @param T $resource | ||
*/ | ||
public function __construct( | ||
private readonly mixed $resource, | ||
) { | ||
} | ||
|
||
/** | ||
* @return T | ||
* | ||
* @throws Exception\CouldNotGetUnlockedResource<T> | ||
*/ | ||
final public function get(): mixed | ||
{ | ||
if (!$this->isLocked()) { | ||
throw new Exception\CouldNotGetUnlockedResource( | ||
__METHOD__, | ||
$this->resource, | ||
new LogicException('Resource is not locked'), | ||
); | ||
} | ||
return $this->resource; | ||
} | ||
|
||
/** | ||
* @deprecated locked externally | ||
*/ | ||
final public function lock(): void | ||
{ | ||
throw new Exception\CouldNotLockResource(new LogicException(self::class . ' is locked externally')); | ||
} | ||
|
||
/** | ||
* @deprecated locked externally | ||
*/ | ||
final public function unlock(): void | ||
{ | ||
throw new Exception\CouldNotUnlockResource(new LogicException(self::class . ' is locked externally')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CriticalSection\Symfony\Lock; | ||
|
||
use PetrKnap\CriticalSection\LockedResource as BaseLockedResource; | ||
use Symfony\Component\Lock\LockInterface; | ||
|
||
/** | ||
* @template T of mixed | ||
* | ||
* @extends LockedResource<T> | ||
*/ | ||
final class LockedResource extends BaseLockedResource | ||
{ | ||
/** | ||
* @var array<LockInterface> | ||
*/ | ||
private readonly array $locks; | ||
|
||
/** | ||
* @param T $resource | ||
*/ | ||
public function __construct( | ||
mixed $resource, | ||
LockInterface $lock1, | ||
LockInterface ...$lockN, | ||
) { | ||
parent::__construct($resource); | ||
$this->locks = [$lock1, ...$lockN]; | ||
} | ||
|
||
public function isLocked(): bool | ||
{ | ||
foreach ($this->locks as $lock) { | ||
if (!$lock->isAcquired()) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CriticalSection; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use stdClass; | ||
|
||
abstract class LockedResourceTestCase extends TestCase | ||
{ | ||
public function testCouldNotGetUnlockedResource(): void | ||
{ | ||
self::expectException(Exception\CouldNotGetUnlockedResource::class); | ||
|
||
$this->getUnlockedResource(null)->get(); | ||
} | ||
|
||
public function testCouldGetLockedResource(): void | ||
{ | ||
$instance = new stdClass(); | ||
|
||
self::assertSame( | ||
$instance, | ||
$this->getLockedResource($instance)->get(), | ||
); | ||
} | ||
|
||
public function testCouldNotLockItself(): void | ||
{ | ||
self::expectException(Exception\CouldNotLockResource::class); | ||
|
||
$this->getUnlockedResource(null)->lock(); | ||
} | ||
|
||
public function testCouldNotUnlockItself(): void | ||
{ | ||
self::expectException(Exception\CouldNotLockResource::class); | ||
|
||
$this->getLockedResource(null)->unlock(); | ||
} | ||
|
||
abstract protected function getUnlockedResource(mixed $resource): LockedResource; | ||
abstract protected function getLockedResource(mixed $resource): LockedResource; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CriticalSection\Symfony\Lock; | ||
|
||
use PetrKnap\CriticalSection\LockedResourceTestCase; | ||
use Symfony\Component\Lock\LockInterface; | ||
|
||
final class LockedResourceTest extends LockedResourceTestCase | ||
{ | ||
protected function getUnlockedResource(mixed $resource): LockedResource | ||
{ | ||
$lock = $this->createMock(LockInterface::class); | ||
$lock->expects(self::any())->method('isAcquired')->willReturn(false); | ||
return new LockedResource($resource, $lock); | ||
} | ||
|
||
protected function getLockedResource(mixed $resource): LockedResource | ||
{ | ||
$lock = $this->createMock(LockInterface::class); | ||
$lock->expects(self::any())->method('isAcquired')->willReturn(true); | ||
return new LockedResource($resource, $lock); | ||
} | ||
} |