Skip to content

Commit 46d7679

Browse files
committed
ACP2E-3673: allow_parallel_generation should be set through environment variable
1 parent d253a44 commit 46d7679

File tree

2 files changed

+42
-37
lines changed

2 files changed

+42
-37
lines changed

lib/internal/Magento/Framework/Cache/LockGuardedCacheLoader.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2019 Adobe
4+
* All Rights Reserved.
55
*/
6-
76
namespace Magento\Framework\Cache;
87

98
use Magento\Framework\App\ObjectManager;
@@ -66,17 +65,16 @@ class LockGuardedCacheLoader
6665
*
6766
* @var string
6867
*/
69-
private const CONFIG_NAME_ALLOW_PARALLEL_CACHE_GENERATION = 'allow_parallel_generation';
68+
private const CONFIG_PATH_ALLOW_PARALLEL_CACHE_GENERATION = 'cache/allow_parallel_generation';
7069

7170
/**
7271
* Config value of parallel generation.
7372
*
74-
* @var bool
73+
* @var bool|null
7574
*/
76-
private $allowParallelGenerationConfigValue;
75+
private ?bool $allowParallelGenerationConfigValue;
7776

7877
/**
79-
* LockGuardedCacheLoader constructor.
8078
* @param LockManagerInterface $locker
8179
* @param int $lockTimeout
8280
* @param int $delayTimeout
@@ -119,11 +117,8 @@ public function lockedLoadData(
119117
$deadline = microtime(true) + $this->loadTimeout / 1000;
120118

121119
if (empty($this->allowParallelGenerationConfigValue)) {
122-
$cacheConfig = $this
123-
->deploymentConfig
124-
->getConfigData('cache');
125-
$this->allowParallelGenerationConfigValue = $cacheConfig[self::CONFIG_NAME_ALLOW_PARALLEL_CACHE_GENERATION]
126-
?? false;
120+
$this->allowParallelGenerationConfigValue = (bool) $this->deploymentConfig
121+
->get(self::CONFIG_PATH_ALLOW_PARALLEL_CACHE_GENERATION);
127122
}
128123

129124
while ($cachedData === false) {
@@ -139,7 +134,7 @@ public function lockedLoadData(
139134
} finally {
140135
$this->locker->unlock($lockName);
141136
}
142-
} elseif ($this->allowParallelGenerationConfigValue === true) {
137+
} elseif ($this->allowParallelGenerationConfigValue) {
143138
return $dataCollector();
144139
}
145140

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

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2019 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

88
namespace Magento\Framework\Cache\Test\Unit;
99

10+
use Magento\Framework\App\DeploymentConfig;
1011
use Magento\Framework\Cache\LockGuardedCacheLoader;
1112
use Magento\Framework\Lock\LockManagerInterface;
12-
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
1313
use PHPUnit\Framework\MockObject\MockObject;
1414
use PHPUnit\Framework\TestCase;
1515

@@ -20,25 +20,27 @@ class LockGuardedCacheLoaderTest extends TestCase
2020
*/
2121
private $lockManagerInterfaceMock;
2222

23+
/**
24+
* @var DeploymentConfig|MockObject
25+
*/
26+
private $deploymentConfigMock;
27+
2328
/**
2429
* @var LockGuardedCacheLoader
2530
*/
26-
private $LockGuardedCacheLoader;
31+
private $lockGuardedCacheLoader;
2732

2833
/**
2934
* @inheritDoc
3035
*/
3136
protected function setUp(): void
3237
{
33-
$this->lockManagerInterfaceMock = $this->getMockForAbstractClass(LockManagerInterface::class);
38+
$this->lockManagerInterfaceMock = $this->createMock(LockManagerInterface::class);
39+
$this->deploymentConfigMock = $this->createMock(DeploymentConfig::class);
3440

35-
$objectManager = new ObjectManagerHelper($this);
36-
37-
$this->LockGuardedCacheLoader = $objectManager->getObject(
38-
LockGuardedCacheLoader::class,
39-
[
40-
'locker' => $this->lockManagerInterfaceMock
41-
]
41+
$this->lockGuardedCacheLoader = new LockGuardedCacheLoader(
42+
$this->lockManagerInterfaceMock,
43+
deploymentConfig: $this->deploymentConfigMock
4244
);
4345
}
4446

@@ -63,12 +65,16 @@ public function testOptimisticDataRead(): void
6365
return true;
6466
};
6567

68+
$this->deploymentConfigMock->expects($this->once())
69+
->method('get')
70+
->with('cache/allow_parallel_generation')
71+
->willReturn(false);
6672
$this->lockManagerInterfaceMock->expects($this->never())->method('lock');
6773
$this->lockManagerInterfaceMock->expects($this->never())->method('unlock');
6874

6975
$this->assertEquals(
7076
'loaded_data',
71-
$this->LockGuardedCacheLoader->lockedLoadData($lockName, $dataLoader, $dataCollector, $dataSaver)
77+
$this->lockGuardedCacheLoader->lockedLoadData($lockName, $dataLoader, $dataCollector, $dataSaver)
7278
);
7379
}
7480

@@ -93,16 +99,19 @@ public function testDataCollectedAfterDeadlineReached(): void
9399
return true;
94100
};
95101

102+
$this->deploymentConfigMock->expects($this->once())
103+
->method('get')
104+
->with('cache/allow_parallel_generation')
105+
->willReturn(false);
96106
$this->lockManagerInterfaceMock
97107
->expects($this->atLeastOnce())->method('lock')
98108
->with($lockName, 0)
99109
->willReturn(false);
100-
101110
$this->lockManagerInterfaceMock->expects($this->never())->method('unlock');
102111

103112
$this->assertEquals(
104113
'collected_data',
105-
$this->LockGuardedCacheLoader->lockedLoadData($lockName, $dataLoader, $dataCollector, $dataSaver)
114+
$this->lockGuardedCacheLoader->lockedLoadData($lockName, $dataLoader, $dataCollector, $dataSaver)
106115
);
107116
}
108117

@@ -127,16 +136,19 @@ public function testDataWrite(): void
127136
return true;
128137
};
129138

139+
$this->deploymentConfigMock->expects($this->once())
140+
->method('get')
141+
->with('cache/allow_parallel_generation')
142+
->willReturn(false);
130143
$this->lockManagerInterfaceMock
131144
->expects($this->once())->method('lock')
132145
->with($lockName, 0)
133146
->willReturn(true);
134-
135147
$this->lockManagerInterfaceMock->expects($this->once())->method('unlock');
136148

137149
$this->assertEquals(
138150
'collected_data',
139-
$this->LockGuardedCacheLoader->lockedLoadData($lockName, $dataLoader, $dataCollector, $dataSaver)
151+
$this->lockGuardedCacheLoader->lockedLoadData($lockName, $dataLoader, $dataCollector, $dataSaver)
140152
);
141153
}
142154

@@ -161,21 +173,19 @@ public function testDataCollectedWithParallelGeneration(): void
161173
return true;
162174
};
163175

164-
$closure = \Closure::bind(function ($cacheLoader) {
165-
return $cacheLoader->allowParallelGenerationConfigValue = true;
166-
}, null, $this->LockGuardedCacheLoader);
167-
$closure($this->LockGuardedCacheLoader);
168-
176+
$this->deploymentConfigMock->expects($this->once())
177+
->method('get')
178+
->with('cache/allow_parallel_generation')
179+
->willReturn(true);
169180
$this->lockManagerInterfaceMock
170181
->expects($this->once())->method('lock')
171182
->with($lockName, 0)
172183
->willReturn(false);
173-
174184
$this->lockManagerInterfaceMock->expects($this->never())->method('unlock');
175185

176186
$this->assertEquals(
177187
'collected_data',
178-
$this->LockGuardedCacheLoader->lockedLoadData($lockName, $dataLoader, $dataCollector, $dataSaver)
188+
$this->lockGuardedCacheLoader->lockedLoadData($lockName, $dataLoader, $dataCollector, $dataSaver)
179189
);
180190
}
181191
}

0 commit comments

Comments
 (0)