-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
245 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace malkusch\lock\mutex; | ||
|
||
use malkusch\lock\exception\LockAcquireException; | ||
use malkusch\lock\exception\TimeoutException; | ||
|
||
class PgAdvisoryLockMutex extends LockMutex | ||
{ | ||
/** | ||
* @var \PDO | ||
*/ | ||
private $pdo; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $key1; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $key2; | ||
|
||
/** | ||
* @throws \RuntimeException | ||
*/ | ||
public function __construct(\PDO $PDO, $name) | ||
{ | ||
$this->pdo = $PDO; | ||
|
||
$hashed_name = hash("sha256", $name, true); | ||
|
||
if (false === $hashed_name) { | ||
throw new \RuntimeException("Unable to hash the key, sha256 algorithm is not supported."); | ||
} | ||
|
||
list($bytes1, $bytes2) = str_split($hashed_name, 4); | ||
|
||
$this->key1 = unpack("i", $bytes1)[1]; | ||
$this->key2 = unpack("i", $bytes2)[1]; | ||
} | ||
|
||
public function lock() | ||
{ | ||
$statement = $this->pdo->prepare("SELECT pg_advisory_lock(?,?)"); | ||
|
||
$statement->execute([ | ||
$this->key1, | ||
$this->key2, | ||
]); | ||
} | ||
|
||
public function unlock() | ||
{ | ||
$statement = $this->pdo->prepare("SELECT pg_advisory_unlock(?,?)"); | ||
$statement->execute([ | ||
$this->key1, | ||
$this->key2 | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
namespace malkusch\lock\mutex; | ||
|
||
use malkusch\lock\exception\LockAcquireException; | ||
use malkusch\lock\exception\LockReleaseException; | ||
|
||
/** | ||
* @author Willem Stuursma-Ruwen <[email protected]> | ||
* @link bitcoin:1P5FAZ4QhXCuwYPnLZdk3PJsqePbu1UDDA Donations | ||
* @license WTFPL | ||
*/ | ||
class PgAdvisoryLockMutexTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var \PDO|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $pdo; | ||
|
||
/** | ||
* @var PgAdvisoryLockMutex | ||
*/ | ||
private $mutex; | ||
|
||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->pdo = $this->createMock(\PDO::class); | ||
|
||
$this->mutex = new PgAdvisoryLockMutex($this->pdo, "test" . uniqid()); | ||
} | ||
|
||
public function testAcquireLock() | ||
{ | ||
$statement = $this->createMock(\PDOStatement::class); | ||
|
||
$this->pdo->expects($this->once()) | ||
->method("prepare") | ||
->with("SELECT pg_advisory_lock(?,?)") | ||
->willReturn($statement); | ||
|
||
$statement->expects($this->once()) | ||
->method("execute") | ||
->with( | ||
$this->logicalAnd( | ||
$this->isType("array"), | ||
$this->countOf(2), | ||
$this->callback(function (...$arguments) { | ||
$integers = $arguments[0]; | ||
|
||
foreach ($integers as $each) { | ||
$this->assertInternalType("integer", $each); | ||
} | ||
|
||
return true; | ||
}) | ||
) | ||
); | ||
|
||
$this->mutex->lock(); | ||
} | ||
|
||
public function testReleaseLock() | ||
{ | ||
$statement = $this->createMock(\PDOStatement::class); | ||
|
||
$this->pdo->expects($this->once()) | ||
->method("prepare") | ||
->with("SELECT pg_advisory_unlock(?,?)") | ||
->willReturn($statement); | ||
|
||
$statement->expects($this->once()) | ||
->method("execute") | ||
->with( | ||
$this->logicalAnd( | ||
$this->isType("array"), | ||
$this->countOf(2), | ||
$this->callback(function (...$arguments) { | ||
$integers = $arguments[0]; | ||
|
||
foreach ($integers as $each) { | ||
$this->assertLessThan(1 << 32, $each); | ||
$this->assertGreaterThan(-(1 << 32), $each); | ||
$this->assertInternalType("integer", $each); | ||
} | ||
|
||
return true; | ||
}) | ||
) | ||
); | ||
|
||
$this->mutex->unlock(); | ||
} | ||
} |
Oops, something went wrong.