Skip to content

Commit b7fe0a0

Browse files
committed
Leave only *Handler API
1 parent 9c08b96 commit b7fe0a0

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

src/Postgres/PostgresAdvisoryLocker.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,16 @@ public function acquireSessionLevelLock(
8181
* ⚠️ Transaction-level advisory locks are strongly preferred whenever possible,
8282
* as they are automatically released at the end of a transaction and are less error-prone.
8383
* Use session-level locks only when transactional context is not available.
84+
* @see acquireTransactionLevelLock() for preferred locking strategy.
85+
*
86+
* @template TReturn
8487
*
8588
* @param PDO $dbConnection Active database connection.
8689
* @param PostgresLockKey $key Lock key to be acquired.
8790
* @param callable(SessionLevelLockHandle): TReturn $callback A callback that receives the lock handle.
8891
* @param PostgresLockWaitModeEnum $waitMode Whether to wait for the lock or fail immediately. Default is non-blocking.
8992
* @param PostgresLockAccessModeEnum $accessMode Whether to acquire a shared or exclusive lock. Default is exclusive.
9093
* @return TReturn The return value of the callback.
91-
*
92-
* @template TReturn
93-
*
94-
* TODO: Cover with tests
95-
* @see acquireTransactionLevelLock() for preferred locking strategy.
96-
*
9794
*/
9895
public function withinSessionLevelLock(
9996
PDO $dbConnection,

test/Integration/Postgres/PostgresAdvisoryLockerTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,30 @@ public function testItCannotReleaseAllLocksAcquiredWithinTransaction(): void
566566
$this->assertPgAdvisoryLockExistsInConnection($dbConnection, $lockKey2);
567567
}
568568

569+
public function testItCanExecuteCodeWithinSessionLock(): void
570+
{
571+
$locker = $this->initLocker();
572+
$dbConnection = $this->initPostgresPdoConnection();
573+
$lockKey = PostgresLockKey::create('test');
574+
$x = 2;
575+
$y = 3;
576+
577+
$result = $locker->withinSessionLevelLock(
578+
$dbConnection,
579+
$lockKey,
580+
function () use ($dbConnection, $lockKey, $x, $y): int {
581+
$this->assertPgAdvisoryLocksCount(1);
582+
$this->assertPgAdvisoryLockExistsInConnection($dbConnection, $lockKey);
583+
584+
return $x + $y;
585+
},
586+
);
587+
588+
$this->assertSame(5, $result);
589+
$this->assertPgAdvisoryLocksCount(0);
590+
$this->assertPgAdvisoryLockMissingInConnection($dbConnection, $lockKey);
591+
}
592+
569593
private function initLocker(): PostgresAdvisoryLocker
570594
{
571595
return new PostgresAdvisoryLocker();

0 commit comments

Comments
 (0)