Skip to content

Commit 8798e5e

Browse files
committed
Rewrite logic to two keyed lock
1 parent 313e83e commit 8798e5e

File tree

3 files changed

+13
-14
lines changed

3 files changed

+13
-14
lines changed

src/LockId/PostgresLockId.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,17 @@
1717

1818
final class PostgresLockId
1919
{
20-
private const DB_INT64_VALUE_MIN = -9_223_372_036_854_775_808;
21-
private const DB_INT64_VALUE_MAX = 9_223_372_036_854_775_807;
20+
private const DB_INT32_VALUE_MIN = -2_147_483_648;
2221
private const DB_INT32_VALUE_MAX = 2_147_483_647;
2322

2423
public function __construct(
2524
public readonly int $id,
2625
public readonly string $humanReadableValue = '',
2726
) {
28-
if ($id < self::DB_INT64_VALUE_MIN) {
27+
if ($id < self::DB_INT32_VALUE_MIN) {
2928
throw new InvalidArgumentException('Out of bound exception (id is too small)');
3029
}
31-
if ($id > self::DB_INT64_VALUE_MAX) {
30+
if ($id > self::DB_INT32_VALUE_MAX) {
3231
throw new InvalidArgumentException('Out of bound exception (id is too big)');
3332
}
3433
}

test/Integration/Locker/PostgresAdvisoryLockerTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121

2222
final class PostgresAdvisoryLockerTest extends AbstractIntegrationTestCase
2323
{
24-
private const DB_INT64_VALUE_MIN = 0;
25-
private const DB_INT64_VALUE_MAX = 9223372036854775807;
24+
private const DB_INT32_VALUE_MIN = -2_147_483_648;
25+
private const DB_INT32_VALUE_MAX = 2_147_483_647;
2626

2727
public function test_it_can_acquire_lock(): void
2828
{
@@ -41,7 +41,7 @@ public function test_it_can_acquire_lock_with_smallest_lock_id(): void
4141
{
4242
$locker = $this->initLocker();
4343
$dbConnection = $this->initPostgresPdoConnection();
44-
$postgresLockId = new PostgresLockId(self::DB_INT64_VALUE_MIN);
44+
$postgresLockId = new PostgresLockId(self::DB_INT32_VALUE_MIN);
4545

4646
$isLockAcquired = $locker->tryAcquireLock($dbConnection, $postgresLockId);
4747

@@ -54,7 +54,7 @@ public function test_it_can_acquire_lock_with_biggest_lock_id(): void
5454
{
5555
$locker = $this->initLocker();
5656
$dbConnection = $this->initPostgresPdoConnection();
57-
$postgresLockId = new PostgresLockId(self::DB_INT64_VALUE_MAX);
57+
$postgresLockId = new PostgresLockId(self::DB_INT32_VALUE_MAX);
5858

5959
$isLockAcquired = $locker->tryAcquireLock($dbConnection, $postgresLockId);
6060

test/Unit/LockId/PostgresLockIdTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@
1919

2020
final class PostgresLockIdTest extends AbstractUnitTestCase
2121
{
22-
private const DB_INT64_VALUE_MIN = 0;
23-
private const DB_INT64_VALUE_MAX = 9223372036854775807;
22+
private const DB_INT32_VALUE_MIN = -2_147_483_648;
23+
private const DB_INT32_VALUE_MAX = 2_147_483_647;
2424

2525
public function test_it_can_create_postgres_lock_id_with_min_id(): void
2626
{
27-
$lockId = new PostgresLockId(self::DB_INT64_VALUE_MIN);
27+
$lockId = new PostgresLockId(self::DB_INT32_VALUE_MIN);
2828

29-
$this->assertSame(self::DB_INT64_VALUE_MIN, $lockId->id);
29+
$this->assertSame(self::DB_INT32_VALUE_MIN, $lockId->id);
3030
}
3131

3232
public function test_it_can_create_postgres_lock_id_with_max_id(): void
3333
{
34-
$lockId = new PostgresLockId(self::DB_INT64_VALUE_MAX);
34+
$lockId = new PostgresLockId(self::DB_INT32_VALUE_MAX);
3535

36-
$this->assertSame(self::DB_INT64_VALUE_MAX, $lockId->id);
36+
$this->assertSame(self::DB_INT32_VALUE_MAX, $lockId->id);
3737
}
3838

3939
public function test_it_can_create_postgres_lock_id_from_lock_id(): void

0 commit comments

Comments
 (0)