Skip to content

Commit

Permalink
#3: RotationChooser
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Bastien committed Jul 10, 2016
1 parent bc50368 commit 5d4ec20
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
26 changes: 26 additions & 0 deletions src/Chooser/RotationChooser.php
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)];
}
}
5 changes: 5 additions & 0 deletions src/Host/HostInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
*/
interface HostInterface
{
/**
* @return string
*/
public function getId();

/**
* @return float
*/
Expand Down
4 changes: 2 additions & 2 deletions src/LoadBalancer.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public function handleRequest(Request $request)
throw new HostRequestException();
}

$response->headers->set('Handled-By', $host->getId());



return $response;
}
}
56 changes: 56 additions & 0 deletions tests/Unit/Chooser/RotationChooserTest.php
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);
}
}

0 comments on commit 5d4ec20

Please sign in to comment.