Skip to content

Commit e442d50

Browse files
committed
ACP2E-4345: Investigate the reasons for increased redis keys and cache keys creation
1 parent d22aaf7 commit e442d50

File tree

1 file changed

+162
-1
lines changed
  • app/code/Magento/RemoteStorage/Test/Unit/Driver/Adpater/Cache

1 file changed

+162
-1
lines changed

app/code/Magento/RemoteStorage/Test/Unit/Driver/Adpater/Cache/GenericTest.php

Lines changed: 162 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Magento\RemoteStorage\Test\Unit\Driver\Adpater\Cache;
88

9+
use Magento\RemoteStorage\Driver\Adapter\Cache\CacheInterface as AdapterCacheInterface;
910
use Magento\Framework\App\CacheInterface;
1011
use Magento\Framework\Serialize\SerializerInterface;
1112
use Magento\RemoteStorage\Driver\Adapter\Cache\Generic;
@@ -38,19 +39,176 @@ class GenericTest extends TestCase
3839
*/
3940
private PathUtil $pathUtilMock;
4041

42+
/**
43+
* @var int
44+
*/
45+
private int $ttl;
46+
4147
protected function setUp(): void
4248
{
4349
$this->cacheAdapterMock = $this->createMock(CacheInterface::class);
4450
$this->serializerMock = $this->createMock(SerializerInterface::class);
4551
$this->pathUtilMock = $this->createMock(PathUtil::class);
52+
$this->ttl = 300;
4653

4754
$this->generic = new Generic(
4855
$this->cacheAdapterMock,
4956
$this->serializerMock,
50-
$this->pathUtilMock
57+
$this->pathUtilMock,
58+
'flysystem:',
59+
$this->ttl
5160
);
5261
}
5362

63+
/**
64+
* @return void
65+
*/
66+
public function testUpdateMetadataPersists(): void
67+
{
68+
$path = 'dir/file.txt';
69+
$objectMetadata = ['type' => 'file', 'size' => 123];
70+
71+
$this->pathUtilMock
72+
->expects($this->exactly(2))
73+
->method('pathInfo')
74+
->willReturnOnConsecutiveCalls(
75+
['dirname' => 'dir', 'path' => $path],
76+
['dirname' => '', 'path' => 'dir']
77+
);
78+
79+
$this->serializerMock
80+
->expects($this->once())
81+
->method('serialize')
82+
->with($this->callback(function (array $data) use ($path, $objectMetadata) {
83+
return isset($data[$path])
84+
&& $data[$path]['type'] === 'file'
85+
&& $data[$path]['size'] === 123
86+
&& $data[$path]['dirname'] === 'dir'
87+
&& $data[$path]['path'] === $path;
88+
}))
89+
->willReturn('SERIALIZED_DATA');
90+
91+
$this->cacheAdapterMock
92+
->expects($this->once())
93+
->method('save')
94+
->with(
95+
'SERIALIZED_DATA',
96+
'flysystem:' . $path,
97+
[AdapterCacheInterface::CACHE_TAG],
98+
$this->ttl
99+
);
100+
101+
$this->cacheAdapterMock
102+
->expects($this->never())
103+
->method('load');
104+
105+
$this->generic->updateMetadata($path, $objectMetadata, true);
106+
}
107+
108+
/**
109+
* @return void
110+
*/
111+
public function testStoreFileNotExistsMarksPathAsNonExistingAndPersists(): void
112+
{
113+
$path = 'missing/file.txt';
114+
115+
$this->serializerMock
116+
->expects($this->once())
117+
->method('serialize')
118+
->with([$path => false])
119+
->willReturn('SERIALIZED_MISSING');
120+
121+
$this->cacheAdapterMock
122+
->expects($this->once())
123+
->method('save')
124+
->with(
125+
'SERIALIZED_MISSING',
126+
'flysystem:' . $path,
127+
[AdapterCacheInterface::CACHE_TAG],
128+
$this->ttl
129+
);
130+
131+
$this->generic->storeFileNotExists($path);
132+
133+
$this->cacheAdapterMock
134+
->expects($this->never())
135+
->method('load');
136+
137+
$this->assertFalse($this->generic->exists($path));
138+
}
139+
140+
/**
141+
* @return void
142+
*/
143+
public function testMoveFileMovesMetadataAndPurgesOldKey(): void
144+
{
145+
$oldPath = 'old/file.txt';
146+
$newPath = 'new/file.txt';
147+
148+
$this->cacheAdapterMock
149+
->expects($this->exactly(2))
150+
->method('load')
151+
->willReturnOnConsecutiveCalls(
152+
'SERIALIZED_OLD',
153+
false
154+
);
155+
156+
$oldMeta = [
157+
$oldPath => [
158+
'path' => $oldPath,
159+
'dirname' => 'old',
160+
'type' => 'file',
161+
],
162+
];
163+
164+
$this->serializerMock
165+
->expects($this->once())
166+
->method('unserialize')
167+
->with('SERIALIZED_OLD')
168+
->willReturn($oldMeta);
169+
170+
$this->pathUtilMock
171+
->expects($this->once())
172+
->method('pathInfo')
173+
->with($newPath)
174+
->willReturn(['dirname' => 'new', 'path' => $newPath]);
175+
176+
$this->serializerMock
177+
->expects($this->once())
178+
->method('serialize')
179+
->with($this->callback(function (array $data) use ($newPath) {
180+
if (!isset($data[$newPath])) {
181+
return false;
182+
}
183+
$meta = $data[$newPath];
184+
185+
return $meta['path'] === $newPath
186+
&& $meta['dirname'] === 'new'
187+
&& $meta['type'] === 'file';
188+
}))
189+
->willReturn('SERIALIZED_NEW');
190+
191+
$this->cacheAdapterMock
192+
->expects($this->once())
193+
->method('save')
194+
->with(
195+
'SERIALIZED_NEW',
196+
'flysystem:' . $newPath,
197+
[AdapterCacheInterface::CACHE_TAG],
198+
$this->ttl
199+
);
200+
201+
$this->cacheAdapterMock
202+
->expects($this->once())
203+
->method('remove')
204+
->with('flysystem:' . $oldPath);
205+
206+
$this->generic->moveFile($oldPath, $newPath);
207+
208+
$this->assertTrue($this->generic->exists($newPath));
209+
$this->assertNull($this->generic->exists($oldPath));
210+
}
211+
54212
/**
55213
* @param string $input
56214
* @param array|null $expectedOutput
@@ -117,6 +275,9 @@ public static function metaDataProvider(): array
117275
];
118276
}
119277

278+
/**
279+
* @return void
280+
*/
120281
protected function tearDown(): void
121282
{
122283
unset($this->generic);

0 commit comments

Comments
 (0)