Skip to content

Commit

Permalink
#2: LoadBalancer NoAvailableHostException
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Bastien committed Jul 10, 2016
1 parent 2f59086 commit dbbfc99
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Chooser/ChooserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace NBN\LoadBalancer\Chooser;

use Symfony\Component\HttpFoundation\Request;

/**
* @author Nicolas Bastien <[email protected]>
*/
Expand All @@ -12,5 +14,5 @@ interface ChooserInterface
* @param array $hosts
* @return Response
*/
public function handleRequest(Request $request, array $hosts);
public function getAvailableHost(Request $request, array $hosts);
}
10 changes: 10 additions & 0 deletions src/Exception/NoAvailableHostException.php
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 NoAvailableHostException extends \RuntimeException
{
}
4 changes: 4 additions & 0 deletions src/LoadBalancer.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,9 @@ public function handleRequest(Request $request)
throw new NoRegisteredHostException();
}

$host = $this->chooser->getAvailableHost($request, $this->hosts);
if ($host == null) {
throw new NoAvailableHostException();
}
}
}
16 changes: 16 additions & 0 deletions tests/Unit/LoadBalancerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace NBN\LoadBalancer;

use NBN\LoadBalancer\Exception\NoAvailableHostException;
use NBN\LoadBalancer\Exception\NoRegisteredHostException;

/**
Expand All @@ -24,4 +25,19 @@ public function testHandleRequestNoHostException()
$this->expectException(NoRegisteredHostException::class);
$loadBalancer->handleRequest($resquest->reveal());
}

/**
* @test LoadBalancer::handleRequest()
*/
public function testHandleRequestNoAvailableException()
{
$chooser = $this->prophesize('NBN\LoadBalancer\Chooser\ChooserInterface');
$host = $this->prophesize('NBN\LoadBalancer\Host\HostInterface');
$resquest = $this->prophesize('Symfony\Component\HttpFoundation\Request');

$loadBalancer = new LoadBalancer([$host], $chooser->reveal());

$this->expectException(NoAvailableHostException::class);
$loadBalancer->handleRequest($resquest->reveal());
}
}

0 comments on commit dbbfc99

Please sign in to comment.