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

- improved phpunit coverage

- small fixes and improvements to loadbalancer plugin
parent 0a453043
...@@ -26,7 +26,7 @@ for($i=1; $i<=8; $i++) { ...@@ -26,7 +26,7 @@ for($i=1; $i<=8; $i++) {
} }
// force a server for a query (normally solr 3 is extremely unlikely based on it's weight) // force a server for a query (normally solr 3 is extremely unlikely based on it's weight)
$loadbalancer->forceServerForNextQuery('solr3'); $loadbalancer->setForcedServerForNextQuery('solr3');
$resultset = $client->select($query); $resultset = $client->select($query);
echo 'Query execution with server forced to solr3<br/>'; echo 'Query execution with server forced to solr3<br/>';
echo 'NumFound: ' . $resultset->getNumFound(). '<br/>'; echo 'NumFound: ' . $resultset->getNumFound(). '<br/>';
......
...@@ -77,7 +77,7 @@ class Solarium_Plugin_Loadbalancer extends Solarium_Plugin_Abstract ...@@ -77,7 +77,7 @@ class Solarium_Plugin_Loadbalancer extends Solarium_Plugin_Abstract
* @var array * @var array
*/ */
protected $_blockedQueryTypes = array( protected $_blockedQueryTypes = array(
Solarium_Client::QUERYTYPE_UPDATE Solarium_Client::QUERYTYPE_UPDATE => true
); );
/** /**
...@@ -296,21 +296,33 @@ class Solarium_Plugin_Loadbalancer extends Solarium_Plugin_Abstract ...@@ -296,21 +296,33 @@ class Solarium_Plugin_Loadbalancer extends Solarium_Plugin_Abstract
/** /**
* Set a forced server (by key) for the next request * Set a forced server (by key) for the next request
* *
* As soon as one query has used the forced server this setting is reset. * As soon as one query has used the forced server this setting is reset. If you want to remove this setting
* pass NULL as the key value.
* *
* If the next query cannot be loadbalanced (for instance based on the querytype) this setting is ignored * If the next query cannot be loadbalanced (for instance based on the querytype) this setting is ignored
* but will still be reset. * but will still be reset.
* *
* @param string $key * @param string|null $key
* @return self Provides fluent interface * @return self Provides fluent interface
*/ */
public function forceServerForNextQuery($key) public function setForcedServerForNextQuery($key)
{ {
if (!array_key_exists($key, $this->_servers)) { if ($key !== null && !array_key_exists($key, $this->_servers)) {
throw new Solarium_Exception('Unknown server forced for next query'); throw new Solarium_Exception('Unknown server forced for next query');
} }
$this->_nextServer = $key; $this->_nextServer = $key;
return $this;
}
/**
* Get the ForcedServerForNextQuery value
*
* @return string|null
*/
public function getForcedServerForNextQuery()
{
return $this->_nextServer;
} }
/** /**
......
...@@ -85,7 +85,7 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase ...@@ -85,7 +85,7 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase
$plugin = $this->_client->getPlugin('myplugin'); $plugin = $this->_client->getPlugin('myplugin');
$this->assertThat($plugin, $this->isInstanceOf('MyClientPlugin')); $this->assertThat($plugin, $this->isInstanceOf('MyClientPlugin'));
$this->assertEquals($options['plugin']['myplugin']['options'], $plugin->getOptions()); $this->assertEquals($options['plugin']['myplugin']['options'], $plugin->getOptions());
} }
public function testConfigModeWithoutKeys() public function testConfigModeWithoutKeys()
...@@ -151,7 +151,7 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase ...@@ -151,7 +151,7 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase
$this->_client->setAdapter($adapterClass); $this->_client->setAdapter($adapterClass);
$this->assertThat($this->_client->getAdapter(), $this->isInstanceOf($adapterClass)); $this->assertThat($this->_client->getAdapter(), $this->isInstanceOf($adapterClass));
} }
public function testSetAndGetAdapterWithObject() public function testSetAndGetAdapterWithObject()
{ {
$adapterClass = 'MyAdapter'; $adapterClass = 'MyAdapter';
...@@ -205,10 +205,25 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase ...@@ -205,10 +205,25 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase
{ {
$this->assertEquals( $this->assertEquals(
null, null,
$this->_client->getPlugin('invalidplugin') $this->_client->getPlugin('invalidplugin', false)
);
}
public function testAutoloadPlugin()
{
$loadbalancer = $this->_client->getPlugin('loadbalancer');
$this->assertThat(
$loadbalancer,
$this->isInstanceOf('Solarium_Plugin_Loadbalancer')
); );
} }
public function testAutoloadInvalidPlugin()
{
$this->setExpectedException('Solarium_Exception');
$this->_client->getPlugin('invalidpluginname');
}
public function testRemoveAndGetPlugins() public function testRemoveAndGetPlugins()
{ {
$options = array('option1' => 1); $options = array('option1' => 1);
...@@ -469,7 +484,7 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase ...@@ -469,7 +484,7 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase
public function testExecuteWithOverridingPlugin() public function testExecuteWithOverridingPlugin()
{ {
$query = new Solarium_Query_Ping(); $query = new Solarium_Query_Ping();
$observer = $this->getMock('Solarium_Plugin_Abstract', array(), array($this->_client,array())); $observer = $this->getMock('Solarium_Plugin_Abstract', array(), array($this->_client,array()));
$observer->expects($this->once()) $observer->expects($this->once())
->method('preExecute') ->method('preExecute')
...@@ -747,8 +762,8 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase ...@@ -747,8 +762,8 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase
->with($this->equalTo(Solarium_Client::QUERYTYPE_MORELIKETHIS), $this->equalTo($options)); ->with($this->equalTo(Solarium_Client::QUERYTYPE_MORELIKETHIS), $this->equalTo($options));
$observer->createMoreLikeThis($options); $observer->createMoreLikeThis($options);
} }
public function testCreateAnalysisField() public function testCreateAnalysisField()
{ {
$options = array('optionA' => 1, 'optionB' => 2); $options = array('optionA' => 1, 'optionB' => 2);
...@@ -773,6 +788,20 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase ...@@ -773,6 +788,20 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase
$observer->createAnalysisDocument($options); $observer->createAnalysisDocument($options);
} }
public function testTriggerEvent()
{
$eventName = 'Test';
$params = array('a', 'b');
$override = true;
$clientMock = $this->getMock('Solarium_Client', array('_callPlugins'));
$clientMock->expects($this->once())
->method('_callPlugins')
->with($this->equalTo('event'.$eventName), $this->equalTo($params), $override);
$clientMock->triggerEvent($eventName, $params, $override);
}
} }
class MyAdapter extends Solarium_Client_Adapter_Http{ class MyAdapter extends Solarium_Client_Adapter_Http{
......
...@@ -53,4 +53,27 @@ class Solarium_Plugin_Loadbalancer_WeightedRandomChoiceTest extends PHPUnit_Fram ...@@ -53,4 +53,27 @@ class Solarium_Plugin_Loadbalancer_WeightedRandomChoiceTest extends PHPUnit_Fram
$this->assertTrue($counts['key2'] < $counts['key3']); $this->assertTrue($counts['key2'] < $counts['key3']);
} }
public function testGetRandomWithExclude()
{
$choices = array('key1' => 1, 'key2' => 1, 'key3' => 300);
$excludes = array('key3');
$randomizer = new Solarium_Plugin_Loadbalancer_WeightedRandomChoice($choices);
$key = $randomizer->getRandom($excludes);
$this->assertTrue($key !== 'key3');
}
public function testAllEntriesExcluded()
{
$choices = array('key1' => 1, 'key2' => 2, 'key3' => 3);
$excludes = array_keys($choices);
$randomizer = new Solarium_Plugin_Loadbalancer_WeightedRandomChoice($choices);
$this->setExpectedException('Solarium_Exception');
$randomizer->getRandom($excludes);
}
} }
\ No newline at end of file
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*/
class Solarium_Plugin_LoadbalancerTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Plugin_Loadbalancer
*/
protected $_plugin;
protected $_serverOptions = array('host' => '192.168.1.10');
public function setUp()
{
$this->_plugin = new Solarium_Plugin_Loadbalancer();
}
public function testSetAndGetFailoverEnabled()
{
$this->_plugin->setFailoverEnabled(true);
$this->assertEquals(true, $this->_plugin->getFailoverEnabled());
}
public function testSetAndGetFailoverMaxRetries()
{
$this->_plugin->setFailoverMaxRetries(16);
$this->assertEquals(16, $this->_plugin->getFailoverMaxRetries());
}
public function testAddServer()
{
$this->_plugin->addServer('s1', $this->_serverOptions, 10);
$this->assertEquals(
array('s1' =>
array(
'options' => $this->_serverOptions,
'weight' => 10,
)
),
$this->_plugin->getServers()
);
}
public function testAddServerWithDuplicateKey()
{
$this->_plugin->addServer('s1', $this->_serverOptions, 10);
$this->setExpectedException('Solarium_Exception');
$this->_plugin->addServer('s1', $this->_serverOptions, 20);
}
public function testGetServer()
{
$this->_plugin->addServer('s1', $this->_serverOptions, 10);
$this->assertEquals(
array('options' => $this->_serverOptions, 'weight' => 10),
$this->_plugin->getServer('s1')
);
}
public function testGetInvalidServer()
{
$this->_plugin->addServer('s1', $this->_serverOptions, 10);
$this->setExpectedException('Solarium_Exception');
$this->_plugin->getServer('s2');
}
public function testClearServers()
{
$this->_plugin->addServer('s1', $this->_serverOptions, 10);
$this->_plugin->clearServers();
$this->assertEquals(array(), $this->_plugin->getServers());
}
public function testAddServers()
{
$servers = array(
's1' => array('options' => $this->_serverOptions, 'weight' => 10),
's2' => array('options' => $this->_serverOptions, 'weight' => 20),
);
$this->_plugin->addServers($servers);
$this->assertEquals($servers, $this->_plugin->getServers());
}
public function testRemoveServer()
{
$servers = array(
's1' => array('options' => $this->_serverOptions, 'weight' => 10),
's2' => array('options' => $this->_serverOptions, 'weight' => 20),
);
$this->_plugin->addServers($servers);
$this->_plugin->removeServer('s1');
$this->assertEquals(
array('s2' => array('options' => $this->_serverOptions, 'weight' => 20)),
$this->_plugin->getServers()
);
}
public function testSetServers()
{
$servers1 = array(
's1' => array('options' => $this->_serverOptions, 'weight' => 10),
's2' => array('options' => $this->_serverOptions, 'weight' => 20),
);
$servers2 = array(
's3' => array('options' => $this->_serverOptions, 'weight' => 50),
's4' => array('options' => $this->_serverOptions, 'weight' => 30),
);
$this->_plugin->addServers($servers1);
$this->_plugin->setServers($servers2);
$this->assertEquals(
$servers2,
$this->_plugin->getServers()
);
}
public function testSetAndGetForcedServerForNextQuery()
{
$servers1 = array(
's1' => array('options' => $this->_serverOptions, 'weight' => 10),
's2' => array('options' => $this->_serverOptions, 'weight' => 20),
);
$this->_plugin->addServers($servers1);
$this->_plugin->setForcedServerForNextQuery('s2');
$this->assertEquals('s2', $this->_plugin->getForcedServerForNextQuery());
}
public function testSetForcedServerForNextQueryWithInvalidKey()
{
$servers1 = array(
's1' => array('options' => $this->_serverOptions, 'weight' => 10),
's2' => array('options' => $this->_serverOptions, 'weight' => 20),
);
$this->_plugin->addServers($servers1);
$this->setExpectedException('Solarium_Exception');
$this->_plugin->setForcedServerForNextQuery('s3');
}
public function testAddBlockedQueryType()
{
$this->_plugin->addBlockedQueryType('type1');
$this->_plugin->addBlockedQueryType('type2');
$this->assertEquals(
array(Solarium_Client::QUERYTYPE_UPDATE, 'type1', 'type2'),
$this->_plugin->getBlockedQueryTypes()
);
}
public function testClearBlockedQueryTypes()
{
$this->_plugin->addBlockedQueryType('type1');
$this->_plugin->addBlockedQueryType('type2');
$this->_plugin->clearBlockedQueryTypes();
$this->assertEquals(array(), $this->_plugin->getBlockedQueryTypes());
}
public function testAddBlockedQueryTypes()
{
$blockedQueryTypes = array('type1', 'type2', 'type3');
$this->_plugin->clearBlockedQueryTypes();
$this->_plugin->addBlockedQueryTypes($blockedQueryTypes);
$this->assertEquals($blockedQueryTypes, $this->_plugin->getBlockedQueryTypes());
}
public function testRemoveBlockedQueryType()
{
$blockedQueryTypes = array('type1', 'type2', 'type3');
$this->_plugin->clearBlockedQueryTypes();
$this->_plugin->addBlockedQueryTypes($blockedQueryTypes);
$this->_plugin->removeBlockedQueryType('type2');
$this->assertEquals(
array('type1', 'type3'),
$this->_plugin->getBlockedQueryTypes()
);
}
public function testSetBlockedQueryTypes()
{
$blockedQueryTypes = array('type1', 'type2', 'type3');
$this->_plugin->setBlockedQueryTypes($blockedQueryTypes);
$this->assertEquals(
$blockedQueryTypes,
$this->_plugin->getBlockedQueryTypes()
);
}
}
\ No newline at end of file
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