Skip to content

Commit

Permalink
chore(CTS): fix flakiness
Browse files Browse the repository at this point in the history
  • Loading branch information
chloelbn committed Mar 31, 2021
1 parent 00aad33 commit f226cf8
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/SearchClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function isAlive($requestOptions = array())

public function multipleQueries($queries, $requestOptions = array())
{
$queries = array_map(function($query) {
$queries = array_map(function ($query) {
$query['params'] = isset($query['params']) ?
Helpers::serializeQueryParameters($query['params']) :
Helpers::serializeQueryParameters(array());
Expand Down
10 changes: 8 additions & 2 deletions tests/Integration/AnalyticsClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ public function testAbTesting()
$cpt++;
} while (false);

$response = $analyticsClient->addABTest($abTest);
$response = TestHelper::retry(function () use ($analyticsClient, $abTest) {
return $analyticsClient->addABTest($abTest);
}, 0.1, 40);

$abTestId = $response['abTestID'];
$index->waitTask($response['taskID']);

Expand Down Expand Up @@ -160,7 +163,10 @@ public function testAaTesting()
$cpt++;
} while (false);

$response = $analyticsClient->addABTest($aaTest);
$response = TestHelper::retry(function () use ($analyticsClient, $aaTest) {
return $analyticsClient->addABTest($aaTest);
}, 0.1, 40);

$aaTestId = $response['abTestID'];
TestHelper::getClient()->waitTask($this->indexes['aa_testing'], $response['taskID']);

Expand Down
1 change: 1 addition & 0 deletions tests/Integration/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public static function setUpBeforeClass()
TestHelper::checkEnvironmentVariables();
} catch (\Exception $e) {
echo $e->getMessage()."\n";

return;
}

Expand Down
63 changes: 14 additions & 49 deletions tests/Integration/SearchClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Algolia\AlgoliaSearch\Tests\Integration;

use Algolia\AlgoliaSearch\Exceptions\NotFoundException;
use Algolia\AlgoliaSearch\Response\MultiResponse;
use Algolia\AlgoliaSearch\SearchClient;
use Algolia\AlgoliaSearch\SearchIndex;
Expand Down Expand Up @@ -271,29 +270,13 @@ public function testApiKeys()

TestHelper::getClient()->updateApiKey($res['key'], $newParams)->wait();

$retry = 1;
$time = 100000;
$maxRetries = 100;
do {
if ($retry >= $maxRetries) {
break;
}

try {
$updatedApiKey = TestHelper::getClient()->getApiKey($res['key']);

if ($updatedApiKey['maxHitsPerQuery'] !== $apiKey['maxHitsPerQuery']) {
$this->assertEquals(42, $updatedApiKey['maxHitsPerQuery']);
break;
}
} catch (NotFoundException $e) {
// Try again
}
$updatedApiKey = TestHelper::retry(function () use ($res) {
return TestHelper::getClient()->getApiKey($res['key']);
});

$retry++;
$factor = ceil($retry / 10);
usleep($factor * $time); // 0.1 second
} while (true);
if ($updatedApiKey['maxHitsPerQuery'] !== $apiKey['maxHitsPerQuery']) {
$this->assertEquals(42, $updatedApiKey['maxHitsPerQuery']);
}

TestHelper::getClient()->deleteApiKey($res['key'])->wait();

Expand All @@ -303,34 +286,16 @@ public function testApiKeys()
$this->assertInstanceOf('Algolia\AlgoliaSearch\Exceptions\NotFoundException', $e);
}

$retry = 1;
$time = 100000;
$maxRetries = 100;
do {
try {
TestHelper::getClient()->restoreApiKey($res['key'])->wait();
} catch (NotFoundException $e) {
$retry++;
$factor = ceil($retry / 10);
usleep($factor * $time); // 0.1 second
continue;
}
TestHelper::retry(function () use ($res) {
TestHelper::getClient()->restoreApiKey($res['key'])->wait();
});

} while ($retry >= $maxRetries);

do {
try {
$restoredApiKey = TestHelper::getClient()->getApiKey($res['key']);
$this->assertEquals($acl, $restoredApiKey['acl']);
$this->assertEquals($params['description'], $restoredApiKey['description']);
} catch (NotFoundException $e) {
$retry++;
$factor = ceil($retry / 10);
usleep($factor * $time); // 0.1 second
continue;
}
$restoredApiKey = TestHelper::retry(function () use ($res) {
return TestHelper::getClient()->getApiKey($res['key']);
});

} while ($retry >= $maxRetries);
$this->assertEquals($acl, $restoredApiKey['acl']);
$this->assertEquals($params['description'], $restoredApiKey['description']);

TestHelper::getClient()->deleteApiKey($res['key'])->wait();
}
Expand Down
5 changes: 4 additions & 1 deletion tests/Integration/SecuredApiKeysTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ public function testSecuredApiKeys()
/** @var SearchIndex $securedIndexDev */
$securedIndexDev = $securedClient->initIndex($this->indexes['secured_api_keys_dev']);

$res = $securedIndex->search('');
$res = TestHelper::retry(function () use ($securedIndex) {
return $securedIndex->search('');
});

$this->assertCount(1, $res['hits']);

try {
Expand Down
24 changes: 24 additions & 0 deletions tests/TestHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Algolia\AlgoliaSearch\Tests;

use Algolia\AlgoliaSearch\Config\SearchConfig;
use Algolia\AlgoliaSearch\Exceptions\NotFoundException;
use Algolia\AlgoliaSearch\SearchClient;
use Faker\Factory;

Expand Down Expand Up @@ -107,4 +108,27 @@ public static function formatRule($rule)

return $rule;
}

/**
* @param mixed $function function on which to retry
* @param float $delay delay for the retry, in seconds
* @param int $maxRetries maximum of retries
*
* @return mixed
*
* @throws \Exception
*/
public static function retry($function, $delay = 0.1, $maxRetries = 30)
{
for ($i = 1; $i < $maxRetries; $i++) {
try {
return $function();
} catch (NotFoundException $e) {
usleep($delay * 1000000); // 0.1 second
continue;
}
}

throw new \Exception('reached the maximum number of retries');
}
}

0 comments on commit f226cf8

Please sign in to comment.