Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/mutex/MySQLMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use malkusch\lock\exception\LockAcquireException;
use malkusch\lock\exception\TimeoutException;
use malkusch\lock\util\LockUtil;

class MySQLMutex extends LockMutex
{
Expand All @@ -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;
}

Expand Down
64 changes: 64 additions & 0 deletions tests/mutex/MySQLMutexTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace malkusch\lock\Tests\mutex;

use malkusch\lock\mutex\MySQLMutex;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class MySQLMutexTest extends TestCase
{
/** @var \PDO&MockObject */
private $pdo;

/** @var MySQLMutex */
private $mutex;

#[\Override]
protected function setUp(): void
{
parent::setUp();

$this->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);
}
}
70 changes: 33 additions & 37 deletions tests/mutex/PgAdvisoryLockMutexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down