-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nicolas Bastien
committed
Jul 10, 2016
1 parent
bc50368
commit 5d4ec20
Showing
4 changed files
with
89 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace NBN\LoadBalancer\Chooser; | ||
|
||
use NBN\LoadBalancer\Exception\NoRegisteredHostException; | ||
use NBN\LoadBalancer\Host\HostInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* @author Nicolas Bastien <[email protected]> | ||
*/ | ||
class RotationChooser implements ChooserInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getAvailableHost(Request $request, array $hosts) | ||
{ | ||
if (count($hosts) == 0) { | ||
throw new NoRegisteredHostException(); | ||
} | ||
|
||
//basic random | ||
return $hosts[array_rand($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
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,56 @@ | ||
<?php | ||
|
||
namespace NBN\LoadBalancer\Chooser; | ||
|
||
use NBN\LoadBalancer\Exception\NoRegisteredHostException; | ||
use NBN\LoadBalancer\Host\HostInterface; | ||
|
||
/** | ||
* vendor/bin/phpunit tests/Unit/Chooser/RotationChooserTest.php | ||
* | ||
* @author Nicolas Bastien <[email protected]> | ||
*/ | ||
class RotationChooserTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @test RotationChooser::getAvailableHost() | ||
*/ | ||
public function testGetAvailableHostNoRegisteredHostException() | ||
{ | ||
$request = $this->prophesize('Symfony\Component\HttpFoundation\Request'); | ||
$chooser = new RotationChooser(); | ||
|
||
$this->expectException(NoRegisteredHostException::class); | ||
$chooser->getAvailableHost($request->reveal(), []); | ||
} | ||
|
||
/** | ||
* @test RotationChooser::getAvailableHost() | ||
*/ | ||
public function testGetAvailableHost() | ||
{ | ||
$request = $this->prophesize('Symfony\Component\HttpFoundation\Request')->reveal(); | ||
|
||
$host1 = $this->prophesize('NBN\LoadBalancer\Host\HostInterface'); | ||
$host1->getId()->willReturn('host-1'); | ||
$host1 = $host1->reveal(); | ||
$host2 = $this->prophesize('NBN\LoadBalancer\Host\HostInterface'); | ||
$host2->getId()->willReturn('host-2'); | ||
$host2 = $host2->reveal(); | ||
$host3 = $this->prophesize('NBN\LoadBalancer\Host\HostInterface'); | ||
$host3->getId()->willReturn('host-3'); | ||
$host3 = $host3->reveal(); | ||
|
||
$chooser = new RotationChooser(); | ||
$results = []; | ||
|
||
for ($i=0; $i<10; $i++) { | ||
$host = $chooser->getAvailableHost($request, [$host1, $host2, $host3]); | ||
$this->assertTrue($host instanceof HostInterface); | ||
$results[$host->getId()] = $host; | ||
} | ||
|
||
//Verify there is more than one result | ||
$this->assertTrue(count($results) > 1); | ||
} | ||
} |