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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
<?php
declare(strict_types=1);
namespace malkusch\lock\Tests\mutex;
use malkusch\lock\exception\LockAcquireException;
use malkusch\lock\exception\LockReleaseException;
use malkusch\lock\exception\MutexException;
use malkusch\lock\exception\TimeoutException;
use malkusch\lock\mutex\RedisMutex;
use phpmock\environment\SleepEnvironmentBuilder;
use phpmock\MockEnabledException;
use phpmock\phpunit\PHPMock;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
/**
* @group redis
*/
#[Group('redis')]
class RedisMutexTest extends TestCase
{
use PHPMock;
#[\Override]
protected function setUp(): void
{
parent::setUp();
$sleepBuilder = new SleepEnvironmentBuilder();
$sleepBuilder->addNamespace(__NAMESPACE__);
$sleepBuilder->addNamespace('malkusch\lock\mutex');
$sleepBuilder->addNamespace('malkusch\lock\util');
$sleep = $sleepBuilder->build();
try {
$sleep->enable();
$this->registerForTearDown($sleep);
} catch (MockEnabledException $e) {
// workaround for burn testing
\assert($e->getMessage() === 'microtime is already enabled.Call disable() on the existing mock.');
}
}
/**
* @param int $count The amount of redis apis
*
* @return RedisMutex&MockObject
*/
private function createRedisMutexMock(int $count, float $timeout = 1): RedisMutex
{
$redisAPIs = array_map(
static fn ($id) => ['id' => $id],
range(1, $count)
);
return $this->getMockBuilder(RedisMutex::class)
->setConstructorArgs([$redisAPIs, 'test', $timeout])
->onlyMethods(['add', 'evalScript'])
->getMock();
}
/**
* Tests acquire() fails because too few servers are available.
*
* @param int $count The total count of servers
* @param int $available The count of available servers
*
* @dataProvider provideMinorityCases
*/
#[DataProvider('provideMinorityCases')]
public function testTooFewServerToAcquire(int $count, int $available): void
{
$this->expectException(LockAcquireException::class);
$this->expectExceptionCode(MutexException::REDIS_NOT_ENOUGH_SERVERS);
$mutex = $this->createRedisMutexMock($count);
$i = 0;
$mutex->expects(self::exactly($count))
->method('add')
->willReturnCallback(
static function () use (&$i, $available): bool {
if ($i < $available) {
++$i;
return true;
}
throw new LockAcquireException();
}
);
$mutex->synchronized(static function (): void {
self::fail('Code should not be executed');
});
}
/**
* Tests synchronized() does work if the majority of servers is up.
*
* @param int $count The total count of servers
* @param int $available The count of available servers
*
* @dataProvider provideMajorityCases
*/
#[DataProvider('provideMajorityCases')]
public function testFaultTolerance(int $count, int $available): void
{
$mutex = $this->createRedisMutexMock($count);
$mutex->expects(self::exactly($count))
->method('evalScript')
->willReturn(true);
$i = 0;
$mutex->expects(self::exactly($count))
->method('add')
->willReturnCallback(
static function () use (&$i, $available): bool {
if ($i < $available) {
++$i;
return true;
}
throw new LockAcquireException();
}
);
$mutex->synchronized(static function () {});
}
/**
* Tests too few keys could be acquired.
*
* @param int $count The total count of servers
* @param int $available The count of available servers
*
* @dataProvider provideMinorityCases
*/
#[DataProvider('provideMinorityCases')]
public function testAcquireTooFewKeys(int $count, int $available): void
{
$this->expectException(TimeoutException::class);
$this->expectExceptionMessage('Timeout of 1.0 seconds exceeded');
$mutex = $this->createRedisMutexMock($count);
$i = 0;
$mutex->expects(self::any())
->method('add')
->willReturnCallback(
static function () use (&$i, $available): bool {
++$i;
return $i <= $available;
}
);
$mutex->synchronized(static function (): void {
self::fail('Code should not be executed');
});
}
/**
* Tests acquiring keys takes too long.
*
* @param int $count The total count of servers
* @param float $timeout The timeout in seconds
* @param float $delay The delay in seconds
*
* @dataProvider provideTimingOutCases
*/
#[DataProvider('provideTimingOutCases')]
public function testTimingOut(int $count, float $timeout, float $delay): void
{
$timeoutStr = (string) round($timeout, 6);
if (strpos($timeoutStr, '.') === false) {
$timeoutStr .= '.0';
}
$this->expectException(TimeoutException::class);
$this->expectExceptionMessage('Timeout of ' . $timeoutStr . ' seconds exceeded');
$mutex = $this->createRedisMutexMock($count, $timeout);
$mutex->expects(self::exactly($count))
->method('add')
->willReturnCallback(static function () use ($delay): bool {
usleep((int) ($delay * 1e6));
return true;
});
$mutex->synchronized(static function (): void {
self::fail('Code should not be executed');
});
}
/**
* @return iterable<list<mixed>>
*/
public static function provideTimingOutCases(): iterable
{
yield [1, 1.2 - 1, 1.201];
yield [2, 1.2 - 1, 1.401];
}
/**
* Tests synchronized() works if the majority of keys was acquired.
*
* @param int $count The total count of servers
* @param int $available The count of available servers
*
* @dataProvider provideMajorityCases
*/
#[DataProvider('provideMajorityCases')]
public function testAcquireWithMajority(int $count, int $available): void
{
$mutex = $this->createRedisMutexMock($count);
$mutex->expects(self::exactly($count))
->method('evalScript')
->willReturn(true);
$i = 0;
$mutex->expects(self::exactly($count))
->method('add')
->willReturnCallback(
static function () use (&$i, $available): bool {
++$i;
return $i <= $available;
}
);
$mutex->synchronized(static function (): void {});
}
/**
* Tests releasing fails because too few servers are available.
*
* @param int $count The total count of servers
* @param int $available The count of available servers
*
* @dataProvider provideMinorityCases
*/
#[DataProvider('provideMinorityCases')]
public function testTooFewServersToRelease(int $count, int $available): void
{
$mutex = $this->createRedisMutexMock($count);
$mutex->expects(self::exactly($count))
->method('add')
->willReturn(true);
$i = 0;
$mutex->expects(self::exactly($count))
->method('evalScript')
->willReturnCallback(
static function () use (&$i, $available): bool {
if ($i < $available) {
++$i;
return true;
}
throw new LockReleaseException();
}
);
$this->expectException(LockReleaseException::class);
$mutex->synchronized(static function (): void {});
}
/**
* Tests releasing too few keys.
*
* @param int $count The total count of servers
* @param int $available The count of available servers
*
* @dataProvider provideMinorityCases
*/
#[DataProvider('provideMinorityCases')]
public function testReleaseTooFewKeys(int $count, int $available): void
{
$mutex = $this->createRedisMutexMock($count);
$mutex->expects(self::exactly($count))
->method('add')
->willReturn(true);
$i = 0;
$mutex->expects(self::exactly($count))
->method('evalScript')
->willReturnCallback(
static function () use (&$i, $available): bool {
++$i;
return $i <= $available;
}
);
$this->expectException(LockReleaseException::class);
$mutex->synchronized(static function (): void {});
}
/**
* Provides test cases with too few.
*
* @return iterable<list<mixed>>
*/
public static function provideMinorityCases(): iterable
{
yield [1, 0];
yield [2, 0];
yield [2, 1];
yield [3, 0];
yield [3, 1];
yield [4, 0];
yield [4, 1];
yield [4, 2];
}
/**
* Provides test cases with enough.
*
* @return iterable<list<mixed>>
*/
public static function provideMajorityCases(): iterable
{
yield [1, 1];
yield [2, 2];
yield [3, 2];
yield [3, 3];
yield [4, 3];
}
}
|