-
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
82dbf03
commit 2f59086
Showing
9 changed files
with
166 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<phpunit | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
> | ||
<testsuites> | ||
<testsuite name="LoadBalancer Test Suite"> | ||
<directory>./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
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,16 @@ | ||
<?php | ||
|
||
namespace NBN\LoadBalancer\Chooser; | ||
|
||
/** | ||
* @author Nicolas Bastien <[email protected]> | ||
*/ | ||
interface ChooserInterface | ||
{ | ||
/** | ||
* @param Request $request | ||
* @param array $hosts | ||
* @return Response | ||
*/ | ||
public function handleRequest(Request $request, array $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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace NBN\LoadBalancer\Exception; | ||
|
||
/** | ||
* @author Nicolas Bastien <[email protected]> | ||
*/ | ||
class NoRegisteredHostException extends \RuntimeException | ||
{ | ||
} |
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,22 @@ | ||
<?php | ||
|
||
namespace NBN\LoadBalancer\Host; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
/** | ||
* @author Nicolas Bastien <[email protected]> | ||
*/ | ||
interface HostInterface | ||
{ | ||
/** | ||
* @return float | ||
*/ | ||
public function getLoad(); | ||
|
||
/** | ||
* @return Response | ||
*/ | ||
public function handleRequest(Request $request); | ||
} |
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,53 @@ | ||
<?php | ||
|
||
namespace NBN\LoadBalancer; | ||
|
||
use NBN\LoadBalancer\Chooser\ChooserInterface; | ||
use NBN\LoadBalancer\Exception\NoAvailableHostException; | ||
use NBN\LoadBalancer\Exception\NoRegisterdHostException; | ||
use NBN\LoadBalancer\Exception\NoRegisteredHostException; | ||
use NBN\LoadBalancer\Host\HostInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
/** | ||
* @author Nicolas Bastien <[email protected]> | ||
*/ | ||
class LoadBalancer implements LoadBalancerInterface | ||
{ | ||
/** | ||
* @var array|HostInterface[] | ||
*/ | ||
protected $hosts; | ||
|
||
/** | ||
* @var ChooserInterface | ||
*/ | ||
protected $chooser; | ||
|
||
/** | ||
* @param array|HostInterface[] $hosts | ||
* @param ChooserInterface $chooser | ||
*/ | ||
public function __construct(array $hosts, ChooserInterface $chooser) | ||
{ | ||
$this->hosts = $hosts; | ||
$this->chooser = $chooser; | ||
} | ||
|
||
public function addHost(HostInterface $host) | ||
{ | ||
$this->hosts[] = $host; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function handleRequest(Request $request) | ||
{ | ||
if (count($this->hosts) == 0) { | ||
throw new NoRegisteredHostException(); | ||
} | ||
|
||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
namespace NBN\LoadBalancer; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
/** | ||
* @author Nicolas Bastien <[email protected]> | ||
*/ | ||
interface LoadBalancerInterface | ||
{ | ||
/** | ||
* Load balance the request according to loadbalancer configuration | ||
* | ||
* @return Response | ||
*/ | ||
public function handleRequest(Request $request); | ||
} |
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,27 @@ | ||
<?php | ||
|
||
namespace NBN\LoadBalancer; | ||
|
||
use NBN\LoadBalancer\Exception\NoRegisteredHostException; | ||
|
||
/** | ||
* vendor/bin/phpunit tests/Unit/LoadBalancerTest.php | ||
* | ||
* @author Nicolas Bastien <[email protected]> | ||
*/ | ||
class LoadBalancerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @test LoadBalancer::handleRequest() | ||
*/ | ||
public function testHandleRequestNoHostException() | ||
{ | ||
$chooser = $this->prophesize('NBN\LoadBalancer\Chooser\ChooserInterface'); | ||
$resquest = $this->prophesize('Symfony\Component\HttpFoundation\Request'); | ||
|
||
$loadBalancer = new LoadBalancer([], $chooser->reveal()); | ||
|
||
$this->expectException(NoRegisteredHostException::class); | ||
$loadBalancer->handleRequest($resquest->reveal()); | ||
} | ||
} |