diff --git a/src/mutex/MySQLMutex.php b/src/mutex/MySQLMutex.php index a2292318..8f3f2b48 100644 --- a/src/mutex/MySQLMutex.php +++ b/src/mutex/MySQLMutex.php @@ -6,6 +6,7 @@ use malkusch\lock\exception\LockAcquireException; use malkusch\lock\exception\TimeoutException; +use malkusch\lock\util\LockUtil; class MySQLMutex extends LockMutex { @@ -21,11 +22,13 @@ public function __construct(\PDO $PDO, string $name, float $timeout = 0) { $this->pdo = $PDO; - if (\strlen($name) > 64) { - throw new \InvalidArgumentException('The maximum length of the lock name is 64 characters'); + $namePrefix = LockUtil::getInstance()->getKeyPrefix() . ':'; + + if (\strlen($name) > 64 - \strlen($namePrefix)) { + throw new \InvalidArgumentException('The maximum length of the lock name is ' . (64 - \strlen($namePrefix)) . ' characters'); } - $this->name = $name; + $this->name = $namePrefix . $name; $this->timeout = $timeout; } diff --git a/tests/mutex/MySQLMutexTest.php b/tests/mutex/MySQLMutexTest.php new file mode 100644 index 00000000..8d06986b --- /dev/null +++ b/tests/mutex/MySQLMutexTest.php @@ -0,0 +1,64 @@ +pdo = $this->createMock(\PDO::class); + + $this->mutex = new MySQLMutex($this->pdo, 'test'); + } + + public function testAcquireLock(): void + { + $statement = $this->createMock(\PDOStatement::class); + + $this->pdo->expects(self::once()) + ->method('prepare') + ->with('SELECT GET_LOCK(?, ?)') + ->willReturn($statement); + + $statement->expects(self::once()) + ->method('execute') + ->with(['php-malkusch-lock:test', 0]); + + $statement->expects(self::once()) + ->method('fetch') + ->willReturn([1]); + + \Closure::bind(static fn ($mutex) => $mutex->lock(), null, MySQLMutex::class)($this->mutex); + } + + public function testReleaseLock(): void + { + $statement = $this->createMock(\PDOStatement::class); + + $this->pdo->expects(self::once()) + ->method('prepare') + ->with('DO RELEASE_LOCK(?)') + ->willReturn($statement); + + $statement->expects(self::once()) + ->method('execute') + ->with(['php-malkusch-lock:test']); + + \Closure::bind(static fn ($mutex) => $mutex->unlock(), null, MySQLMutex::class)($this->mutex); + } +} diff --git a/tests/mutex/PgAdvisoryLockMutexTest.php b/tests/mutex/PgAdvisoryLockMutexTest.php index 8c890734..ba933b20 100644 --- a/tests/mutex/PgAdvisoryLockMutexTest.php +++ b/tests/mutex/PgAdvisoryLockMutexTest.php @@ -24,7 +24,7 @@ protected function setUp(): void $this->pdo = $this->createMock(\PDO::class); - $this->mutex = new PgAdvisoryLockMutex($this->pdo, 'test' . uniqid()); + $this->mutex = new PgAdvisoryLockMutex($this->pdo, 'test'); } private function isPhpunit9x(): bool @@ -43,23 +43,21 @@ public function testAcquireLock(): void $statement->expects(self::once()) ->method('execute') - ->with( - self::logicalAnd( - new IsType(IsType::TYPE_ARRAY), - self::countOf(2), - self::callback(function (...$arguments): bool { - if ($this->isPhpunit9x()) { // https://github.com/sebastianbergmann/phpunit/issues/5891 - $arguments = $arguments[0]; - } - - foreach ($arguments as $v) { - self::assertIsInt($v); - } - - return true; - }) - ) - ); + ->with(self::logicalAnd( + new IsType(IsType::TYPE_ARRAY), + self::countOf(2), + self::callback(function (...$arguments): bool { + if ($this->isPhpunit9x()) { // https://github.com/sebastianbergmann/phpunit/issues/5891 + $arguments = $arguments[0]; + } + + foreach ($arguments as $v) { + self::assertIsInt($v); + } + + return true; + }) + )); \Closure::bind(static fn ($mutex) => $mutex->lock(), null, PgAdvisoryLockMutex::class)($this->mutex); } @@ -75,25 +73,23 @@ public function testReleaseLock(): void $statement->expects(self::once()) ->method('execute') - ->with( - self::logicalAnd( - new IsType(IsType::TYPE_ARRAY), - self::countOf(2), - self::callback(function (...$arguments): bool { - if ($this->isPhpunit9x()) { // https://github.com/sebastianbergmann/phpunit/issues/5891 - $arguments = $arguments[0]; - } - - foreach ($arguments as $v) { - self::assertLessThan(1 << 32, $v); - self::assertGreaterThan(-(1 << 32), $v); - self::assertIsInt($v); - } - - return true; - }) - ) - ); + ->with(self::logicalAnd( + new IsType(IsType::TYPE_ARRAY), + self::countOf(2), + self::callback(function (...$arguments): bool { + if ($this->isPhpunit9x()) { // https://github.com/sebastianbergmann/phpunit/issues/5891 + $arguments = $arguments[0]; + } + + foreach ($arguments as $v) { + self::assertLessThan(1 << 32, $v); + self::assertGreaterThan(-(1 << 32), $v); + self::assertIsInt($v); + } + + return true; + }) + )); \Closure::bind(static fn ($mutex) => $mutex->unlock(), null, PgAdvisoryLockMutex::class)($this->mutex); }