Commit d56401c3 authored by Bas de Nooijer's avatar Bas de Nooijer

Updated loadbalancer plugin unittests

parent f36394a4
...@@ -50,13 +50,22 @@ class LoadbalancerTest extends \PHPUnit_Framework_TestCase ...@@ -50,13 +50,22 @@ class LoadbalancerTest extends \PHPUnit_Framework_TestCase
*/ */
protected $client; protected $client;
protected $serverOptions = array('host' => 'nonexistinghostname');
public function setUp() public function setUp()
{ {
$this->plugin = new Loadbalancer(); $this->plugin = new Loadbalancer();
$this->client = new Client(); $options = array(
'endpoint' => array(
'server1' => array(
'host' => 'host1'
),
'server2' => array(
'host' => 'host2'
),
),
);
$this->client = new Client($options);
$adapter = $this->getMock('Solarium\Core\Client\Adapter\Http'); $adapter = $this->getMock('Solarium\Core\Client\Adapter\Http');
$adapter->expects($this->any()) $adapter->expects($this->any())
->method('execute') ->method('execute')
...@@ -68,19 +77,9 @@ class LoadbalancerTest extends \PHPUnit_Framework_TestCase ...@@ -68,19 +77,9 @@ class LoadbalancerTest extends \PHPUnit_Framework_TestCase
public function testConfigMode() public function testConfigMode()
{ {
$options = array( $options = array(
'server' => array( 'endpoint' => array(
'server1' => array( 'server1' => 10,
'options' => array( 'server2' => 5,
'host' => 'host1'
),
'weight' => 10,
),
'server2' => array(
'options' => array(
'host' => 'host2'
),
'weight' => 5,
),
), ),
'blockedquerytype' => array(Client::QUERY_UPDATE, Client::QUERY_MORELIKETHIS) 'blockedquerytype' => array(Client::QUERY_UPDATE, Client::QUERY_MORELIKETHIS)
); );
...@@ -88,17 +87,8 @@ class LoadbalancerTest extends \PHPUnit_Framework_TestCase ...@@ -88,17 +87,8 @@ class LoadbalancerTest extends \PHPUnit_Framework_TestCase
$this->plugin->setOptions($options); $this->plugin->setOptions($options);
$this->assertEquals( $this->assertEquals(
array( array('server1' => 10, 'server2' => 5),
'server1' => array( $this->plugin->getEndpoints()
'options' => array('host' => 'host1'),
'weight' => 10,
),
'server2' => array(
'options' => array('host' => 'host2'),
'weight' => 5,
)
),
$this->plugin->getServers()
); );
$this->assertEquals( $this->assertEquals(
...@@ -119,124 +109,139 @@ class LoadbalancerTest extends \PHPUnit_Framework_TestCase ...@@ -119,124 +109,139 @@ class LoadbalancerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(16, $this->plugin->getFailoverMaxRetries()); $this->assertEquals(16, $this->plugin->getFailoverMaxRetries());
} }
public function testAddServer() public function testAddEndpoint()
{ {
$this->plugin->addServer('s1', $this->serverOptions, 10); $this->plugin->addEndpoint('s1', 10);
$this->assertEquals( $this->assertEquals(
array('s1' => array('s1' => 10),
array( $this->plugin->getEndpoints()
'options' => $this->serverOptions,
'weight' => 10,
)
),
$this->plugin->getServers()
); );
} }
public function testAddServerWithDuplicateKey() public function testAddEndpointWithObject()
{ {
$this->plugin->addServer('s1', $this->serverOptions, 10); $this->plugin->addEndpoint($this->client->getEndpoint('server1'), 10);
$this->setExpectedException('Solarium\Core\Exception'); $this->assertEquals(
$this->plugin->addServer('s1', $this->serverOptions, 20); array('server1' => 10),
$this->plugin->getEndpoints()
);
} }
public function testGetServer() public function testAddEndpoints()
{ {
$this->plugin->addServer('s1', $this->serverOptions, 10); $endpoints = array('s1' => 10, 's2' => 8);
$this->plugin->addEndpoints($endpoints);
$this->assertEquals( $this->assertEquals(
array('options' => $this->serverOptions, 'weight' => 10), $endpoints,
$this->plugin->getServer('s1') $this->plugin->getEndpoints()
); );
} }
public function testGetInvalidServer() public function testAddEndpointWithDuplicateKey()
{ {
$this->plugin->addServer('s1', $this->serverOptions, 10); $this->plugin->addEndpoint('s1', 10);
$this->setExpectedException('Solarium\Core\Exception'); $this->setExpectedException('Solarium\Core\Exception');
$this->plugin->getServer('s2'); $this->plugin->addEndpoint('s1', 20);
} }
public function testClearServers() public function testClearEndpoints()
{ {
$this->plugin->addServer('s1', $this->serverOptions, 10); $this->plugin->addEndpoint('s1', 10);
$this->plugin->clearServers(); $this->plugin->clearEndpoints();
$this->assertEquals(array(), $this->plugin->getServers()); $this->assertEquals(array(), $this->plugin->getEndpoints());
} }
public function testAddServers() public function testRemoveEndpoint()
{ {
$servers = array( $endpoints = array(
's1' => array('options' => $this->serverOptions, 'weight' => 10), 's1' => 10,
's2' => array('options' => $this->serverOptions, 'weight' => 20), 's2' => 20,
); );
$this->plugin->addServers($servers); $this->plugin->addEndpoints($endpoints);
$this->assertEquals($servers, $this->plugin->getServers()); $this->plugin->removeEndpoint('s1');
$this->assertEquals(
array('s2' => 20),
$this->plugin->getEndpoints()
);
} }
public function testRemoveServer() public function testRemoveEndpointWithObject()
{ {
$servers = array( $endpoints = array(
's1' => array('options' => $this->serverOptions, 'weight' => 10), 'server1' => 10,
's2' => array('options' => $this->serverOptions, 'weight' => 20), 'server2' => 20,
); );
$this->plugin->addServers($servers); $this->plugin->addEndpoints($endpoints);
$this->plugin->removeServer('s1'); $this->plugin->removeEndpoint($this->client->getEndpoint('server1'));
$this->assertEquals( $this->assertEquals(
array('s2' => array('options' => $this->serverOptions, 'weight' => 20)), array('server2' => 20),
$this->plugin->getServers() $this->plugin->getEndpoints()
); );
} }
public function testSetServers() public function testSetEndpoints()
{ {
$servers1 = array( $endpoints1 = array(
's1' => array('options' => $this->serverOptions, 'weight' => 10), 's1' => 10,
's2' => array('options' => $this->serverOptions, 'weight' => 20), 's2' => 20,
); );
$servers2 = array( $endpoints2 = array(
's3' => array('options' => $this->serverOptions, 'weight' => 50), 's3' => 50,
's4' => array('options' => $this->serverOptions, 'weight' => 30), 's4' => 30,
); );
$this->plugin->addServers($servers1); $this->plugin->addEndpoints($endpoints1);
$this->plugin->setServers($servers2); $this->plugin->setEndpoints($endpoints2);
$this->assertEquals( $this->assertEquals(
$servers2, $endpoints2,
$this->plugin->getServers() $this->plugin->getEndpoints()
); );
} }
public function testSetAndGetForcedServerForNextQuery() public function testSetAndGetForcedEndpointForNextQuery()
{ {
$servers1 = array( $endpoints1 = array(
's1' => array('options' => $this->serverOptions, 'weight' => 10), 's1' => 10,
's2' => array('options' => $this->serverOptions, 'weight' => 20), 's2' => 20,
); );
$this->plugin->addServers($servers1); $this->plugin->addEndpoints($endpoints1);
$this->plugin->setForcedServerForNextQuery('s2'); $this->plugin->setForcedEndpointForNextQuery('s2');
$this->assertEquals('s2', $this->plugin->getForcedServerForNextQuery()); $this->assertEquals('s2', $this->plugin->getForcedEndpointForNextQuery());
} }
public function testSetForcedServerForNextQueryWithInvalidKey() public function testSetAndGetForcedEndpointForNextQueryWithObject()
{ {
$servers1 = array( $endpoints1 = array(
's1' => array('options' => $this->serverOptions, 'weight' => 10), 'server1' => 10,
's2' => array('options' => $this->serverOptions, 'weight' => 20), 'server2' => 20,
); );
$this->plugin->addServers($servers1); $this->plugin->addEndpoints($endpoints1);
$this->plugin->setForcedEndpointForNextQuery($this->client->getEndpoint('server2'));
$this->assertEquals('server2', $this->plugin->getForcedEndpointForNextQuery());
}
public function testSetForcedEndpointForNextQueryWithInvalidKey()
{
$endpoints1 = array(
's1' => 10,
's2' => 20,
);
$this->plugin->addEndpoints($endpoints1);
$this->setExpectedException('Solarium\Core\Exception'); $this->setExpectedException('Solarium\Core\Exception');
$this->plugin->setForcedServerForNextQuery('s3'); $this->plugin->setForcedEndpointForNextQuery('s3');
} }
public function testAddBlockedQueryType() public function testAddBlockedQueryType()
...@@ -293,45 +298,45 @@ class LoadbalancerTest extends \PHPUnit_Framework_TestCase ...@@ -293,45 +298,45 @@ class LoadbalancerTest extends \PHPUnit_Framework_TestCase
); );
} }
public function testPreExecuteRequestWithForcedServer() public function testPreExecuteRequestWithForcedEndpoint()
{ {
$servers = array( $endpoints = array(
's1' => array('options' => $this->serverOptions, 'weight' => 100), 'server1' => 100,
's2' => array('options' => $this->serverOptions, 'weight' => 1), 'server2' => 1,
); );
$query = new SelectQuery(); $query = new SelectQuery();
$request = new Request(); $request = new Request();
$this->plugin->setServers($servers); $this->plugin->setEndpoints($endpoints);
$this->plugin->setForcedServerForNextQuery('s2'); $this->plugin->setForcedEndpointForNextQuery('server2');
$this->plugin->preCreateRequest($query); $this->plugin->preCreateRequest($query);
$this->plugin->preExecuteRequest($request); $this->plugin->preExecuteRequest($request);
$this->assertEquals( $this->assertEquals(
's2', 'server2',
$this->plugin->getLastServerKey() $this->plugin->getLastEndpoint()
); );
} }
public function testAdapterPresetRestore() public function testDefaultEndpointRestore()
{ {
$originalHost = $this->client->getAdapter()->getHost(); $originalHost = $this->client->getEndpoint()->getHost();
$servers = array( $endpoints = array(
's1' => array('options' => $this->serverOptions, 'weight' => 100), 'server1' => 100,
's2' => array('options' => $this->serverOptions, 'weight' => 1), 'server2' => 1,
); );
$request = new Request(); $request = new Request();
$this->plugin->setServers($servers); $this->plugin->setEndpoints($endpoints);
$this->plugin->setForcedServerForNextQuery('s2'); $this->plugin->setForcedEndpointForNextQuery('server2');
$query = new SelectQuery(); $query = new SelectQuery();
$this->plugin->preCreateRequest($query); $this->plugin->preCreateRequest($query);
$this->plugin->preExecuteRequest($request); $this->plugin->preExecuteRequest($request);
$this->assertEquals( $this->assertEquals(
's2', 'server2',
$this->plugin->getLastServerKey() $this->plugin->getLastEndpoint()
); );
$query = new SelectQuery(); // this is a blocked querytype that should trigger a restore $query = new SelectQuery(); // this is a blocked querytype that should trigger a restore
...@@ -340,18 +345,18 @@ class LoadbalancerTest extends \PHPUnit_Framework_TestCase ...@@ -340,18 +345,18 @@ class LoadbalancerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals( $this->assertEquals(
$originalHost, $originalHost,
$this->client->getAdapter()->getHost() $this->client->getEndpoint()->getHost()
); );
} }
public function testBlockedQueryTypeNotLoadbalanced() public function testBlockedQueryTypeNotLoadbalanced()
{ {
$originalHost = $this->client->getAdapter()->getHost(); $originalHost = $this->client->getEndpoint()->getHost();
$servers = array( $endpoints = array(
's1' => array('options' => $this->serverOptions, 'weight' => 100), 'server1' => 100,
's2' => array('options' => $this->serverOptions, 'weight' => 1), 'server2' => 1,
); );
$this->plugin->setServers($servers); $this->plugin->setEndpoints($endpoints);
$request = new Request(); $request = new Request();
$query = new UpdateQuery(); // this is a blocked querytype that should not be loadbalanced $query = new UpdateQuery(); // this is a blocked querytype that should not be loadbalanced
...@@ -360,22 +365,22 @@ class LoadbalancerTest extends \PHPUnit_Framework_TestCase ...@@ -360,22 +365,22 @@ class LoadbalancerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals( $this->assertEquals(
$originalHost, $originalHost,
$this->client->getAdapter()->getHost() $this->client->getEndpoint()->getHost()
); );
$this->assertEquals( $this->assertEquals(
null, null,
$this->plugin->getLastServerKey() $this->plugin->getLastEndpoint()
); );
} }
public function testLoadbalancerRandomizing() public function testLoadbalancerRandomizing()
{ {
$servers = array( $endpoints = array(
's1' => array('options' => $this->serverOptions, 'weight' => 1), 'server1' => 1,
's2' => array('options' => $this->serverOptions, 'weight' => 1), 'server2' => 1,
); );
$this->plugin->setServers($servers); $this->plugin->setEndpoints($endpoints);
$request = new Request(); $request = new Request();
$query = new SelectQuery(); // $query = new SelectQuery(); //
...@@ -383,23 +388,22 @@ class LoadbalancerTest extends \PHPUnit_Framework_TestCase ...@@ -383,23 +388,22 @@ class LoadbalancerTest extends \PHPUnit_Framework_TestCase
$this->plugin->preExecuteRequest($request); $this->plugin->preExecuteRequest($request);
$this->assertTrue( $this->assertTrue(
in_array($this->plugin->getLastServerKey(), array('s1','s2')) in_array($this->plugin->getLastEndpoint(), array('server1','server2'))
); );
} }
public function testFailover() public function testFailover()
{ {
$this->plugin = new TestLoadbalancer(); // special loadbalancer that returns servers in fixed order $this->plugin = new TestLoadbalancer(); // special loadbalancer that returns endpoints in fixed order
$this->client = new Client();
$this->client->setAdapter(new TestAdapterForFailover()); // set special mock that fails once $this->client->setAdapter(new TestAdapterForFailover()); // set special mock that fails once
$this->plugin->initPlugin($this->client, array()); $this->plugin->initPlugin($this->client, array());
$request = new Request(); $request = new Request();
$servers = array( $endpoints = array(
's1' => array('options' => $this->serverOptions, 'weight' => 1), 'server1' => 1,
's2' => array('options' => $this->serverOptions, 'weight' => 1), 'server2' => 1,
); );
$this->plugin->setServers($servers); $this->plugin->setEndpoints($endpoints);
$this->plugin->setFailoverEnabled(true); $this->plugin->setFailoverEnabled(true);
$query = new SelectQuery(); $query = new SelectQuery();
...@@ -407,26 +411,26 @@ class LoadbalancerTest extends \PHPUnit_Framework_TestCase ...@@ -407,26 +411,26 @@ class LoadbalancerTest extends \PHPUnit_Framework_TestCase
$this->plugin->preExecuteRequest($request); $this->plugin->preExecuteRequest($request);
$this->assertEquals( $this->assertEquals(
's2', 'server2',
$this->plugin->getLastServerKey() $this->plugin->getLastEndpoint()
); );
} }
public function testFailoverMaxRetries() public function testFailoverMaxRetries()
{ {
$this->plugin = new TestLoadbalancer(); // special loadbalancer that returns servers in fixed order $this->plugin = new TestLoadbalancer(); // special loadbalancer that returns endpoints in fixed order
$this->client = new Client();
$adapter = new TestAdapterForFailover(); $adapter = new TestAdapterForFailover();
$adapter->setFailCount(10); $adapter->setFailCount(10);
$this->client->setAdapter($adapter); // set special mock that fails for all servers $this->client->setAdapter($adapter); // set special mock that fails for all endpoints
$this->plugin->initPlugin($this->client, array()); $this->plugin->initPlugin($this->client, array());
$request = new Request(); $request = new Request();
$servers = array( $endpoints = array(
's1' => array('options' => $this->serverOptions, 'weight' => 1), 'server1' => 1,
's2' => array('options' => $this->serverOptions, 'weight' => 1), 'server2' => 1,
); );
$this->plugin->setServers($servers); $this->plugin->setEndpoints($endpoints);
$this->plugin->setFailoverEnabled(true); $this->plugin->setFailoverEnabled(true);
$query = new SelectQuery(); $query = new SelectQuery();
...@@ -446,18 +450,18 @@ class TestLoadbalancer extends Loadbalancer{ ...@@ -446,18 +450,18 @@ class TestLoadbalancer extends Loadbalancer{
protected $counter = 0; protected $counter = 0;
/** /**
* Get options array for a randomized server * Get options array for a randomized endpoint
* *
* @return array * @return array
*/ */
protected function getRandomServerOptions() protected function getRandomEndpoint()
{ {
$this->counter++; $this->counter++;
$serverKey = 's'.$this->counter; $endpointKey = 'server'.$this->counter;
$this->serverExcludes[] = $serverKey; $this->endpointExcludes[] = $endpointKey;
$this->lastServerKey = $serverKey; $this->lastEndpoint = $endpointKey;
return $this->servers[$serverKey]['options']; return $this->client->getEndpoint($endpointKey);
} }
} }
...@@ -473,7 +477,7 @@ class TestAdapterForFailover extends HttpAdapter{ ...@@ -473,7 +477,7 @@ class TestAdapterForFailover extends HttpAdapter{
$this->failCount = $count; $this->failCount = $count;
} }
public function execute($request) public function execute($request, $endpoint)
{ {
$this->counter++; $this->counter++;
if($this->counter <= $this->failCount) { if($this->counter <= $this->failCount) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment