-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from algolia/feature/fallback-strategy-compliance
Randomize the fallback hosts on a per client instance basis.
- Loading branch information
Showing
6 changed files
with
122 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
namespace AlgoliaSearch\Tests; | ||
|
||
use AlgoliaSearch\ClientContext; | ||
|
||
class ClientContextTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testRandomReadFallbackHosts() | ||
{ | ||
$context = new ClientContext('whatever', 'whatever', null); | ||
$hosts = $context->readHostsArray; | ||
|
||
// Here we check that different calls results in the hosts being in and different order. | ||
$isRandom = false; | ||
for ($i = 0; $i < 100; $i++) { | ||
$otherContext = new ClientContext('whatever', 'whatever', null); | ||
if ($hosts !== $otherContext->readHostsArray) { | ||
$isRandom = true; | ||
break; | ||
} | ||
} | ||
|
||
$this->assertTrue($isRandom); | ||
|
||
// Check that the first entry is the correct API endpoint. | ||
$this->assertEquals('whatever-dsn.algolia.net', $hosts[0]); | ||
|
||
// As hosts are in a random order, we sort everything to be sure the correct hosts are present. | ||
sort($hosts, SORT_STRING); | ||
$expectedHosts = [ | ||
'whatever-1.algolianet.com', | ||
'whatever-2.algolianet.com', | ||
'whatever-3.algolianet.com', | ||
'whatever-dsn.algolia.net', | ||
]; | ||
$this->assertEquals($expectedHosts, $hosts); | ||
} | ||
|
||
public function testRandomReadPlacesFallbackHosts() | ||
{ | ||
$context = new ClientContext('whatever', 'whatever', null, true); | ||
$hosts = $context->readHostsArray; | ||
|
||
// Here we check that different calls results in the hosts being in and different order. | ||
$isRandom = false; | ||
for ($i = 0; $i < 100; $i++) { | ||
$otherContext = new ClientContext('whatever', 'whatever', null); | ||
if ($hosts !== $otherContext->readHostsArray) { | ||
$isRandom = true; | ||
break; | ||
} | ||
} | ||
|
||
$this->assertTrue($isRandom); | ||
|
||
// Check that the first entry is the correct places API endpoint. | ||
$this->assertEquals('places-dsn.algolia.net', $hosts[0]); | ||
|
||
// As hosts are in a random order, we sort everything to be sure the correct hosts are present. | ||
sort($hosts, SORT_STRING); | ||
$expectedHosts = [ | ||
'places-1.algolianet.com', | ||
'places-2.algolianet.com', | ||
'places-3.algolianet.com', | ||
'places-dsn.algolia.net', | ||
]; | ||
$this->assertEquals($expectedHosts, $hosts); | ||
} | ||
|
||
public function testRandomWriteFallbackHosts() | ||
{ | ||
$context = new ClientContext('whatever', 'whatever', null); | ||
$hosts = $context->writeHostsArray; | ||
|
||
// Here we check that different calls results in the hosts being in and different order. | ||
$isRandom = false; | ||
for ($i = 0; $i < 100; $i++) { | ||
$otherContext = new ClientContext('whatever', 'whatever', null); | ||
if ($hosts !== $otherContext->writeHostsArray) { | ||
$isRandom = true; | ||
break; | ||
} | ||
} | ||
|
||
$this->assertTrue($isRandom); | ||
|
||
// Check that the first entry is the correct API endpoint. | ||
$this->assertEquals('whatever.algolia.net', $hosts[0]); | ||
|
||
// As hosts are in a random order, we sort everything to be sure the correct hosts are present. | ||
sort($hosts, SORT_STRING); | ||
$expectedHosts = [ | ||
'whatever-1.algolianet.com', | ||
'whatever-2.algolianet.com', | ||
'whatever-3.algolianet.com', | ||
'whatever.algolia.net', | ||
]; | ||
$this->assertEquals($expectedHosts, $hosts); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters