Skip to content

Commit 666f7a4

Browse files
committed
MAGE-1218 Emulate failed detach in integration test
1 parent b232a0c commit 666f7a4

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

Test/Integration/Product/ReplicaIndexingTest.php

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public function testReplicaRebuild(): void
202202
*/
203203
public function testReplicaSync(): void
204204
{
205-
$primaryIndexName = $this->getIndexName('default');
205+
$primaryIndexName = $this->indexName;
206206

207207
// Make one replica virtual
208208
$this->mockSortUpdate('created_at', 'desc', ['virtualReplica' => 1]);
@@ -244,10 +244,77 @@ public function testReplicaDelete(): void
244244
}
245245
}
246246

247+
/**
248+
* Test
249+
* @magentoConfigFixture current_store algoliasearch_instant/instant/is_instant_enabled 1
250+
*/
251+
public function testReplicaDeleteUnreliable(): void
252+
{
253+
$primaryIndexName = $this->indexName;
254+
255+
$sorting = $this->populateReplicas(1);
256+
257+
$currentSettings = $this->algoliaHelper->getSettings($primaryIndexName);
258+
$this->assertArrayHasKey('replicas', $currentSettings);
259+
$replicas = $currentSettings['replicas'];
260+
261+
$this->assertEquals(count($sorting), count($replicas));
262+
263+
$this->getCrippledReplicaManager()->deleteReplicasFromAlgolia(1);
264+
265+
$newSettings = $this->algoliaHelper->getSettings($primaryIndexName);
266+
$this->assertArrayNotHasKey('replicas', $newSettings);
267+
foreach ($replicas as $replica) {
268+
$this->assertIndexNotExists($this->extractIndexFromReplicaSetting($replica));
269+
}
270+
}
271+
247272
protected function extractIndexFromReplicaSetting(string $setting): string {
248273
return preg_replace('/^virtual\((.*)\)$/', '$1', $setting);
249274
}
250275

276+
/**
277+
* If a replica fails to detach from the primary it can create deletion errors
278+
* Typically this is the result of latency even if task reports as completed from the API (hypothesis)
279+
* This aims to reproduce this potential scenario by not disassociating the replica
280+
*
281+
*/
282+
protected function getCrippledReplicaManager(): ReplicaManagerInterface
283+
{
284+
$mockedClass = \Algolia\AlgoliaSearch\Service\Product\ReplicaManager::class;
285+
$mockedMethod = 'clearReplicasSettingInAlgolia';
286+
$mockedReplicaManager = $this->getMockBuilder($mockedClass)
287+
->setConstructorArgs([
288+
$this->objectManager->get(ConfigHelper::class),
289+
$this->objectManager->get(AlgoliaHelper::class),
290+
$this->objectManager->get(ReplicaState::class),
291+
$this->objectManager->get(VirtualReplicaValidatorFactory::class),
292+
$this->objectManager->get(IndexNameFetcher::class),
293+
$this->objectManager->get(StoreNameFetcher::class),
294+
$this->objectManager->get(SortingTransformer::class),
295+
$this->objectManager->get(StoreManagerInterface::class),
296+
$this->objectManager->get(Logger::class)
297+
])
298+
->onlyMethods([$mockedMethod])
299+
->getMock();
300+
$mockedReplicaManager
301+
->expects($this->once())
302+
->method($mockedMethod)
303+
->willReturnCallback(
304+
function (...$params)
305+
use ($mockedClass, $mockedMethod, $mockedReplicaManager)
306+
{
307+
// DO NOTHING
308+
return;
309+
310+
// If aiming to test a throttle on retry invoke after a specified number of failures
311+
//$originalMethod = new \ReflectionMethod($mockedClass, $mockedMethod);
312+
//return $originalMethod->invoke($mockedReplicaManager, ...$params);
313+
}
314+
);
315+
return $mockedReplicaManager;
316+
}
317+
251318
/**
252319
* Populate replica indices for test based on store id and return sorting configuration used
253320
*

0 commit comments

Comments
 (0)