Skip to content

Commit

Permalink
Add host model + loadbalancer add host by configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Bastien committed Jul 12, 2016
1 parent 77108b4 commit c74fde2
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 7 deletions.
10 changes: 10 additions & 0 deletions src/Exception/AlreadyRegisteredHostException.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 AlreadyRegisteredHostException extends \RuntimeException
{
}
98 changes: 98 additions & 0 deletions src/Host/Host.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace NBN\LoadBalancer\Host;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* @author Nicolas Bastien <[email protected]>
*/
class Host implements HostInterface
{
/**
* @var string
*/
protected $id;

/**
* @var string
*/
protected $url;

/**
* @var array
*/
protected $settings;

/**
* @param string $id
* @param string $url
* @param array $settings
*/
public function __construct($id, $url, array $settings)
{
$this->id = $id;
$this->url = $url;
$this->settings = $settings;
}

/**
* {@inheritdoc}
*/
public function getId()
{
return $this->id;
}

/**
* {@inheritdoc}
*/
public function getUrl()
{
return $this->url;
}

/**
* {@inheritdoc}
*/
public function getSettings()
{
return $this->settings;
}

/**
* @param string $name Setting name
* @param mixed $default Default value is setting is not set
* @return mixed
*/
public function getSetting($name, $default = null)
{
if (isset($this->settings[$name])) {
return $this->settings[$name];
}

return $default;
}

/**
* {@inheritdoc}
*/
public function getLoad()
{
//if load is not defined presume it's overloaded
return $this->getSetting('load', 1);
}

/**
* {@inheritdoc}
*/
public function handleRequest(Request $request)
{
$response = new Response();
$response->setStatusCode($this->getSetting('response-status', 200));
$response->setContent(sprintf($this->getSetting('response-content-format', '%s'), $request->getUri()));

return $response;
}
}
30 changes: 28 additions & 2 deletions src/LoadBalancer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace NBN\LoadBalancer;

use NBN\LoadBalancer\Chooser\ChooserInterface;
use NBN\LoadBalancer\Exception\AlreadyRegisteredHostException;
use NBN\LoadBalancer\Exception\HostRequestException;
use NBN\LoadBalancer\Exception\NoAvailableHostException;
use NBN\LoadBalancer\Exception\NoRegisteredHostException;
use NBN\LoadBalancer\Host\Host;
use NBN\LoadBalancer\Host\HostInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -31,13 +33,37 @@ class LoadBalancer implements LoadBalancerInterface
*/
public function __construct(array $hosts, ChooserInterface $chooser)
{
$this->hosts = $hosts;
foreach ($hosts as $host) {
$this->hosts[$host->getId()] = $host;
}

$this->chooser = $chooser;
}

/**
* @param HostInterface $host
*/
public function addHost(HostInterface $host)
{
$this->hosts[] = $host;
if (isset($this->hosts[$host->getId()])) {
throw new AlreadyRegisteredHostException();
}

$this->hosts[$host->getId()] = $host;
}

/**
* @param string $id
* @param string $url
* @param array $settings
*/
public function addHostByConfiguration($id, $url, $settings)
{
if (isset($this->hosts[$id])) {
throw new AlreadyRegisteredHostException();
}

$this->hosts[$id] = new Host($id, $url, $settings);
}

/**
Expand Down
30 changes: 25 additions & 5 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\AlreadyRegisteredHostException;
use NBN\LoadBalancer\Exception\HostRequestException;
use NBN\LoadBalancer\Exception\NoAvailableHostException;
use NBN\LoadBalancer\Exception\NoRegisteredHostException;
Expand Down Expand Up @@ -34,9 +35,10 @@ public function testHandleRequestNoAvailableException()
{
$chooser = $this->prophesize('NBN\LoadBalancer\Chooser\ChooserInterface');
$host = $this->prophesize('NBN\LoadBalancer\Host\HostInterface');
$host->getId()->willReturn('host-id');
$request = $this->prophesize('Symfony\Component\HttpFoundation\Request');

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

$this->expectException(NoAvailableHostException::class);
$loadBalancer->handleRequest($request->reveal());
Expand All @@ -47,14 +49,32 @@ public function testHandleRequestNoAvailableException()
*/
public function testHandleRequestHostRequestException()
{
$host = $this->prophesize('NBN\LoadBalancer\Host\HostInterface')->reveal();
$request = $this->prophesize('Symfony\Component\HttpFoundation\Request');
$request = $this->prophesize('Symfony\Component\HttpFoundation\Request')->reveal();
$host = $this->prophesize('NBN\LoadBalancer\Host\HostInterface');
$host->getId()->willReturn('host-id');
$host->handleRequest($request)->willReturn(null);
$host = $host->reveal();
$chooser = $this->prophesize('NBN\LoadBalancer\Chooser\ChooserInterface');
$chooser->getAvailableHost($request, [$host])->willReturn($host);
$chooser->getAvailableHost($request, ['host-id' => $host])->willReturn($host);

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

$this->expectException(HostRequestException::class);
$loadBalancer->handleRequest($request->reveal());
$loadBalancer->handleRequest($request);
}

/**
* @test LoadBalancer::addHostByConfiguration()
*/
public function testaddHostByConfigurationAlreadyRegisteredHostException()
{
$host = $this->prophesize('NBN\LoadBalancer\Host\HostInterface');
$host->getId()->willReturn('host-id');
$chooser = $this->prophesize('NBN\LoadBalancer\Chooser\ChooserInterface');

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

$this->expectException(AlreadyRegisteredHostException::class);
$loadBalancer->addHostByConfiguration('host-id', 'url', []);
}
}

0 comments on commit c74fde2

Please sign in to comment.