Skip to content

Commit 58e119f

Browse files
committed
Leave only *Handler API
1 parent 2fff9f9 commit 58e119f

File tree

3 files changed

+34
-74
lines changed

3 files changed

+34
-74
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ $locker = new \Cog\DbLocker\Postgres\PostgresAdvisoryLocker();
3838
$lockId = \Cog\DbLocker\Postgres\PostgresLockKey::create('user', '4');
3939

4040
$dbConnection->beginTransaction();
41-
$lock = $locker->acquireSessionLevelLockHandler(
41+
$lock = $locker->acquireSessionLevelLock(
4242
$dbConnection,
4343
$lockId,
4444
\Cog\DbLocker\Postgres\Enum\PostgresLockWaitModeEnum::NonBlocking,
@@ -61,7 +61,7 @@ $locker = new \Cog\DbLocker\Postgres\PostgresAdvisoryLocker();
6161
$lockId = \Cog\DbLocker\Postgres\PostgresLockKey::create('user', '4');
6262

6363
try {
64-
$lock = $locker->acquireSessionLevelLockHandler(
64+
$lock = $locker->acquireSessionLevelLock(
6565
$dbConnection,
6666
$lockId,
6767
\Cog\DbLocker\Postgres\Enum\PostgresLockWaitModeEnum::NonBlocking,

src/Postgres/PostgresAdvisoryLocker.php

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ final class PostgresAdvisoryLocker
2828
*
2929
* TODO: Cover with tests
3030
*/
31-
public function acquireTransactionLevelLockHandler(
31+
public function acquireTransactionLevelLock(
3232
PDO $dbConnection,
3333
PostgresLockKey $postgresLockId,
3434
PostgresLockWaitModeEnum $waitMode = PostgresLockWaitModeEnum::NonBlocking,
@@ -45,33 +45,13 @@ public function acquireTransactionLevelLockHandler(
4545
);
4646
}
4747

48-
/**
49-
* Acquire a transaction-level advisory lock with configurable wait and access modes.
50-
*
51-
* TODO: Do we need low-level API?
52-
*/
53-
public function acquireTransactionLevelLock(
54-
PDO $dbConnection,
55-
PostgresLockKey $postgresLockId,
56-
PostgresLockWaitModeEnum $waitMode = PostgresLockWaitModeEnum::NonBlocking,
57-
PostgresLockAccessModeEnum $accessMode = PostgresLockAccessModeEnum::Exclusive,
58-
): bool {
59-
return $this->acquireLock(
60-
$dbConnection,
61-
$postgresLockId,
62-
PostgresLockLevelEnum::Transaction,
63-
$waitMode,
64-
$accessMode,
65-
);
66-
}
67-
6848
/**
6949
* Acquire a session-level advisory lock with configurable wait and access modes.
7050
*
7151
* TODO: Write that transaction-level is recommended.
7252
* TODO: Cover with tests
7353
*/
74-
public function acquireSessionLevelLockHandler(
54+
public function acquireSessionLevelLock(
7555
PDO $dbConnection,
7656
PostgresLockKey $postgresLockId,
7757
PostgresLockWaitModeEnum $waitMode = PostgresLockWaitModeEnum::NonBlocking,
@@ -92,27 +72,6 @@ public function acquireSessionLevelLockHandler(
9272
);
9373
}
9474

95-
/**
96-
* Acquire a session-level advisory lock with configurable wait and access modes.
97-
*
98-
* TODO: Write that transaction-level is recommended.
99-
* TODO: Do we need low-level API?
100-
*/
101-
public function acquireSessionLevelLock(
102-
PDO $dbConnection,
103-
PostgresLockKey $postgresLockId,
104-
PostgresLockWaitModeEnum $waitMode = PostgresLockWaitModeEnum::NonBlocking,
105-
PostgresLockAccessModeEnum $accessMode = PostgresLockAccessModeEnum::Exclusive,
106-
): bool {
107-
return $this->acquireLock(
108-
$dbConnection,
109-
$postgresLockId,
110-
PostgresLockLevelEnum::Session,
111-
$waitMode,
112-
$accessMode,
113-
);
114-
}
115-
11675
/**
11776
* Release session level advisory lock.
11877
*/

test/Integration/Postgres/PostgresAdvisoryLockerTest.php

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function testItCanTryAcquireLockWithinSession(
3939
accessMode: $accessMode,
4040
);
4141

42-
$this->assertTrue($isLockAcquired);
42+
$this->assertTrue($isLockAcquired->wasAcquired);
4343
$this->assertPgAdvisoryLocksCount(1);
4444
$this->assertPgAdvisoryLockExistsInConnection($dbConnection, $postgresLockId, $accessMode);
4545
}
@@ -71,7 +71,7 @@ public function testItCanTryAcquireLockWithinTransaction(
7171
accessMode: $accessMode,
7272
);
7373

74-
$this->assertTrue($isLockAcquired);
74+
$this->assertTrue($isLockAcquired->wasAcquired);
7575
$this->assertPgAdvisoryLocksCount(1);
7676
$this->assertPgAdvisoryLockExistsInConnection($dbConnection, $postgresLockId, $accessMode);
7777
}
@@ -100,7 +100,7 @@ public function testItCanTryAcquireLockFromIntKeysCornerCases(): void
100100
$postgresLockId,
101101
);
102102

103-
$this->assertTrue($isLockAcquired);
103+
$this->assertTrue($isLockAcquired->wasAcquired);
104104
$this->assertPgAdvisoryLocksCount(1);
105105
$this->assertPgAdvisoryLockExistsInConnection($dbConnection, $postgresLockId);
106106
}
@@ -133,17 +133,17 @@ public function testItCanTryAcquireLockInSameConnectionOnlyOnce(): void
133133
$dbConnection = $this->initPostgresPdoConnection();
134134
$postgresLockId = PostgresLockKey::create('test');
135135

136-
$isLockAcquired1 = $locker->acquireSessionLevelLock(
136+
$isLock1Acquired = $locker->acquireSessionLevelLock(
137137
$dbConnection,
138138
$postgresLockId,
139139
);
140-
$isLockAcquired2 = $locker->acquireSessionLevelLock(
140+
$isLock2Acquired = $locker->acquireSessionLevelLock(
141141
$dbConnection,
142142
$postgresLockId,
143143
);
144144

145-
$this->assertTrue($isLockAcquired1);
146-
$this->assertTrue($isLockAcquired2);
145+
$this->assertTrue($isLock1Acquired->wasAcquired);
146+
$this->assertTrue($isLock2Acquired->wasAcquired);
147147
$this->assertPgAdvisoryLocksCount(1);
148148
$this->assertPgAdvisoryLockExistsInConnection($dbConnection, $postgresLockId);
149149
}
@@ -164,8 +164,8 @@ public function testItCanTryAcquireMultipleLocksInOneConnection(): void
164164
$postgresLockId2,
165165
);
166166

167-
$this->assertTrue($isLock1Acquired);
168-
$this->assertTrue($isLock2Acquired);
167+
$this->assertTrue($isLock1Acquired->wasAcquired);
168+
$this->assertTrue($isLock2Acquired->wasAcquired);
169169
$this->assertPgAdvisoryLocksCount(2);
170170
$this->assertPgAdvisoryLockExistsInConnection($dbConnection, $postgresLockId1);
171171
$this->assertPgAdvisoryLockExistsInConnection($dbConnection, $postgresLockId2);
@@ -177,17 +177,18 @@ public function testItCannotAcquireSameLockInTwoConnections(): void
177177
$dbConnection1 = $this->initPostgresPdoConnection();
178178
$dbConnection2 = $this->initPostgresPdoConnection();
179179
$postgresLockId = PostgresLockKey::create('test');
180-
$locker->acquireSessionLevelLock(
180+
// TODO: Fix corner-case: without variable its self-destructing instantly
181+
$connection1Lock = $locker->acquireSessionLevelLock(
181182
$dbConnection1,
182183
$postgresLockId,
183184
);
184185

185-
$isLockAcquired = $locker->acquireSessionLevelLock(
186+
$connection2Lock = $locker->acquireSessionLevelLock(
186187
$dbConnection2,
187188
$postgresLockId,
188189
);
189190

190-
$this->assertFalse($isLockAcquired);
191+
$this->assertFalse($connection2Lock->wasAcquired);
191192
$this->assertPgAdvisoryLocksCount(1);
192193
$this->assertPgAdvisoryLockMissingInConnection($dbConnection2, $postgresLockId);
193194
}
@@ -199,7 +200,7 @@ public function testItCanReleaseLock(
199200
$locker = $this->initLocker();
200201
$dbConnection = $this->initPostgresPdoConnection();
201202
$postgresLockId = PostgresLockKey::create('test');
202-
$locker->acquireSessionLevelLock(
203+
$connectionLock = $locker->acquireSessionLevelLock(
203204
$dbConnection,
204205
$postgresLockId,
205206
accessMode: $accessMode,
@@ -235,7 +236,7 @@ public function testItCanNotReleaseLockOfDifferentModes(
235236
$locker = $this->initLocker();
236237
$dbConnection = $this->initPostgresPdoConnection();
237238
$postgresLockId = PostgresLockKey::create('test');
238-
$locker->acquireSessionLevelLock(
239+
$connectionLock = $locker->acquireSessionLevelLock(
239240
$dbConnection,
240241
$postgresLockId,
241242
accessMode: $acquireMode,
@@ -271,11 +272,11 @@ public function testItCanReleaseLockTwiceIfAcquiredTwice(): void
271272
$locker = $this->initLocker();
272273
$dbConnection = $this->initPostgresPdoConnection();
273274
$postgresLockId = PostgresLockKey::create('test');
274-
$locker->acquireSessionLevelLock(
275+
$connectionLock1 = $locker->acquireSessionLevelLock(
275276
$dbConnection,
276277
$postgresLockId,
277278
);
278-
$locker->acquireSessionLevelLock(
279+
$connectionLock2 = $locker->acquireSessionLevelLock(
279280
$dbConnection,
280281
$postgresLockId,
281282
);
@@ -308,7 +309,7 @@ public function testItCanTryAcquireLockInSecondConnectionAfterRelease(): void
308309
$postgresLockId,
309310
);
310311

311-
$this->assertTrue($isLockAcquired);
312+
$this->assertTrue($isLockAcquired->wasAcquired);
312313
$this->assertPgAdvisoryLocksCount(1);
313314
$this->assertPgAdvisoryLockExistsInConnection($dbConnection2, $postgresLockId);
314315
}
@@ -319,23 +320,23 @@ public function testItCannotAcquireLockInSecondConnectionAfterOneReleaseTwiceLoc
319320
$dbConnection1 = $this->initPostgresPdoConnection();
320321
$dbConnection2 = $this->initPostgresPdoConnection();
321322
$postgresLockId = PostgresLockKey::create('test');
322-
$locker->acquireSessionLevelLock(
323+
$connection1Lock1 = $locker->acquireSessionLevelLock(
323324
$dbConnection1,
324325
$postgresLockId,
325326
);
326-
$locker->acquireSessionLevelLock(
327+
$connection1Lock2 = $locker->acquireSessionLevelLock(
327328
$dbConnection1,
328329
$postgresLockId,
329330
);
330331

331332
$isLockReleased = $locker->releaseSessionLevelLock($dbConnection1, $postgresLockId);
332-
$isLockAcquired = $locker->acquireSessionLevelLock(
333+
$connection2Lock = $locker->acquireSessionLevelLock(
333334
$dbConnection2,
334335
$postgresLockId,
335336
);
336337

337338
$this->assertTrue($isLockReleased);
338-
$this->assertFalse($isLockAcquired);
339+
$this->assertFalse($connection2Lock->wasAcquired);
339340
$this->assertPgAdvisoryLocksCount(1);
340341
$this->assertPgAdvisoryLockExistsInConnection($dbConnection1, $postgresLockId);
341342
$this->assertPgAdvisoryLockMissingInConnection($dbConnection2, $postgresLockId);
@@ -359,7 +360,7 @@ public function testItCannotReleaseLockIfAcquiredInOtherConnection(): void
359360
$dbConnection1 = $this->initPostgresPdoConnection();
360361
$dbConnection2 = $this->initPostgresPdoConnection();
361362
$postgresLockId = PostgresLockKey::create('test');
362-
$locker->acquireSessionLevelLock(
363+
$connection1Lock = $locker->acquireSessionLevelLock(
363364
$dbConnection1,
364365
$postgresLockId,
365366
);
@@ -412,19 +413,19 @@ public function testItCanReleaseAllLocksInConnectionButKeepsOtherConnectionLocks
412413
$postgresLockId2 = PostgresLockKey::create('test2');
413414
$postgresLockId3 = PostgresLockKey::create('test3');
414415
$postgresLockId4 = PostgresLockKey::create('test4');
415-
$locker->acquireSessionLevelLock(
416+
$connect1Lock1 = $locker->acquireSessionLevelLock(
416417
$dbConnection1,
417418
$postgresLockId1,
418419
);
419-
$locker->acquireSessionLevelLock(
420+
$connect1Lock2 = $locker->acquireSessionLevelLock(
420421
$dbConnection1,
421422
$postgresLockId2,
422423
);
423-
$locker->acquireSessionLevelLock(
424+
$connect2Lock3 = $locker->acquireSessionLevelLock(
424425
$dbConnection2,
425426
$postgresLockId3,
426427
);
427-
$locker->acquireSessionLevelLock(
428+
$connect2Lock4 = $locker->acquireSessionLevelLock(
428429
$dbConnection2,
429430
$postgresLockId4,
430431
);
@@ -460,17 +461,17 @@ public function testItCannotAcquireLockInSecondConnectionIfTakenWithinTransactio
460461
$dbConnection2 = $this->initPostgresPdoConnection();
461462
$postgresLockId = PostgresLockKey::create('test');
462463
$dbConnection1->beginTransaction();
463-
$locker->acquireSessionLevelLock(
464+
$connection1Lock = $locker->acquireSessionLevelLock(
464465
$dbConnection1,
465466
$postgresLockId,
466467
);
467468

468-
$isLockAcquired = $locker->acquireSessionLevelLock(
469+
$connection2Lock = $locker->acquireSessionLevelLock(
469470
$dbConnection2,
470471
$postgresLockId,
471472
);
472473

473-
$this->assertFalse($isLockAcquired);
474+
$this->assertFalse($connection2Lock->wasAcquired);
474475
$this->assertPgAdvisoryLocksCount(1);
475476
$this->assertPgAdvisoryLockExistsInConnection($dbConnection1, $postgresLockId);
476477
}

0 commit comments

Comments
 (0)