Skip to content

Commit 565f63e

Browse files
committed
MC-35903: Refactor place where lock mechanism is used regarding proper lock description
1 parent 63448b8 commit 565f63e

File tree

5 files changed

+48
-23
lines changed

5 files changed

+48
-23
lines changed

app/code/Magento/MessageQueue/Test/Unit/Console/StartConsumerCommandTest.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ public function testExecute(
103103
$pidFilePath,
104104
$singleThread,
105105
$lockExpects,
106-
$isLockedExpects,
107106
$isLocked,
108107
$unlockExpects,
109108
$runProcessExpects,
@@ -144,14 +143,11 @@ public function testExecute(
144143
->method('get')->with($consumerName, $batchSize)->willReturn($consumer);
145144
$consumer->expects($this->exactly($runProcessExpects))->method('process')->with($numberOfMessages);
146145

147-
$this->lockManagerMock->expects($this->exactly($isLockedExpects))
148-
->method('isLocked')
149-
->with(md5($consumerName)) //phpcs:ignore
150-
->willReturn($isLocked);
151-
152146
$this->lockManagerMock->expects($this->exactly($lockExpects))
153147
->method('lock')
154-
->with(md5($consumerName)); //phpcs:ignore
148+
->with(md5($consumerName))//phpcs:ignore
149+
->willReturn($isLocked);
150+
155151
$this->lockManagerMock->expects($this->exactly($unlockExpects))
156152
->method('unlock')
157153
->with(md5($consumerName)); //phpcs:ignore
@@ -172,8 +168,7 @@ public function executeDataProvider()
172168
'pidFilePath' => null,
173169
'singleThread' => false,
174170
'lockExpects' => 0,
175-
'isLockedExpects' => 0,
176-
'isLocked' => false,
171+
'isLocked' => true,
177172
'unlockExpects' => 0,
178173
'runProcessExpects' => 1,
179174
'expectedReturn' => Cli::RETURN_SUCCESS,
@@ -182,19 +177,17 @@ public function executeDataProvider()
182177
'pidFilePath' => '/var/consumer.pid',
183178
'singleThread' => true,
184179
'lockExpects' => 1,
185-
'isLockedExpects' => 1,
186-
'isLocked' => false,
180+
'isLocked' => true,
187181
'unlockExpects' => 1,
188182
'runProcessExpects' => 1,
189183
'expectedReturn' => Cli::RETURN_SUCCESS,
190184
],
191185
[
192186
'pidFilePath' => '/var/consumer.pid',
193187
'singleThread' => true,
194-
'lockExpects' => 0,
195-
'isLockedExpects' => 1,
196-
'isLocked' => true,
197-
'unlockExpects' => 0,
188+
'lockExpects' => 1,
189+
'isLocked' => false,
190+
'unlockExpects' => 1,
198191
'runProcessExpects' => 0,
199192
'expectedReturn' => Cli::RETURN_FAILURE,
200193
],

dev/tests/integration/testsuite/Magento/Framework/Lock/Backend/CacheTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,15 @@ public function testParallelLock(): void
6060
*/
6161
public function testParallelLockExpired(): void
6262
{
63-
$lifeTime = \Closure::bind(function (Cache $class) {
64-
return $class->defaultLifetime;
63+
$testLifeTime = 2;
64+
\Closure::bind(function (Cache $class) use ($testLifeTime) {
65+
$class->defaultLifetime = $testLifeTime;
6566
}, null, $this->cacheInstance1)($this->cacheInstance1);
6667

6768
$identifier1 = \uniqid('lock_name_1_', true);
6869

6970
$this->assertTrue($this->cacheInstance1->lock($identifier1, 0));
70-
$this->assertTrue($this->cacheInstance2->lock($identifier1, $lifeTime + 1));
71+
$this->assertTrue($this->cacheInstance2->lock($identifier1, $testLifeTime + 1));
7172

7273
$this->cacheInstance2->unlock($identifier1);
7374
}

lib/internal/Magento/Framework/Cache/Test/Unit/LockGuardedCacheLoaderTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function testDataCollectedAfterDeadlineReached(): void
9595

9696
$this->lockManagerInterfaceMock
9797
->expects($this->atLeastOnce())->method('lock')
98-
->with($lockName, 10)
98+
->with($lockName, 0)
9999
->willReturn(false);
100100

101101
$this->lockManagerInterfaceMock->expects($this->never())->method('unlock');
@@ -129,7 +129,7 @@ public function testDataWrite(): void
129129

130130
$this->lockManagerInterfaceMock
131131
->expects($this->once())->method('lock')
132-
->with($lockName, 10)
132+
->with($lockName, 0)
133133
->willReturn(true);
134134

135135
$this->lockManagerInterfaceMock->expects($this->once())->method('unlock');
@@ -168,7 +168,7 @@ public function testDataCollectedWithParallelGeneration(): void
168168

169169
$this->lockManagerInterfaceMock
170170
->expects($this->once())->method('lock')
171-
->with($lockName, 10)
171+
->with($lockName, 0)
172172
->willReturn(false);
173173

174174
$this->lockManagerInterfaceMock->expects($this->never())->method('unlock');

lib/internal/Magento/Framework/Lock/Backend/Cache.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,14 @@ class Cache implements \Magento\Framework\Lock\LockManagerInterface
4343
*
4444
* @var int
4545
*/
46-
private $defaultLifetime = 10;
46+
private $defaultLifetime = 7200;
47+
48+
/**
49+
* Array for keeping all lock attempt to release them on destruct.
50+
*
51+
* @var string[]
52+
*/
53+
private $lockArrayState = [];
4754

4855
/**
4956
* @param FrontendInterface $cache
@@ -77,6 +84,7 @@ public function lock(string $name, int $timeout = -1): bool
7784
$data = $this->cache->load($this->getIdentifier($name));
7885

7986
if ($data === $this->lockSign) {
87+
$this->lockArrayState[$name] = 1;
8088
return true;
8189
}
8290

@@ -101,6 +109,7 @@ public function unlock(string $name): bool
101109
$removeResult = false;
102110
if ($data === $this->lockSign) {
103111
$removeResult = (bool)$this->cache->remove($this->getIdentifier($name));
112+
unset($this->lockArrayState[$name]);
104113
}
105114

106115
return $removeResult;
@@ -147,4 +156,26 @@ private function generateLockSign()
147156

148157
return $sign;
149158
}
159+
160+
/**
161+
* Destruct method should release all locks that left.
162+
*
163+
* @return void
164+
*/
165+
public function __destruct()
166+
{
167+
$this->releaseLocks();
168+
}
169+
170+
/**
171+
* Release all locks that were not removed with unlock method.
172+
*
173+
* @return void
174+
*/
175+
private function releaseLocks()
176+
{
177+
foreach ($this->lockArrayState as $name => $value) {
178+
$this->unlock($name);
179+
}
180+
}
150181
}

lib/internal/Magento/Framework/Lock/Test/Unit/Backend/CacheTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,6 @@ public function testLockWithNotEmptyData(): void
162162
->with(self::LOCK_PREFIX . $identifier)
163163
->willReturn(\uniqid('some_rand-', true));
164164

165-
$this->assertEquals(false, $this->cache->lock($identifier));
165+
$this->assertEquals(false, $this->cache->lock($identifier, 0));
166166
}
167167
}

0 commit comments

Comments
 (0)