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
|
<?php
declare(strict_types=1);
namespace malkusch\lock\Tests\mutex;
use malkusch\lock\mutex\FlockMutex;
use malkusch\lock\mutex\LockMutex;
use malkusch\lock\mutex\MemcachedMutex;
use malkusch\lock\mutex\Mutex;
use malkusch\lock\mutex\MySQLMutex;
use malkusch\lock\mutex\NoMutex;
use malkusch\lock\mutex\PgAdvisoryLockMutex;
use malkusch\lock\mutex\PHPRedisMutex;
use malkusch\lock\mutex\PredisMutex;
use malkusch\lock\mutex\SemaphoreMutex;
use malkusch\lock\mutex\SpinlockMutex;
use malkusch\lock\mutex\TransactionalMutex;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
use PHPUnit\Framework\TestCase;
use Predis\Client;
/**
* If you want to run integrations tests you should provide these environment variables:
*
* - MEMCACHE_HOST
* - REDIS_URIS - a comma separated list of redis:// URIs.
*/
class MutexTest extends TestCase
{
protected const TIMEOUT = 4;
#[\Override]
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
vfsStream::setup('test');
}
/**
* Provides Mutex factories.
*
* @return iterable<list<mixed>>
*/
public static function provideMutexFactoriesCases(): iterable
{
yield 'NoMutex' => [static function (): Mutex {
return new NoMutex();
}];
yield 'TransactionalMutex' => [static function (): Mutex {
$pdo = new \PDO('sqlite::memory:');
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
return new TransactionalMutex($pdo, self::TIMEOUT);
}];
yield 'FlockMutex' => [static function (): Mutex {
$file = fopen(vfsStream::url('test/lock'), 'w');
return new FlockMutex($file);
}];
yield 'SemaphoreMutex' => [static function (): Mutex {
return new SemaphoreMutex(sem_get(ftok(__FILE__, 'a')));
}];
yield 'SpinlockMutex' => [static function (): Mutex {
$lock = new class('test') extends SpinlockMutex {
#[\Override]
protected function acquire(string $key, float $expire): bool
{
return true;
}
#[\Override]
protected function release(string $key): bool
{
return true;
}
};
return $lock;
}];
yield 'LockMutex' => [static function (): Mutex {
$lock = new class extends LockMutex {
#[\Override]
protected function lock(): void {}
#[\Override]
protected function unlock(): void {}
};
return $lock;
}];
if (getenv('MEMCACHE_HOST')) {
yield 'MemcachedMutex' => [static function (): Mutex {
$memcache = new \Memcached();
$memcache->addServer(getenv('MEMCACHE_HOST'), 11211);
return new MemcachedMutex('test', $memcache, self::TIMEOUT);
}];
}
if (getenv('REDIS_URIS')) {
$uris = explode(',', getenv('REDIS_URIS'));
yield 'PredisMutex' => [static function () use ($uris): Mutex {
$clients = array_map(
static fn ($uri) => new Client($uri),
$uris
);
return new PredisMutex($clients, 'test', self::TIMEOUT);
}];
if (class_exists(\Redis::class)) {
yield 'PHPRedisMutex' => [
static function () use ($uris): Mutex {
$apis = array_map(
static function ($uri) {
$redis = new \Redis();
$uri = parse_url($uri);
$redis->connect($uri['host'], $uri['port'] ?? 6379);
if (!empty($uri['pass'])) {
$redis->auth(
empty($uri['user'])
? $uri['pass']
: [$uri['user'], $uri['pass']]
);
}
return $redis;
},
$uris
);
return new PHPRedisMutex($apis, 'test', self::TIMEOUT);
},
];
}
}
if (getenv('MYSQL_DSN')) {
yield 'MySQLMutex' => [static function (): Mutex {
$pdo = new \PDO(getenv('MYSQL_DSN'), getenv('MYSQL_USER'), getenv('MYSQL_PASSWORD'));
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
return new MySQLMutex($pdo, 'test' . time(), self::TIMEOUT);
}];
}
if (getenv('PGSQL_DSN')) {
yield 'PgAdvisoryLockMutex' => [static function (): Mutex {
$pdo = new \PDO(getenv('PGSQL_DSN'), getenv('PGSQL_USER'), getenv('PGSQL_PASSWORD'));
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
return new PgAdvisoryLockMutex($pdo, 'test');
}];
}
}
/**
* Tests synchronized() executes the code and returns its result.
*
* @param \Closure(): Mutex $mutexFactory
*
* @dataProvider provideMutexFactoriesCases
*/
#[DataProvider('provideMutexFactoriesCases')]
public function testSynchronizedDelegates(\Closure $mutexFactory): void
{
$mutex = $mutexFactory();
$result = $mutex->synchronized(static function (): string {
return 'test';
});
self::assertSame('test', $result);
}
/**
* Tests that synchronized() released the lock.
*
* @param \Closure(): Mutex $mutexFactory
*
* @doesNotPerformAssertions
*
* @dataProvider provideMutexFactoriesCases
*/
#[DoesNotPerformAssertions]
#[DataProvider('provideMutexFactoriesCases')]
public function testRelease(\Closure $mutexFactory): void
{
$mutex = $mutexFactory();
$mutex->synchronized(static function () {});
$mutex->synchronized(static function () {});
}
/**
* Tests synchronized() rethrows the exception of the code.
*
* @param \Closure(): Mutex $mutexFactory
*
* @dataProvider provideMutexFactoriesCases
*/
#[DataProvider('provideMutexFactoriesCases')]
public function testSynchronizedPassesExceptionThrough(\Closure $mutexFactory): void
{
$this->expectException(\DomainException::class);
$mutex = $mutexFactory();
$mutex->synchronized(static function () {
throw new \DomainException();
});
}
}
|