Skip to content

Commit 7be2b8c

Browse files
authored
Merge pull request #2885 from magento-performance/MAGETWO-92402
[performance] MAGETWO-92402: Write tests for price indexer parallelization
2 parents 4e63cc4 + 79dc9f1 commit 7be2b8c

22 files changed

+1647
-3
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@
8181
"lusitanian/oauth": "~0.8.10",
8282
"sebastian/phpcpd": "2.0.4"
8383
},
84+
"suggest": {
85+
"ext-pcntl": "Need for run processes in parallel mode"
86+
},
8487
"replace": {
8588
"magento/module-marketplace": "100.2.2",
8689
"magento/module-admin-notification": "100.2.3",
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\TestFramework\Annotation;
8+
9+
use Magento\Catalog\Model\Indexer\Product\Price\ModeSwitcher;
10+
use Magento\Catalog\Model\Indexer\Product\Price\Processor;
11+
use Magento\Framework\App\Cache\TypeListInterface;
12+
use Magento\Framework\App\Config\ConfigResource\ConfigInterface;
13+
use Magento\Framework\App\Config\ScopeConfigInterface;
14+
use Magento\Framework\ObjectManagerInterface;
15+
use Magento\TestFramework\App\Config;
16+
use Magento\TestFramework\Helper\Bootstrap;
17+
use Magento\Catalog\Model\Indexer\Product\Price\DimensionModeConfiguration;
18+
use PHPUnit\Framework\TestCase;
19+
20+
/**
21+
* Implementation of the @magentoIndexerDimensionMode DocBlock annotation
22+
*/
23+
class IndexerDimensionMode
24+
{
25+
/** @var TypeListInterface */
26+
private $cacheTypeList;
27+
28+
/** @var ScopeConfigInterface */
29+
private $configReader;
30+
31+
/** @var ModeSwitcher */
32+
private $modeSwitcher;
33+
34+
/** @var ConfigInterface */
35+
private $configWriter;
36+
37+
/** @var ObjectManagerInterface */
38+
private $objectManager;
39+
40+
/** @var \Magento\TestFramework\Db\Mysql */
41+
private $db;
42+
43+
/** @var bool */
44+
private $isDimensionMode = false;
45+
46+
private function restoreDb()
47+
{
48+
$this->db = Bootstrap::getInstance()->getBootstrap()->getApplication()->getDbInstance();
49+
$this->objectManager = Bootstrap::getObjectManager();
50+
$this->db->restoreFromDbDump();
51+
$this->cacheTypeList = $this->objectManager->get(TypeListInterface::class);
52+
$this->cacheTypeList->cleanType('config');
53+
$this->objectManager->get(Config::class)->clean();
54+
}
55+
56+
/**
57+
* @param string $mode
58+
*/
59+
private function setDimensionMode($mode, $test)
60+
{
61+
$this->objectManager = Bootstrap::getObjectManager();
62+
$this->modeSwitcher = $this->objectManager->get(ModeSwitcher::class);
63+
$this->configWriter = $this->objectManager->get(ConfigInterface::class);
64+
$this->configReader = $this->objectManager->get(ScopeConfigInterface::class);
65+
$this->cacheTypeList = $this->objectManager->get(TypeListInterface::class);
66+
67+
$this->configReader->clean();
68+
$previousMode = $this->configReader->getValue(ModeSwitcher::XML_PATH_PRICE_DIMENSIONS_MODE) ?:
69+
DimensionModeConfiguration::DIMENSION_NONE;
70+
71+
if ($previousMode !== $mode) {
72+
//Create new tables and move data
73+
$this->modeSwitcher->createTables($mode);
74+
$this->modeSwitcher->moveData($mode, $previousMode);
75+
76+
//Change config options
77+
$this->configWriter->saveConfig(ModeSwitcher::XML_PATH_PRICE_DIMENSIONS_MODE, $mode);
78+
$this->cacheTypeList->cleanType('config');
79+
$this->objectManager->get(Config::class)->clean();
80+
81+
//Delete old tables
82+
$this->modeSwitcher->dropTables($previousMode);
83+
} else {
84+
$this->fail('Dimensions mode for indexer has not been changed', $test);
85+
}
86+
}
87+
88+
/**
89+
* Handler for 'startTest' event
90+
*
91+
* @param TestCase $test
92+
* @return void
93+
*/
94+
public function startTest(TestCase $test)
95+
{
96+
$source = $test->getAnnotations();
97+
98+
if (isset($source['method']['magentoIndexerDimensionMode'])) {
99+
$annotations = $source['method']['magentoIndexerDimensionMode'];
100+
} elseif (isset($source['class']['magentoIndexerDimensionMode'])) {
101+
$annotations = $source['class']['magentoIndexerDimensionMode'];
102+
} else {
103+
return;
104+
}
105+
106+
$dbIsolation = $source['method']['magentoDbIsolation']
107+
?? $source['class']['magentoDbIsolation']
108+
?? ['disabled'];
109+
110+
if ($dbIsolation[0] != 'disabled') {
111+
$this->fail("Invalid @magentoDbIsolation declaration: $dbIsolation[0]", $test);
112+
}
113+
114+
list($indexerType, $indexerMode) = explode(' ', $annotations[0]);
115+
116+
if ($indexerType == Processor::INDEXER_ID) {
117+
$this->isDimensionMode = true;
118+
$this->setDimensionMode($indexerMode, $test);
119+
}
120+
}
121+
122+
/**
123+
* Handler for 'endTest' event
124+
*
125+
* @return void
126+
*/
127+
public function endTest()
128+
{
129+
if ($this->isDimensionMode) {
130+
$this->restoreDb();
131+
$this->isDimensionMode = false;
132+
}
133+
}
134+
135+
/**
136+
* Fails the test with specified error message
137+
*
138+
* @param string $message
139+
* @param TestCase $test
140+
* @throws \Exception
141+
*/
142+
private function fail($message, TestCase $test)
143+
{
144+
$test->fail("{$message} in the test '{$test->toString()}'");
145+
}
146+
}

dev/tests/integration/framework/Magento/TestFramework/Application.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ public function install($cleanup)
485485
);
486486

487487
// right after a clean installation, store DB dump for future reuse in tests or running the test suite again
488-
if (!$db->isDbDumpExists()) {
488+
if (!$db->isDbDumpExists() || $cleanup) {
489489
$this->getDbInstance()->storeDbDump();
490490
}
491491
}

dev/tests/integration/framework/Magento/TestFramework/Bootstrap/DocBlock.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ protected function _getSubscribers(\Magento\TestFramework\Application $applicati
5454
new \Magento\TestFramework\Isolation\WorkingDirectory(),
5555
new \Magento\TestFramework\Isolation\DeploymentConfig(),
5656
new \Magento\TestFramework\Annotation\AppIsolation($application),
57+
new \Magento\TestFramework\Annotation\IndexerDimensionMode($application),
5758
new \Magento\TestFramework\Isolation\AppConfig(),
5859
new \Magento\TestFramework\Annotation\ConfigFixture(),
5960
new \Magento\TestFramework\Annotation\DataFixtureBeforeTransaction($this->_fixturesBaseDir),

dev/tests/integration/testsuite/Magento/Bundle/Block/Catalog/Product/View/Type/BundleTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
/**
99
* @magentoDataFixture Magento/Bundle/_files/product.php
10+
* @magentoDbIsolation disabled
1011
* @magentoAppArea frontend
1112
*/
1213
class BundleTest extends \PHPUnit\Framework\TestCase

dev/tests/integration/testsuite/Magento/Bundle/Controller/ProductTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class ProductTest extends \Magento\TestFramework\TestCase\AbstractController
1313
{
1414
/**
1515
* @magentoDataFixture Magento/Bundle/_files/product.php
16+
* @magentoDbIsolation disabled
1617
*/
1718
public function testViewAction()
1819
{

0 commit comments

Comments
 (0)