Skip to content

Commit 6724083

Browse files
committed
chore(CTS): fix flakiness
1 parent 00aad33 commit 6724083

6 files changed

+50
-53
lines changed

src/SearchClient.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public function isAlive($requestOptions = array())
155155

156156
public function multipleQueries($queries, $requestOptions = array())
157157
{
158-
$queries = array_map(function($query) {
158+
$queries = array_map(function ($query) {
159159
$query['params'] = isset($query['params']) ?
160160
Helpers::serializeQueryParameters($query['params']) :
161161
Helpers::serializeQueryParameters(array());

tests/Integration/AnalyticsClientTest.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ public function testAbTesting()
6161
$cpt++;
6262
} while (false);
6363

64-
$response = $analyticsClient->addABTest($abTest);
64+
$response = TestHelper::retry(function () use ($analyticsClient, $abTest) {
65+
return $analyticsClient->addABTest($abTest);
66+
}, 0.1, 40);
67+
6568
$abTestId = $response['abTestID'];
6669
$index->waitTask($response['taskID']);
6770

@@ -160,7 +163,10 @@ public function testAaTesting()
160163
$cpt++;
161164
} while (false);
162165

163-
$response = $analyticsClient->addABTest($aaTest);
166+
$response = TestHelper::retry(function () use ($analyticsClient, $aaTest) {
167+
return $analyticsClient->addABTest($aaTest);
168+
}, 0.1, 40);
169+
164170
$aaTestId = $response['abTestID'];
165171
TestHelper::getClient()->waitTask($this->indexes['aa_testing'], $response['taskID']);
166172

tests/Integration/BaseTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public static function setUpBeforeClass()
1515
TestHelper::checkEnvironmentVariables();
1616
} catch (\Exception $e) {
1717
echo $e->getMessage()."\n";
18+
1819
return;
1920
}
2021

tests/Integration/SearchClientTest.php

+14-49
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Algolia\AlgoliaSearch\Tests\Integration;
44

5-
use Algolia\AlgoliaSearch\Exceptions\NotFoundException;
65
use Algolia\AlgoliaSearch\Response\MultiResponse;
76
use Algolia\AlgoliaSearch\SearchClient;
87
use Algolia\AlgoliaSearch\SearchIndex;
@@ -271,29 +270,13 @@ public function testApiKeys()
271270

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

274-
$retry = 1;
275-
$time = 100000;
276-
$maxRetries = 100;
277-
do {
278-
if ($retry >= $maxRetries) {
279-
break;
280-
}
281-
282-
try {
283-
$updatedApiKey = TestHelper::getClient()->getApiKey($res['key']);
284-
285-
if ($updatedApiKey['maxHitsPerQuery'] !== $apiKey['maxHitsPerQuery']) {
286-
$this->assertEquals(42, $updatedApiKey['maxHitsPerQuery']);
287-
break;
288-
}
289-
} catch (NotFoundException $e) {
290-
// Try again
291-
}
273+
$updatedApiKey = TestHelper::retry(function () use ($res) {
274+
return TestHelper::getClient()->getApiKey($res['key']);
275+
});
292276

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

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

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

306-
$retry = 1;
307-
$time = 100000;
308-
$maxRetries = 100;
309-
do {
310-
try {
311-
TestHelper::getClient()->restoreApiKey($res['key'])->wait();
312-
} catch (NotFoundException $e) {
313-
$retry++;
314-
$factor = ceil($retry / 10);
315-
usleep($factor * $time); // 0.1 second
316-
continue;
317-
}
289+
TestHelper::retry(function () use ($res) {
290+
TestHelper::getClient()->restoreApiKey($res['key'])->wait();
291+
});
318292

319-
} while ($retry >= $maxRetries);
320-
321-
do {
322-
try {
323-
$restoredApiKey = TestHelper::getClient()->getApiKey($res['key']);
324-
$this->assertEquals($acl, $restoredApiKey['acl']);
325-
$this->assertEquals($params['description'], $restoredApiKey['description']);
326-
} catch (NotFoundException $e) {
327-
$retry++;
328-
$factor = ceil($retry / 10);
329-
usleep($factor * $time); // 0.1 second
330-
continue;
331-
}
293+
$restoredApiKey = TestHelper::retry(function () use ($res) {
294+
return TestHelper::getClient()->getApiKey($res['key']);
295+
});
332296

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

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

tests/Integration/SecuredApiKeysTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ public function testSecuredApiKeys()
5252
/** @var SearchIndex $securedIndexDev */
5353
$securedIndexDev = $securedClient->initIndex($this->indexes['secured_api_keys_dev']);
5454

55-
$res = $securedIndex->search('');
55+
$res = TestHelper::retry(function () use ($securedIndex) {
56+
return $securedIndex->search('');
57+
});
58+
5659
$this->assertCount(1, $res['hits']);
5760

5861
try {

tests/TestHelper.php

+22
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Algolia\AlgoliaSearch\Tests;
44

55
use Algolia\AlgoliaSearch\Config\SearchConfig;
6+
use Algolia\AlgoliaSearch\Exceptions\NotFoundException;
67
use Algolia\AlgoliaSearch\SearchClient;
78
use Faker\Factory;
89

@@ -107,4 +108,25 @@ public static function formatRule($rule)
107108

108109
return $rule;
109110
}
111+
112+
/**
113+
* @param mixed $function function on which to retry
114+
* @param float $delay delay for the retry, in seconds
115+
* @param int $maxRetries maximum of retries
116+
* @return mixed
117+
* @throws \Exception
118+
*/
119+
public static function retry($function, $delay = 0.1, $maxRetries = 30)
120+
{
121+
for ($i = 1; $i < $maxRetries; $i++) {
122+
try {
123+
return $function();
124+
} catch (NotFoundException $e) {
125+
usleep($delay * $i * 1000000); // 0.1 second
126+
continue;
127+
}
128+
}
129+
130+
throw new \Exception('reached the maximum number of retries');
131+
}
110132
}

0 commit comments

Comments
 (0)