@@ -164,7 +164,7 @@ public function testItCanReleaseLock(): void
164
164
PostgresAdvisoryLockScopeEnum::Session,
165
165
);
166
166
167
- $ isLockReleased = $ locker ->releaseLockWithinSession ($ dbConnection , $ postgresLockId );
167
+ $ isLockReleased = $ locker ->releaseLock ($ dbConnection , $ postgresLockId );
168
168
169
169
$ this ->assertTrue ($ isLockReleased );
170
170
$ this ->assertPgAdvisoryLocksCount (0 );
@@ -186,8 +186,8 @@ public function testItCanReleaseLockTwiceIfAcquiredTwice(): void
186
186
PostgresAdvisoryLockScopeEnum::Session,
187
187
);
188
188
189
- $ isLockReleased1 = $ locker ->releaseLockWithinSession ($ dbConnection , $ postgresLockId );
190
- $ isLockReleased2 = $ locker ->releaseLockWithinSession ($ dbConnection , $ postgresLockId );
189
+ $ isLockReleased1 = $ locker ->releaseLock ($ dbConnection , $ postgresLockId );
190
+ $ isLockReleased2 = $ locker ->releaseLock ($ dbConnection , $ postgresLockId );
191
191
192
192
$ this ->assertTrue ($ isLockReleased1 );
193
193
$ this ->assertTrue ($ isLockReleased2 );
@@ -205,7 +205,7 @@ public function testItCanTryAcquireLockInSecondConnectionAfterRelease(): void
205
205
$ postgresLockId ,
206
206
PostgresAdvisoryLockScopeEnum::Session,
207
207
);
208
- $ locker ->releaseLockWithinSession (
208
+ $ locker ->releaseLock (
209
209
$ dbConnection1 ,
210
210
$ postgresLockId ,
211
211
);
@@ -238,7 +238,7 @@ public function testItCannotAcquireLockInSecondConnectionAfterOneReleaseTwiceLoc
238
238
PostgresAdvisoryLockScopeEnum::Session,
239
239
);
240
240
241
- $ isLockReleased = $ locker ->releaseLockWithinSession ($ dbConnection1 , $ postgresLockId );
241
+ $ isLockReleased = $ locker ->releaseLock ($ dbConnection1 , $ postgresLockId );
242
242
$ isLockAcquired = $ locker ->acquireLock (
243
243
$ dbConnection2 ,
244
244
$ postgresLockId ,
@@ -258,7 +258,7 @@ public function testItCannotReleaseLockIfNotAcquired(): void
258
258
$ dbConnection = $ this ->initPostgresPdoConnection ();
259
259
$ postgresLockId = PostgresLockId::fromKeyValue ('test ' );
260
260
261
- $ isLockReleased = $ locker ->releaseLockWithinSession ($ dbConnection , $ postgresLockId );
261
+ $ isLockReleased = $ locker ->releaseLock ($ dbConnection , $ postgresLockId );
262
262
263
263
$ this ->assertFalse ($ isLockReleased );
264
264
$ this ->assertPgAdvisoryLocksCount (0 );
@@ -276,7 +276,7 @@ public function testItCannotReleaseLockIfAcquiredInOtherConnection(): void
276
276
PostgresAdvisoryLockScopeEnum::Session,
277
277
);
278
278
279
- $ isLockReleased = $ locker ->releaseLockWithinSession ($ dbConnection2 , $ postgresLockId );
279
+ $ isLockReleased = $ locker ->releaseLock ($ dbConnection2 , $ postgresLockId );
280
280
281
281
$ this ->assertFalse ($ isLockReleased );
282
282
$ this ->assertPgAdvisoryLocksCount (1 );
@@ -298,7 +298,7 @@ public function testItCanReleaseAllLocksInConnection(): void
298
298
PostgresAdvisoryLockScopeEnum::Session,
299
299
);
300
300
301
- $ locker ->releaseAllLocksWithinSession ($ dbConnection );
301
+ $ locker ->releaseAllLocks ($ dbConnection );
302
302
303
303
$ this ->assertPgAdvisoryLocksCount (0 );
304
304
}
@@ -308,7 +308,7 @@ public function testItCanReleaseAllLocksInConnectionIfNoLocksWereAcquired(): voi
308
308
$ locker = $ this ->initLocker ();
309
309
$ dbConnection = $ this ->initPostgresPdoConnection ();
310
310
311
- $ locker ->releaseAllLocksWithinSession ($ dbConnection );
311
+ $ locker ->releaseAllLocks ($ dbConnection );
312
312
313
313
$ this ->assertPgAdvisoryLocksCount (0 );
314
314
}
@@ -343,7 +343,7 @@ public function testItCanReleaseAllLocksInConnectionButKeepsOtherLocks(): void
343
343
PostgresAdvisoryLockScopeEnum::Session,
344
344
);
345
345
346
- $ locker ->releaseAllLocksWithinSession ($ dbConnection1 );
346
+ $ locker ->releaseAllLocks ($ dbConnection1 );
347
347
348
348
$ this ->assertPgAdvisoryLocksCount (2 );
349
349
$ this ->assertPgAdvisoryLockExistsInConnection ($ dbConnection2 , $ postgresLockId3 );
@@ -475,7 +475,7 @@ public function testItCannotReleaseLockAcquiredWithinTransaction(): void
475
475
PostgresAdvisoryLockScopeEnum::Transaction,
476
476
);
477
477
478
- $ isLockReleased = $ locker ->releaseLockWithinSession ($ dbConnection , $ postgresLockId );
478
+ $ isLockReleased = $ locker ->releaseLock ($ dbConnection , $ postgresLockId );
479
479
480
480
$ this ->assertFalse ($ isLockReleased );
481
481
$ this ->assertPgAdvisoryLocksCount (1 );
@@ -500,13 +500,76 @@ public function testItCannotReleaseAllLocksAcquiredWithinTransaction(): void
500
500
PostgresAdvisoryLockScopeEnum::Transaction,
501
501
);
502
502
503
- $ locker ->releaseAllLocksWithinSession ($ dbConnection );
503
+ $ locker ->releaseAllLocks ($ dbConnection );
504
504
505
505
$ this ->assertPgAdvisoryLocksCount (1 );
506
506
$ this ->assertPgAdvisoryLockMissingInConnection ($ dbConnection , $ postgresLockId1 );
507
507
$ this ->assertPgAdvisoryLockExistsInConnection ($ dbConnection , $ postgresLockId2 );
508
508
}
509
509
510
+ public function testItCannotReleaseAllLocksWithTransactionScope (): void
511
+ {
512
+ $ locker = $ this ->initLocker ();
513
+ $ dbConnection = $ this ->initPostgresPdoConnection ();
514
+ $ postgresLockId1 = PostgresLockId::fromKeyValue ('test ' );
515
+ $ postgresLockId2 = PostgresLockId::fromKeyValue ('test2 ' );
516
+ $ locker ->acquireLock (
517
+ $ dbConnection ,
518
+ $ postgresLockId1 ,
519
+ PostgresAdvisoryLockScopeEnum::Session,
520
+ );
521
+ $ dbConnection ->beginTransaction ();
522
+ $ locker ->acquireLock (
523
+ $ dbConnection ,
524
+ $ postgresLockId2 ,
525
+ PostgresAdvisoryLockScopeEnum::Transaction,
526
+ );
527
+
528
+ try {
529
+ $ locker ->releaseAllLocks (
530
+ $ dbConnection ,
531
+ PostgresAdvisoryLockScopeEnum::Transaction,
532
+ );
533
+ } catch (\InvalidArgumentException $ exception ) {
534
+ $ this ->assertSame (
535
+ 'Transaction-level advisory lock cannot be released ' ,
536
+ $ exception ->getMessage (),
537
+ );
538
+ }
539
+ }
540
+
541
+ public function testItCannotReleaseLocksWithTransactionScope (): void
542
+ {
543
+ $ locker = $ this ->initLocker ();
544
+ $ dbConnection = $ this ->initPostgresPdoConnection ();
545
+ $ postgresLockId1 = PostgresLockId::fromKeyValue ('test ' );
546
+ $ postgresLockId2 = PostgresLockId::fromKeyValue ('test2 ' );
547
+ $ locker ->acquireLock (
548
+ $ dbConnection ,
549
+ $ postgresLockId1 ,
550
+ PostgresAdvisoryLockScopeEnum::Session,
551
+ );
552
+ $ dbConnection ->beginTransaction ();
553
+ $ locker ->acquireLock (
554
+ $ dbConnection ,
555
+ $ postgresLockId2 ,
556
+ PostgresAdvisoryLockScopeEnum::Transaction,
557
+ );
558
+
559
+ try {
560
+ $ locker ->releaseLock (
561
+ $ dbConnection ,
562
+ $ postgresLockId2 ,
563
+ PostgresAdvisoryLockScopeEnum::Transaction,
564
+ );
565
+ } catch (\InvalidArgumentException $ exception ) {
566
+ $ this ->assertSame (
567
+ 'Transaction-level advisory lock cannot be released ' ,
568
+ $ exception ->getMessage (),
569
+ );
570
+ }
571
+ }
572
+
510
573
private function initLocker (): PostgresAdvisoryLocker
511
574
{
512
575
return new PostgresAdvisoryLocker ();
0 commit comments