1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
<?php
declare(strict_types=1);
namespace malkusch\lock\Tests\mutex;
use malkusch\lock\exception\LockAcquireException;
use malkusch\lock\exception\LockReleaseException;
use malkusch\lock\mutex\PredisMutex;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Predis\ClientInterface;
use Predis\PredisException;
use Psr\Log\LoggerInterface;
interface ClientInterfaceWithSetAndEvalMethods extends ClientInterface
{
/**
* @return mixed
*/
public function eval();
/**
* @return mixed
*/
public function set();
}
/**
* @group redis
*/
#[Group('redis')]
class PredisMutexTest extends TestCase
{
/** @var ClientInterface&MockObject */
private $client;
/** @var PredisMutex */
private $mutex;
/** @var LoggerInterface&MockObject */
private $logger;
#[\Override]
protected function setUp(): void
{
parent::setUp();
$this->client = $this->createMock(ClientInterfaceWithSetAndEvalMethods::class);
$this->mutex = new PredisMutex([$this->client], 'test', 2.5);
$this->logger = $this->createMock(LoggerInterface::class);
$this->mutex->setLogger($this->logger);
}
/**
* Tests add() fails.
*/
public function testAddFailsToSetKey(): void
{
$this->client->expects(self::atLeastOnce())
->method('set')
->with('lock_test', self::isString(), 'PX', 3500, 'NX')
->willReturn(null);
$this->logger->expects(self::never())
->method('warning');
$this->expectException(LockAcquireException::class);
$this->mutex->synchronized(
static function (): void {
self::fail('Code execution is not expected');
}
);
}
/**
* Tests add() errors.
*/
public function testAddErrors(): void
{
$this->client->expects(self::atLeastOnce())
->method('set')
->with('lock_test', self::isString(), 'PX', 3500, 'NX')
->willThrowException($this->createMock(PredisException::class));
$this->logger->expects(self::once())
->method('warning')
->with('Could not set {key} = {token} at server #{index}', self::anything());
$this->expectException(LockAcquireException::class);
$this->mutex->synchronized(
static function () {
self::fail('Code execution is not expected');
}
);
}
public function testWorksNormally(): void
{
$this->client->expects(self::atLeastOnce())
->method('set')
->with('lock_test', self::isString(), 'PX', 3500, 'NX')
->willReturnSelf();
$this->client->expects(self::once())
->method('eval')
->with(self::anything(), 1, 'lock_test', self::isString())
->willReturn(true);
$executed = false;
$this->mutex->synchronized(static function () use (&$executed): void {
$executed = true;
});
self::assertTrue($executed);
}
/**
* Tests evalScript() fails.
*/
public function testEvalScriptFails(): void
{
$this->client->expects(self::atLeastOnce())
->method('set')
->with('lock_test', self::isString(), 'PX', 3500, 'NX')
->willReturnSelf();
$this->client->expects(self::once())
->method('eval')
->with(self::anything(), 1, 'lock_test', self::isString())
->willThrowException($this->createMock(PredisException::class));
$this->logger->expects(self::once())
->method('warning')
->with('Could not unset {key} = {token} at server #{index}', self::anything());
$executed = false;
$this->expectException(LockReleaseException::class);
$this->mutex->synchronized(static function () use (&$executed): void {
$executed = true;
});
self::assertTrue($executed);
}
}
|