Skip to content

Commit 8d50cf4

Browse files
committed
Merge remote-tracking branch 'performance/MC-33797-3' into MC-33275-2
2 parents 7febc1d + 4e9b841 commit 8d50cf4

File tree

2 files changed

+194
-1
lines changed
  • dev/tests/integration/testsuite/Magento/Framework/Lock/Backend
  • lib/internal/Magento/Framework/Lock/Test/Unit/Backend

2 files changed

+194
-1
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Lock\Backend;
7+
8+
use Magento\Framework\Lock\Backend\Cache;
9+
10+
class CacheTest extends \PHPUnit\Framework\TestCase
11+
{
12+
/**
13+
* @var Cache
14+
*/
15+
private $cacheInstance1;
16+
17+
/**
18+
* @var Cache
19+
*/
20+
private $cacheInstance2;
21+
22+
/**
23+
* @inheritDoc
24+
*/
25+
protected function setUp(): void
26+
{
27+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
28+
29+
$frontendInterface1 = $objectManager->create(\Magento\Framework\App\Cache\Type\Config::class);
30+
$this->cacheInstance1 = new Cache($frontendInterface1);
31+
32+
$frontendInterface2 = $objectManager->create(\Magento\Framework\App\Cache\Type\Config::class);
33+
$this->cacheInstance2 = new Cache($frontendInterface2);
34+
}
35+
36+
/**
37+
* Verify lock mechanism in general.
38+
*
39+
* @return void
40+
*/
41+
public function testParallelLock(): void
42+
{
43+
$identifier1 = \uniqid('lock_name_1_', true);
44+
45+
$this->assertTrue($this->cacheInstance1->lock($identifier1, 2));
46+
47+
$this->assertFalse($this->cacheInstance1->lock($identifier1, 2));
48+
$this->assertFalse($this->cacheInstance2->lock($identifier1, 2));
49+
sleep(4);
50+
$this->assertFalse($this->cacheInstance1->isLocked($identifier1));
51+
52+
$this->assertTrue($this->cacheInstance2->lock($identifier1, -1));
53+
sleep(4);
54+
$this->assertTrue($this->cacheInstance1->isLocked($identifier1));
55+
}
56+
57+
/**
58+
* Verify that lock will be released after timeout expiration.
59+
*
60+
* @return void
61+
*/
62+
public function testParallelLockExpired(): void
63+
{
64+
$identifier1 = \uniqid('lock_name_1_', true);
65+
66+
$this->assertTrue($this->cacheInstance1->lock($identifier1, 1));
67+
sleep(2);
68+
$this->assertFalse($this->cacheInstance1->isLocked($identifier1));
69+
70+
$this->assertTrue($this->cacheInstance1->lock($identifier1, 1));
71+
sleep(2);
72+
$this->assertFalse($this->cacheInstance1->isLocked($identifier1));
73+
74+
$this->assertTrue($this->cacheInstance2->lock($identifier1, 1));
75+
sleep(2);
76+
$this->assertFalse($this->cacheInstance1->isLocked($identifier1));
77+
}
78+
79+
/**
80+
* Verify that lock will not be released by another lock name.
81+
*
82+
* @return void
83+
*/
84+
public function testParallelUnlock(): void
85+
{
86+
$identifier1 = \uniqid('lock_name_1_', true);
87+
$identifier2 = \uniqid('lock_name_2_', true);
88+
89+
$this->assertTrue($this->cacheInstance1->lock($identifier1, 30));
90+
$this->assertTrue($this->cacheInstance2->lock($identifier2, 30));
91+
92+
$this->assertFalse($this->cacheInstance2->unlock($identifier1));
93+
$this->assertTrue($this->cacheInstance2->unlock($identifier2));
94+
95+
$this->assertTrue($this->cacheInstance2->isLocked($identifier1));
96+
$this->assertFalse($this->cacheInstance2->isLocked($identifier2));
97+
}
98+
99+
/**
100+
* Verify that lock will not be released by another lock name when both locks will never be expired.
101+
*
102+
* @return void
103+
*/
104+
public function testParallelUnlockNoExpiration(): void
105+
{
106+
$identifier1 = \uniqid('lock_name_1_', true);
107+
$identifier2 = \uniqid('lock_name_2_', true);
108+
109+
$this->assertTrue($this->cacheInstance1->lock($identifier1, -1));
110+
$this->assertTrue($this->cacheInstance2->lock($identifier2, -1));
111+
112+
$this->assertFalse($this->cacheInstance2->unlock($identifier1));
113+
$this->assertTrue($this->cacheInstance2->unlock($identifier2));
114+
115+
$this->assertTrue($this->cacheInstance2->isLocked($identifier1));
116+
$this->assertFalse($this->cacheInstance2->isLocked($identifier2));
117+
}
118+
}

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

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,83 @@ public function testUnlockWithAnotherSign(): void
8585
$this->frontendCacheMock
8686
->expects($this->once())->method('load')
8787
->with(self::LOCK_PREFIX . $identifier)
88-
->willReturn(\uniqid('some_rand-'));
88+
->willReturn(\uniqid('some_rand-', true));
8989

9090
$this->assertFalse($this->cache->unlock($identifier));
9191
}
92+
93+
/**
94+
* Verify that unlock method will be terminated if lockSign is empty.
95+
*
96+
* @return void
97+
*/
98+
public function testUnlockWithEmptyLockSign(): void
99+
{
100+
$identifier = 'lock_name';
101+
102+
$closure = \Closure::bind(function ($cacheInstance) {
103+
$cacheInstance->lockSign = null;
104+
}, null, $this->cache);
105+
$closure($this->cache);
106+
107+
$this->assertEquals(false, $this->cache->unlock($identifier));
108+
}
109+
110+
/**
111+
* Verify that lock will not be released when $data is empty
112+
*
113+
* @return void
114+
*/
115+
public function testUnlockWithEmptyData(): void
116+
{
117+
$identifier = 'lock_name';
118+
119+
$this->frontendCacheMock
120+
->expects($this->once())->method('load')
121+
->with(self::LOCK_PREFIX . $identifier)
122+
->willReturn(false);
123+
124+
$this->assertEquals(false, $this->cache->unlock($identifier));
125+
}
126+
127+
/**
128+
* Verify that lockSign will be generated if empty during cache lock.
129+
*
130+
* @return void
131+
*/
132+
public function testLockWithEmptyLockSign(): void
133+
{
134+
$identifier = 'lock_name';
135+
136+
$closure = \Closure::bind(function ($cacheInstance) {
137+
$cacheInstance->lockSign = null;
138+
}, null, $this->cache);
139+
$closure($this->cache);
140+
141+
$this->cache->lock($identifier, 10);
142+
143+
$closure = \Closure::bind(function ($cacheInstance) {
144+
return $cacheInstance->lockSign;
145+
}, null, $this->cache);
146+
$lockSign = $closure($this->cache);
147+
148+
$this->assertEquals(true, isset($lockSign));
149+
}
150+
151+
/**
152+
* Verify that lock will not be made when $data is not empty
153+
*
154+
* @return void
155+
*/
156+
public function testLockWithNotEmptyData(): void
157+
{
158+
$identifier = 'lock_name';
159+
160+
$this->frontendCacheMock
161+
->expects($this->once())->method('load')
162+
->with(self::LOCK_PREFIX . $identifier)
163+
->willReturn(\uniqid('some_rand-', true));
164+
165+
$this->assertEquals(false, $this->cache->lock($identifier));
166+
}
92167
}

0 commit comments

Comments
 (0)