Skip to content

Commit

Permalink
- improved phpunit coverage
Browse files Browse the repository at this point in the history
- small fixes and improvements to loadbalancer plugin
  • Loading branch information
basdenooijer committed Nov 4, 2011
1 parent 0a45304 commit a1c99c5
Show file tree
Hide file tree
Showing 5 changed files with 308 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/7.1-plugin-loadbalancer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
}

// 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);
echo 'Query execution with server forced to solr3<br/>';
echo 'NumFound: ' . $resultset->getNumFound(). '<br/>';
Expand Down
22 changes: 17 additions & 5 deletions library/Solarium/Plugin/Loadbalancer.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Solarium_Plugin_Loadbalancer extends Solarium_Plugin_Abstract
* @var array
*/
protected $_blockedQueryTypes = array(
Solarium_Client::QUERYTYPE_UPDATE
Solarium_Client::QUERYTYPE_UPDATE => true
);

/**
Expand Down Expand Up @@ -296,21 +296,33 @@ public function removeServer($key)
/**
* 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
* but will still be reset.
*
* @param string $key
* @param string|null $key
* @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');
}

$this->_nextServer = $key;
return $this;
}

/**
* Get the ForcedServerForNextQuery value
*
* @return string|null
*/
public function getForcedServerForNextQuery()
{
return $this->_nextServer;
}

/**
Expand Down
41 changes: 35 additions & 6 deletions tests/Solarium/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function testConfigMode()
$plugin = $this->_client->getPlugin('myplugin');
$this->assertThat($plugin, $this->isInstanceOf('MyClientPlugin'));
$this->assertEquals($options['plugin']['myplugin']['options'], $plugin->getOptions());

}

public function testConfigModeWithoutKeys()
Expand Down Expand Up @@ -151,7 +151,7 @@ public function testSetAndGetAdapterWithString()
$this->_client->setAdapter($adapterClass);
$this->assertThat($this->_client->getAdapter(), $this->isInstanceOf($adapterClass));
}

public function testSetAndGetAdapterWithObject()
{
$adapterClass = 'MyAdapter';
Expand Down Expand Up @@ -205,10 +205,25 @@ public function testGetInvalidPlugin()
{
$this->assertEquals(
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()
{
$options = array('option1' => 1);
Expand Down Expand Up @@ -469,7 +484,7 @@ public function testExecutePostPlugin()
public function testExecuteWithOverridingPlugin()
{
$query = new Solarium_Query_Ping();

$observer = $this->getMock('Solarium_Plugin_Abstract', array(), array($this->_client,array()));
$observer->expects($this->once())
->method('preExecute')
Expand Down Expand Up @@ -747,8 +762,8 @@ public function testCreateMoreLikeThis()
->with($this->equalTo(Solarium_Client::QUERYTYPE_MORELIKETHIS), $this->equalTo($options));

$observer->createMoreLikeThis($options);
}
}

public function testCreateAnalysisField()
{
$options = array('optionA' => 1, 'optionB' => 2);
Expand All @@ -773,6 +788,20 @@ public function testCreateAnalysisDocument()
$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{
Expand Down
23 changes: 23 additions & 0 deletions tests/Solarium/Plugin/Loadbalancer/WeightedRandomChoiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,27 @@ public function testGetRandom()
$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);
}

}
Loading

0 comments on commit a1c99c5

Please sign in to comment.