Skip to content

Commit 1b7b32b

Browse files
merge magento/2.4-develop into magento-tsg/2.4-develop-pr51
2 parents 9d94c34 + 5fca50e commit 1b7b32b

File tree

3 files changed

+328
-14
lines changed

3 files changed

+328
-14
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function lockedLoadData(
116116
callable $dataSaver
117117
) {
118118
$cachedData = $dataLoader(); //optimistic read
119-
$deadline = microtime(true) + $this->loadTimeout / 100;
119+
$deadline = microtime(true) + $this->loadTimeout / 1000;
120120

121121
if (empty($this->allowParallelGenerationConfigValue)) {
122122
$cacheConfig = $this

lib/internal/Magento/Framework/Cache/Test/Unit/Backend/RemoteSynchronizedCacheTest.php

Lines changed: 146 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@ protected function setUp(): void
6161
}
6262

6363
/**
64+
* Test that exception is thrown if cache is not configured.
65+
*
6466
* @param array $options
6567
*
6668
* @dataProvider initializeWithExceptionDataProvider
6769
*/
68-
public function testInitializeWithException($options)
70+
public function testInitializeWithException($options): void
6971
{
7072
$this->expectException('Zend_Cache_Exception');
7173
$this->objectManager->getObject(
@@ -79,7 +81,7 @@ public function testInitializeWithException($options)
7981
/**
8082
* @return array
8183
*/
82-
public function initializeWithExceptionDataProvider()
84+
public function initializeWithExceptionDataProvider(): array
8385
{
8486
return [
8587
'empty_backend_option' => [
@@ -104,11 +106,13 @@ public function initializeWithExceptionDataProvider()
104106
}
105107

106108
/**
109+
* Test that exception is not thrown if cache is configured.
110+
*
107111
* @param array $options
108112
*
109113
* @dataProvider initializeWithOutExceptionDataProvider
110114
*/
111-
public function testInitializeWithOutException($options)
115+
public function testInitializeWithOutException($options): void
112116
{
113117
$result = $this->objectManager->getObject(
114118
RemoteSynchronizedCache::class,
@@ -122,7 +126,7 @@ public function testInitializeWithOutException($options)
122126
/**
123127
* @return array
124128
*/
125-
public function initializeWithOutExceptionDataProvider()
129+
public function initializeWithOutExceptionDataProvider(): array
126130
{
127131
$connectionMock = $this->getMockBuilder(Mysql::class)
128132
->disableOriginalConstructor()
@@ -151,9 +155,11 @@ public function initializeWithOutExceptionDataProvider()
151155
}
152156

153157
/**
154-
* Test that load will always return newest data.
158+
* Test that load will return the newest data.
159+
*
160+
* @return void
155161
*/
156-
public function testLoadWithLocalData()
162+
public function testLoad(): void
157163
{
158164
$localData = 1;
159165
$remoteData = 2;
@@ -182,7 +188,12 @@ public function testLoadWithLocalData()
182188
$this->assertEquals($remoteData, $this->remoteSyncCacheInstance->load(1));
183189
}
184190

185-
public function testLoadWithNoLocalAndNoRemoteData()
191+
/**
192+
* Test that load will not return data when no local data and no remote data exist.
193+
*
194+
* @return void
195+
*/
196+
public function testLoadWithNoLocalAndNoRemoteData(): void
186197
{
187198
$localData = false;
188199
$remoteData = false;
@@ -197,10 +208,15 @@ public function testLoadWithNoLocalAndNoRemoteData()
197208
->method('load')
198209
->willReturn($remoteData);
199210

200-
$this->assertEquals($remoteData, $this->remoteSyncCacheInstance->load(1));
211+
$this->assertEquals(false, $this->remoteSyncCacheInstance->load(1));
201212
}
202213

203-
public function testLoadWithNoLocalAndRemoteData()
214+
/**
215+
* Test that load will return the newest data when only remote data exists.
216+
*
217+
* @return void
218+
*/
219+
public function testLoadWithNoLocalAndWithRemoteData(): void
204220
{
205221
$localData = false;
206222
$remoteData = 1;
@@ -223,7 +239,109 @@ public function testLoadWithNoLocalAndRemoteData()
223239
$this->assertEquals($remoteData, $this->remoteSyncCacheInstance->load(1));
224240
}
225241

226-
public function testRemove()
242+
/**
243+
* Test that load will return the newest data when local data and remote data are the same.
244+
*
245+
* @return void
246+
*/
247+
public function testLoadWithEqualLocalAndRemoteData(): void
248+
{
249+
$localData = 1;
250+
$remoteData = 1;
251+
252+
$this->localCacheMockExample
253+
->expects($this->at(0))
254+
->method('load')
255+
->willReturn($localData);
256+
257+
$this->remoteCacheMockExample
258+
->expects($this->at(0))
259+
->method('load')
260+
->willReturn(\hash('sha256', (string)$remoteData));
261+
262+
$this->assertEquals($localData, $this->remoteSyncCacheInstance->load(1));
263+
}
264+
265+
/**
266+
* Test that load will return stale cache.
267+
*
268+
* @return void
269+
*/
270+
public function testLoadWithStaleCache(): void
271+
{
272+
$localData = 1;
273+
274+
$this->localCacheMockExample
275+
->expects($this->at(0))
276+
->method('load')
277+
->willReturn($localData);
278+
279+
$this->remoteCacheMockExample
280+
->expects($this->at(0))
281+
->method('load')
282+
->willReturn(false);
283+
284+
$closure = \Closure::bind(function ($cacheInstance) {
285+
$cacheInstance->_options['use_stale_cache'] = true;
286+
}, null, $this->remoteSyncCacheInstance);
287+
$closure($this->remoteSyncCacheInstance);
288+
289+
$this->remoteCacheMockExample
290+
->expects($this->at(2))
291+
->method('load')
292+
->willReturn(true);
293+
294+
$this->assertEquals($localData, $this->remoteSyncCacheInstance->load(1));
295+
}
296+
297+
/**
298+
* Test that load will generate data on the first attempt.
299+
*
300+
* @return void
301+
*/
302+
public function testLoadWithoutStaleCache(): void
303+
{
304+
$localData = 1;
305+
306+
$this->localCacheMockExample
307+
->expects($this->at(0))
308+
->method('load')
309+
->willReturn($localData);
310+
311+
$this->remoteCacheMockExample
312+
->expects($this->at(0))
313+
->method('load')
314+
->willReturn(false);
315+
316+
$closure = \Closure::bind(function ($cacheInstance) {
317+
$cacheInstance->_options['use_stale_cache'] = true;
318+
}, null, $this->remoteSyncCacheInstance);
319+
$closure($this->remoteSyncCacheInstance);
320+
321+
$this->remoteCacheMockExample
322+
->expects($this->at(2))
323+
->method('load')
324+
->willReturn(false);
325+
326+
$closure = \Closure::bind(function ($cacheInstance) {
327+
return $cacheInstance->lockSign;
328+
}, null, $this->remoteSyncCacheInstance);
329+
$lockSign = $closure($this->remoteSyncCacheInstance);
330+
331+
$this->remoteCacheMockExample
332+
->expects($this->at(4))
333+
->method('load')
334+
->willReturn($lockSign);
335+
336+
$this->assertEquals(false, $this->remoteSyncCacheInstance->load(1));
337+
}
338+
339+
/**
340+
* Test data remove.
341+
*
342+
* @return void
343+
*/
344+
public function testRemove(): void
227345
{
228346
$this->remoteCacheMockExample
229347
->expects($this->exactly(2))
@@ -238,7 +356,12 @@ public function testRemove()
238356
$this->remoteSyncCacheInstance->remove(1);
239357
}
240358

241-
public function testClean()
359+
/**
360+
* Test data clean.
361+
*
362+
* @return void
363+
*/
364+
public function testClean(): void
242365
{
243366
$this->remoteCacheMockExample
244367
->expects($this->exactly(1))
@@ -248,7 +371,12 @@ public function testClean()
248371
$this->remoteSyncCacheInstance->clean();
249372
}
250373

251-
public function testSaveWithEqualRemoteData()
374+
/**
375+
* Test data save when remote data exist.
376+
*
377+
* @return void
378+
*/
379+
public function testSaveWithEqualRemoteData(): void
252380
{
253381
$remoteData = 1;
254382

@@ -285,7 +413,12 @@ public function testSaveWithMismatchedRemoteData()
285413
$this->remoteSyncCacheInstance->save(2, 1);
286414
}
287415

288-
public function testSaveWithoutRemoteData()
416+
/**
417+
* Test data save when remote data is not exist.
418+
*
419+
* @return void
420+
*/
421+
public function testSaveWithoutRemoteData(): void
289422
{
290423
$this->remoteCacheMockExample
291424
->expects($this->at(0))

0 commit comments

Comments
 (0)