Skip to content

Commit e414845

Browse files
committed
Rewrite logic to two keyed lock
1 parent 77e1515 commit e414845

File tree

3 files changed

+108
-57
lines changed

3 files changed

+108
-57
lines changed

src/LockId/PostgresLockId.php

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,37 @@ final class PostgresLockId
2020
private const DB_INT32_VALUE_MIN = -2_147_483_648;
2121
private const DB_INT32_VALUE_MAX = 2_147_483_647;
2222

23-
public function __construct(
23+
private function __construct(
2424
public readonly int $classId,
2525
public readonly int $objectId,
2626
public readonly string $humanReadableValue = '',
2727
) {
2828
if ($classId < self::DB_INT32_VALUE_MIN) {
29-
throw new InvalidArgumentException('Out of bound exception (classId is too small)');
29+
throw new InvalidArgumentException("Out of bound exception (classId=$classId is too small)");
3030
}
3131
if ($classId > self::DB_INT32_VALUE_MAX) {
32-
throw new InvalidArgumentException('Out of bound exception (classId is too big)');
32+
throw new InvalidArgumentException("Out of bound exception (classId=$classId is too big)");
3333
}
3434
if ($objectId < self::DB_INT32_VALUE_MIN) {
35-
throw new InvalidArgumentException('Out of bound exception (objectId is too small)');
35+
throw new InvalidArgumentException("Out of bound exception (objectId=$objectId is too small)");
3636
}
3737
if ($objectId > self::DB_INT32_VALUE_MAX) {
38-
throw new InvalidArgumentException('Out of bound exception (objectId is too big)');
38+
throw new InvalidArgumentException("Out of bound exception (objectId=$objectId is too big)");
3939
}
4040
}
4141

42+
public static function fromKeyValue(
43+
string $key,
44+
string $value = '',
45+
): self {
46+
return self::fromLockId(
47+
new LockId(
48+
$key,
49+
$value,
50+
),
51+
);
52+
}
53+
4254
public static function fromLockId(
4355
LockId $lockId,
4456
): self {
@@ -49,15 +61,13 @@ classId: self::convertStringToSignedInt32($lockId->key),
4961
);
5062
}
5163

52-
public static function fromKeyValue(
53-
string $key,
54-
string $value = '',
64+
public static function fromIntKeys(
65+
int $classId,
66+
int $objectId,
5567
): self {
56-
return self::fromLockId(
57-
new LockId(
58-
$key,
59-
$value,
60-
),
68+
return new self(
69+
$classId,
70+
$objectId,
6171
);
6272
}
6373

test/Integration/Locker/PostgresAdvisoryLockerTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function test_it_can_acquire_lock_with_min_class_id(): void
4141
{
4242
$locker = $this->initLocker();
4343
$dbConnection = $this->initPostgresPdoConnection();
44-
$postgresLockId = new PostgresLockId(self::DB_INT32_VALUE_MIN, 0);
44+
$postgresLockId = PostgresLockId::fromIntKeys(self::DB_INT32_VALUE_MIN, 0);
4545

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

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

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

@@ -67,7 +67,7 @@ public function test_it_can_acquire_lock_with_min_object_id(): void
6767
{
6868
$locker = $this->initLocker();
6969
$dbConnection = $this->initPostgresPdoConnection();
70-
$postgresLockId = new PostgresLockId(0, self::DB_INT32_VALUE_MIN);
70+
$postgresLockId = PostgresLockId::fromIntKeys(0, self::DB_INT32_VALUE_MIN);
7171

7272
$isLockAcquired = $locker->tryAcquireLock($dbConnection, $postgresLockId);
7373

@@ -80,7 +80,7 @@ public function test_it_can_acquire_lock_with_max_object_id(): void
8080
{
8181
$locker = $this->initLocker();
8282
$dbConnection = $this->initPostgresPdoConnection();
83-
$postgresLockId = new PostgresLockId(0, self::DB_INT32_VALUE_MAX);
83+
$postgresLockId = PostgresLockId::fromIntKeys(0, self::DB_INT32_VALUE_MAX);
8484

8585
$isLockAcquired = $locker->tryAcquireLock($dbConnection, $postgresLockId);
8686

test/Unit/LockId/PostgresLockIdTest.php

Lines changed: 81 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,33 @@ final class PostgresLockIdTest extends AbstractUnitTestCase
2323
private const DB_INT32_VALUE_MIN = -2_147_483_648;
2424
private const DB_INT32_VALUE_MAX = 2_147_483_647;
2525

26-
#[DataProvider('provideItCanCreatePostgresLockIdData')]
27-
public function testItCanCreatePostgresLockId(
28-
int $classId,
29-
int $objectId,
26+
#[DataProvider('provideItCanCreatePostgresLockIdFromKeyValueData')]
27+
public function testItCanCreatePostgresLockIdFromKeyValue(
28+
string $key,
29+
string $value,
30+
int $expectedClassId,
31+
int $expectedObjectId,
3032
): void {
31-
$lockId = new PostgresLockId($classId, $objectId);
33+
$postgresLockId = PostgresLockId::fromKeyValue($key, $value);
3234

33-
$this->assertSame($classId, $lockId->classId);
34-
$this->assertSame($objectId, $lockId->objectId);
35+
$this->assertSame($expectedClassId, $postgresLockId->classId);
36+
$this->assertSame($expectedObjectId, $postgresLockId->objectId);
3537
}
3638

37-
public static function provideItCanCreatePostgresLockIdData(): array
39+
public static function provideItCanCreatePostgresLockIdFromKeyValueData(): array
3840
{
3941
return [
40-
'min class_id' => [
41-
self::DB_INT32_VALUE_MIN,
42-
0,
43-
],
44-
'max class_id' => [
45-
self::DB_INT32_VALUE_MAX,
46-
0,
47-
],
48-
'min object_id' => [
42+
'key + empty value' => [
43+
'test',
44+
'',
45+
-662733300,
4946
0,
50-
self::DB_INT32_VALUE_MIN,
5147
],
52-
'max object_id' => [
53-
0,
54-
self::DB_INT32_VALUE_MAX,
48+
'key + value' => [
49+
'test',
50+
'1',
51+
-662733300,
52+
-2082672713,
5553
],
5654
];
5755
}
@@ -84,33 +82,76 @@ public static function provideItCanCreatePostgresLockIdFromLockIdData(): array
8482
];
8583
}
8684

87-
#[DataProvider('provideItCanCreatePostgresLockIdFromKeyValueData')]
88-
public function testItCanCreatePostgresLockIdFromKeyValue(
89-
string $key,
90-
string $value,
91-
int $expectedClassId,
92-
int $expectedObjectId,
85+
#[DataProvider('provideItCanCreatePostgresLockIdFromIntKeysData')]
86+
public function testItCanCreatePostgresLockIdFromIntKeys(
87+
int $classId,
88+
int $objectId,
9389
): void {
94-
$postgresLockId = PostgresLockId::fromKeyValue($key, $value);
90+
$lockId = PostgresLockId::fromIntKeys($classId, $objectId);
9591

96-
$this->assertSame($expectedClassId, $postgresLockId->classId);
97-
$this->assertSame($expectedObjectId, $postgresLockId->objectId);
92+
$this->assertSame($classId, $lockId->classId);
93+
$this->assertSame($objectId, $lockId->objectId);
9894
}
9995

100-
public static function provideItCanCreatePostgresLockIdFromKeyValueData(): array
96+
public static function provideItCanCreatePostgresLockIdFromIntKeysData(): array
10197
{
10298
return [
103-
'key + empty value' => [
104-
'test',
105-
'',
106-
-662733300,
99+
'min class_id' => [
100+
self::DB_INT32_VALUE_MIN,
107101
0,
108102
],
109-
'key + value' => [
110-
'test',
111-
'1',
112-
-662733300,
113-
-2082672713,
103+
'max class_id' => [
104+
self::DB_INT32_VALUE_MAX,
105+
0,
106+
],
107+
'min object_id' => [
108+
0,
109+
self::DB_INT32_VALUE_MIN,
110+
],
111+
'max object_id' => [
112+
0,
113+
self::DB_INT32_VALUE_MAX,
114+
],
115+
];
116+
}
117+
118+
#[DataProvider('provideItCanCreatePostgresLockIdFromOutOfRangeIntKeysData')]
119+
public function testItCanNotCreatePostgresLockIdFromOutOfRangeIntKeys(
120+
int $classId,
121+
int $objectId,
122+
string $expectedExceptionMessage,
123+
): void {
124+
$this->expectException(\InvalidArgumentException::class);
125+
$this->expectExceptionMessage($expectedExceptionMessage);
126+
127+
$lockId = PostgresLockId::fromIntKeys($classId, $objectId);
128+
129+
$this->assertSame($classId, $lockId->classId);
130+
$this->assertSame($objectId, $lockId->objectId);
131+
}
132+
133+
public static function provideItCanCreatePostgresLockIdFromOutOfRangeIntKeysData(): array
134+
{
135+
return [
136+
'min class_id' => [
137+
self::DB_INT32_VALUE_MIN - 1,
138+
0,
139+
"Out of bound exception (classId=-2147483649 is too small)"
140+
],
141+
'max class_id' => [
142+
self::DB_INT32_VALUE_MAX + 1,
143+
0,
144+
"Out of bound exception (classId=2147483648 is too big)"
145+
],
146+
'min object_id' => [
147+
0,
148+
self::DB_INT32_VALUE_MIN - 1,
149+
"Out of bound exception (objectId=-2147483649 is too small)"
150+
],
151+
'max object_id' => [
152+
0,
153+
self::DB_INT32_VALUE_MAX + 1,
154+
"Out of bound exception (objectId=2147483648 is too big)"
114155
],
115156
];
116157
}

0 commit comments

Comments
 (0)