Skip to content

Commit

Permalink
MAGE-1054: Fix queue tests (#1630)
Browse files Browse the repository at this point in the history
* MAGE-1054: refactor queue tests

* MAGE-1054: re-add the skipped tests

* MAGE-1054: fix tests
  • Loading branch information
damcou authored Oct 16, 2024
1 parent d318626 commit be5dac2
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 87 deletions.
2 changes: 1 addition & 1 deletion Helper/AlgoliaHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public function moveIndex(string $fromIndexName, string $toIndexName): void
'destination' => $toIndexName
]
);
self::setLastOperationInfo($fromIndexName, $response);
self::setLastOperationInfo($toIndexName, $response);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions Setup/Patch/Schema/ConfigPatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class ConfigPatch implements SchemaPatchInterface
],
],

'algoliasearch_instant/instant/facets' => [
'algoliasearch_instant/instant_facets/facets' => [
[
'attribute' => 'price',
'type' => 'slider',
Expand All @@ -135,7 +135,7 @@ class ConfigPatch implements SchemaPatchInterface
'create_rule' => '2',
],
],
'algoliasearch_instant/instant/sorts' => [
'algoliasearch_instant/instant_sorts/sorts' => [
[
'attribute' => 'price',
'sort' => 'asc',
Expand Down
146 changes: 62 additions & 84 deletions Test/Integration/Queue/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Algolia\AlgoliaSearch\Test\Integration\Queue;

use Algolia\AlgoliaSearch\Helper\ConfigHelper;
use Algolia\AlgoliaSearch\Model\Indexer\Product;
use Algolia\AlgoliaSearch\Model\Indexer\QueueRunner;
use Algolia\AlgoliaSearch\Model\IndicesConfigurator;
Expand All @@ -12,6 +13,10 @@
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Adapter\AdapterInterface;

/**
* @magentoDbIsolation disabled
* @magentoAppIsolation enabled
*/
class QueueTest extends TestCase
{
private const INCOMPLETE_REASON = "Must revisit transaction handling across connections.";
Expand All @@ -29,27 +34,27 @@ public function setUp(): void
{
parent::setUp();

$this->jobsCollectionFactory = $this->getObjectManager()->create(JobsCollectionFactory::class);
$this->jobsCollectionFactory = $this->objectManager->create(JobsCollectionFactory::class);

/** @var ResourceConnection $resource */
$resource = $this->getObjectManager()->create(ResourceConnection::class);
$resource = $this->objectManager->get(ResourceConnection::class);
$this->connection = $resource->getConnection();

$this->queue = $this->getObjectManager()->create(Queue::class);
$this->queue = $this->objectManager->get(Queue::class);
}

public function testFill()
{
$this->resetConfigs([
'algoliasearch_queue/queue/number_of_job_to_run',
'algoliasearch_advanced/queue/number_of_element_by_page',
ConfigHelper::NUMBER_OF_JOB_TO_RUN,
ConfigHelper::NUMBER_OF_ELEMENT_BY_PAGE,
]);

$this->setConfig('algoliasearch_queue/queue/active', '1');
$this->setConfig(ConfigHelper::IS_ACTIVE, '1');
$this->connection->query('TRUNCATE TABLE algoliasearch_queue');

/** @var Product $indexer */
$indexer = $this->getObjectManager()->create(Product::class);
$indexer = $this->objectManager->get(Product::class);
$indexer->executeFull();

$rows = $this->connection->query('SELECT * FROM algoliasearch_queue')->fetchAll();
Expand Down Expand Up @@ -80,18 +85,17 @@ public function testFill()
}
}

/**
* @depends testFill
* @magentoDbIsolation disabled
*/
public function testExecute()
{
$this->markTestIncomplete(self::INCOMPLETE_REASON);
$this->setConfig(ConfigHelper::IS_ACTIVE, '1');
$this->connection->query('TRUNCATE TABLE algoliasearch_queue');

$this->setConfig('algoliasearch_queue/queue/active', '1');
/** @var Product $indexer */
$indexer = $this->objectManager->get(Product::class);
$indexer->executeFull();

/** @var Queue $queue */
$queue = $this->getObjectManager()->create(Queue::class);
$queue = $this->objectManager->get(Queue::class);

// Run the first two jobs - saveSettings, batch
$queue->runCron(2, true);
Expand All @@ -109,7 +113,7 @@ public function testExecute()

$this->assertTrue($existsDefaultTmpIndex, 'Default products production index does not exists and it should');

// Run the second two jobs - batch, move
// Run the last move - move
$queue->runCron(2, true);

$this->algoliaHelper->waitLastTask();
Expand All @@ -132,31 +136,26 @@ public function testExecute()
$this->assertTrue($existsDefaultProdIndex, 'Default product production index does not exists and it should');

/** TODO: There are mystery items being added to queue from unknown save process on product_id=1 */
/* $rows = $this->connection->query('SELECT * FROM algoliasearch_queue')->fetchAll();
$this->assertEquals(0, count($rows)); */
$rows = $this->connection->query('SELECT * FROM algoliasearch_queue')->fetchAll();
$this->assertEquals(0, count($rows));
}

/**
* @magentoDbIsolation disabled
*/
public function testSettings()
{
$this->markTestIncomplete(self::INCOMPLETE_REASON);

$this->resetConfigs([
'algoliasearch_queue/queue/number_of_job_to_run',
'algoliasearch_advanced/queue/number_of_element_by_page',
'algoliasearch_instant/instant_facets/facets',
'algoliasearch_products/products/product_additional_attributes',
ConfigHelper::NUMBER_OF_JOB_TO_RUN,
ConfigHelper::NUMBER_OF_ELEMENT_BY_PAGE,
ConfigHelper::FACETS,
ConfigHelper::PRODUCT_ATTRIBUTES
]);

$this->setConfig('algoliasearch_queue/queue/active', '1');
$this->setConfig(ConfigHelper::IS_ACTIVE, '1');

$this->connection->query('DELETE FROM algoliasearch_queue');

// Reindex products multiple times
/** @var Product $indexer */
$indexer = $this->getObjectManager()->create(Product::class);
$indexer = $this->objectManager->get(Product::class);
$indexer->executeFull();
$indexer->executeFull();
$indexer->executeFull();
Expand All @@ -166,7 +165,7 @@ public function testSettings()

// Process the whole queue
/** @var QueueRunner $queueRunner */
$queueRunner = $this->getObjectManager()->create(QueueRunner::class);
$queueRunner = $this->objectManager->get(QueueRunner::class);
$queueRunner->executeFull();
$queueRunner->executeFull();
$queueRunner->executeFull();
Expand All @@ -181,19 +180,16 @@ public function testSettings()
$this->assertFalse(empty($settings['searchableAttributes']), 'SearchableAttributes should be set, but they are not.');
}

/**
* @magentoDbIsolation disabled
*/
public function testMergeSettings()
{
$this->setConfig('algoliasearch_queue/queue/active', '1');
$this->setConfig('algoliasearch_queue/queue/number_of_job_to_run', 1);
$this->setConfig('algoliasearch_advanced/queue/number_of_element_by_page', 300);
$this->setConfig(ConfigHelper::IS_ACTIVE, '1');
$this->setConfig(ConfigHelper::NUMBER_OF_JOB_TO_RUN, 1);
$this->setConfig(ConfigHelper::NUMBER_OF_ELEMENT_BY_PAGE, 300);

$this->connection->query('DELETE FROM algoliasearch_queue');

/** @var Product $productIndexer */
$productIndexer = $this->getObjectManager()->create(Product::class);
$productIndexer = $this->objectManager->get(Product::class);
$productIndexer->executeFull();

$rows = $this->connection->query('SELECT * FROM algoliasearch_queue')->fetchAll();
Expand All @@ -208,7 +204,7 @@ public function testMergeSettings()
$this->assertEquals(['sku'], $settings['disableTypoToleranceOnAttributes']);

/** @var QueueRunner $queueRunner */
$queueRunner = $this->getObjectManager()->create(QueueRunner::class);
$queueRunner = $this->objectManager->get(QueueRunner::class);
$queueRunner->executeFull();

$this->algoliaHelper->waitLastTask();
Expand All @@ -223,9 +219,6 @@ public function testMergeSettings()
$this->assertEquals(['sku'], $settings['disableTypoToleranceOnAttributes']);
}

/**
* @magentoDbIsolation disabled
*/
public function testMerging()
{
$this->connection->query('DELETE FROM algoliasearch_queue');
Expand Down Expand Up @@ -434,9 +427,6 @@ public function testMerging()
$this->assertEquals($expectedProductJob, $productJob->toArray());
}

/**
* @magentoDbIsolation disabled
*/
public function testMergingWithStaticMethods()
{
$this->connection->query('TRUNCATE TABLE algoliasearch_queue');
Expand Down Expand Up @@ -587,13 +577,8 @@ public function testMergingWithStaticMethods()
$this->assertEquals('rebuildStoreProductIndex', $jobs[11]->getMethod());
}

/**
* @magentoDbIsolation disabled
*/
public function testGetJobs()
{
$this->markTestIncomplete(self::INCOMPLETE_REASON);

$this->connection->query('TRUNCATE TABLE algoliasearch_queue');

$data = [
Expand Down Expand Up @@ -740,18 +725,12 @@ public function testGetJobs()

$expectedFirstJob = [
'job_id' => '1',
'created' => '2017-09-01 12:00:00',
'pid' => null,
'pid' => $pid,
'class' => \Algolia\AlgoliaSearch\Helper\Data::class,
'method' => 'rebuildStoreCategoryIndex',
'data' => '{"store_id":"1","category_ids":["9","22"]}',
'max_retries' => '3',
'retries' => '0',
'error_log' => '',
'data_size' => 3,
'merged_ids' => ['1', '7'],
'store_id' => '1',
'is_full_reindex' => 0,
'decoded_data' => [
'store_id' => '1',
'category_ids' => [
Expand All @@ -760,43 +739,48 @@ public function testGetJobs()
2 => '40',
],
],
'locked_at' => null,
'debug' => null,
];

$expectedLastJob = [
'job_id' => '6',
'created' => '2017-09-01 12:00:00',
'pid' => null,
'pid' => $pid,
'class' => \Algolia\AlgoliaSearch\Helper\Data::class,
'method' => 'rebuildStoreProductIndex',
'data' => '{"store_id":"3","product_ids":["448"]}',
'max_retries' => '3',
'retries' => '0',
'error_log' => '',
'data_size' => 2,
'merged_ids' => ['6', '12'],
'store_id' => '3',
'is_full_reindex' => 0,
'decoded_data' => [
'store_id' => '3',
'product_ids' => [
0 => '448',
1 => '405',
],
],
'locked_at' => null,
'debug' => null,
];

/** @var Job $firstJob */
$firstJob = reset($jobs);
$firstJob = $firstJob->toArray();

/** @var Job $lastJob */
$lastJob = end($jobs);
$lastJob = $lastJob->toArray();

$valuesToCheck = [
'job_id',
'method',
'class',
'store_id',
'pid',
'data',
'merged_ids',
'decoded_data',
];

$this->assertEquals($expectedFirstJob, $firstJob->toArray());
$this->assertEquals($expectedLastJob, $lastJob->toArray());
foreach ($valuesToCheck as $valueToCheck) {
$this->assertEquals($expectedFirstJob[$valueToCheck], $firstJob[$valueToCheck]);
$this->assertEquals($expectedLastJob[$valueToCheck], $lastJob[$valueToCheck]);
}

$dbJobs = $this->connection->query('SELECT * FROM algoliasearch_queue')->fetchAll();

Expand All @@ -807,14 +791,11 @@ public function testGetJobs()
}
}

/**
* @magentoDbIsolation disabled
*/
public function testHugeJob()
{
// Default value - maxBatchSize = 1000
$this->setConfig('algoliasearch_queue/queue/number_of_job_to_run', 10);
$this->setConfig('algoliasearch_advanced/queue/number_of_element_by_page', 100);
$this->setConfig(ConfigHelper::NUMBER_OF_JOB_TO_RUN, 10);
$this->setConfig(ConfigHelper::NUMBER_OF_ELEMENT_BY_PAGE, 100);

$productIds = range(1, 5000);
$jsonProductIds = json_encode($productIds);
Expand Down Expand Up @@ -851,8 +832,8 @@ public function testHugeJob()
public function testMaxSingleJobSize()
{
// Default value - maxBatchSize = 1000
$this->setConfig('algoliasearch_queue/queue/number_of_job_to_run', 10);
$this->setConfig('algoliasearch_advanced/queue/number_of_element_by_page', 100);
$this->setConfig(ConfigHelper::NUMBER_OF_JOB_TO_RUN, 10);
$this->setConfig(ConfigHelper::NUMBER_OF_ELEMENT_BY_PAGE, 100);

$productIds = range(1, 99);
$jsonProductIds = json_encode($productIds);
Expand Down Expand Up @@ -889,25 +870,22 @@ public function testMaxSingleJobSize()
$this->assertEquals($pid, $lastJob['pid']);
}

/**
* @magentoDbIsolation disabled
*/
public function testMaxSingleJobsSizeOnProductReindex()
{
$this->resetConfigs([
'algoliasearch_queue/queue/number_of_job_to_run',
'algoliasearch_advanced/queue/number_of_element_by_page',
ConfigHelper::NUMBER_OF_JOB_TO_RUN,
ConfigHelper::NUMBER_OF_ELEMENT_BY_PAGE,
]);

$this->setConfig('algoliasearch_queue/queue/active', '1');
$this->setConfig(ConfigHelper::IS_ACTIVE, '1');

$this->setConfig('algoliasearch_queue/queue/number_of_job_to_run', 10);
$this->setConfig('algoliasearch_advanced/queue/number_of_element_by_page', 100);
$this->setConfig(ConfigHelper::NUMBER_OF_JOB_TO_RUN, 10);
$this->setConfig(ConfigHelper::NUMBER_OF_ELEMENT_BY_PAGE, 100);

$this->connection->query('TRUNCATE TABLE algoliasearch_queue');

/** @var Product $indexer */
$indexer = $this->getObjectManager()->create(Product::class);
$indexer = $this->objectManager->get(Product::class);
$indexer->execute(range(1, 512));

$dbJobs = $this->connection->query('SELECT * FROM algoliasearch_queue')->fetchAll();
Expand Down

0 comments on commit be5dac2

Please sign in to comment.