Skip to content

Commit

Permalink
Add tests, minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
chabior committed Aug 6, 2014
1 parent ddcdbf4 commit 384e930
Show file tree
Hide file tree
Showing 12 changed files with 314 additions and 42 deletions.
3 changes: 3 additions & 0 deletions changes.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
CHANGES

2014-08-06
- Add connection pool and connection strategy

2014-07-26
- Release v1.3.0.0
- Prepare Elastica Release v1.3.0.0
Expand Down
9 changes: 2 additions & 7 deletions lib/Elastica/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ protected function _initConnections()
$strategy = Connection\Strategy\StrategyFactory::getSimpleStrategy();
}

$this->_connectionPool = new Connection\ConnectionPool($connections, $strategy);
$this->_connectionPool = new Connection\ConnectionPool($connections, $strategy, $this->_callback);
}

/**
Expand Down Expand Up @@ -584,12 +584,7 @@ public function request($path, $method = Request::GET, $data = array(), array $q
return $response;

} catch (ConnectionException $e) {
$connection->setEnabled(false);

// Calls callback with connection as param to make it possible to persist invalid connections
if ($this->_callback) {
call_user_func($this->_callback, $connection, $e, $this);
}
$this->_connectionPool->onFail($connection, $e, $this);

// In case there is no valid connection left, throw exception which caused the disabling of the connection.
if (!$this->hasConnection())
Expand Down
31 changes: 27 additions & 4 deletions lib/Elastica/Connection/ConnectionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Elastica\Connection;

use Elastica\Client;
use Elastica\Connection;
use Elastica\Connection\Strategy\StrategyInterface;
use Exception;

/**
* Description of ConnectionPool
Expand All @@ -19,22 +21,29 @@ class ConnectionPool
protected $connections;
/**
*
* @var Elastica\Connection\Strategy\StrategyInterface
* @var StrategyInterface
*/
protected $strategy;
/**
*
* @var callback
*/
protected $callback;
/**
*
* @param array|Connection[] $connections
*/
public function __construct(array $connections, StrategyInterface $strategy)
public function __construct(array $connections, StrategyInterface $strategy, $callback = null)
{
$this->connections = $connections;

$this->strategy = $strategy;

$this->callback = $callback;
}
/**
*
* @param \Elastica\Connection $connection
* @param Connection $connection
*/
public function addConnection(Connection $connection)
{
Expand Down Expand Up @@ -72,10 +81,24 @@ public function getConnections()
}
/**
*
* @return \Elastica\Connection
* @return Connection
*/
public function getConnection()
{
return $this->strategy->getConnection($this->getConnections());
}
/**
*
* @param Connection $connection
* @param Exception $e
* @param Client $client
*/
public function onFail(Connection $connection, Exception $e, Client $client)
{
$connection->setEnabled(false);

if ($this->callback) {
call_user_func($this->callback, $connection, $e, $client);
}
}
}
2 changes: 1 addition & 1 deletion lib/Elastica/Connection/Strategy/CallbackStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ public function getConnection($connections)
*/
public static function isValid($callback)
{
return is_object($callback) && ($callback instanceof Closure);
return is_object($callback) && ($callback instanceof \Closure);
}
}
29 changes: 0 additions & 29 deletions lib/Elastica/Connection/Strategy/Request.php

This file was deleted.

2 changes: 1 addition & 1 deletion lib/Elastica/Connection/Strategy/StrategyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static function create($strategyName)
$strategy = $strategyName;
} else if (CallbackStrategy::isValid($strategyName)) {
$strategy = new CallbackStrategy($strategyName);
} else if (class_exists($strategyName)) {
} else if (is_string($strategyName) && class_exists($strategyName)) {
$strategy = new $strategyName();
} else if (is_string($strategyName)) {
$pathToStrategy = '\\Elastica\\Connection\\Strategy\\'.$strategyName;
Expand Down
83 changes: 83 additions & 0 deletions test/lib/Elastica/Test/Connection/ConnectionPollTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Elastica\Test\Connection;

use Elastica\Test\Base as BaseTest;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
* Description of ConnectionPollTest
*
* @author chabior
*/
class ConnectionPollTest extends BaseTest
{
public function testConstruct()
{
$pool = $this->createPool();

$this->assertEquals($this->getConnections(), $pool->getConnections());
}

public function testSetConnections()
{
$pool = $this->createPool();

$connections = $this->getConnections(5);

$pool->setConnections($connections);

$this->assertEquals($connections, $pool->getConnections());
}

public function testHasConnection()
{
$pool = $this->createPool();

$this->assertTrue($pool->hasConnection());
}

public function testFailHasConnections()
{
$pool = $this->createPool();

$pool->setConnections(array());

$this->assertFalse($pool->hasConnection());
}

public function testGetConnection()
{
$pool = $this->createPool();

$this->assertTrue($pool->getConnection() instanceof \Elastica\Connection);
}

protected function getConnections($quantity = 1)
{
$params = array();
$connections = array();

for ($i = 0; $i<$quantity; $i++) {
$connections[] = new \Elastica\Connection($params);
}

return $connections;
}

protected function createPool()
{
$connections = $this->getConnections();
$strategy = \Elastica\Connection\Strategy\StrategyFactory::getSimpleStrategy();

$pool = new \Elastica\Connection\ConnectionPool($connections, $strategy);
$pool->getConnections();

return $pool;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Elastica\Test\Connection\Strategy;

use Elastica\Connection\Strategy\CallbackStrategy;
use Elastica\Test\Base;

/**
* Description of CallbackStrategyTest
*
* @author chabior
*/
class CallbackStrategyTest extends Base
{
public function testInvoke()
{
$count = 0;

$callback = function ($connections) use(&$count) {
$count++;
};

$strategy = new CallbackStrategy($callback);
$strategy->getConnection(array());

$this->assertEquals(1, $count);
}

public function testIsValid()
{
$callback = function(){};

$isValid = CallbackStrategy::isValid($callback);

$this->assertTrue($isValid);
}

public function testFailIsValid()
{
$callback = new \stdClass();

$isValid = CallbackStrategy::isValid($callback);

$this->assertFalse($isValid);
}

public function testConnection()
{
$count = 0;

$config = array('connectionStrategy' => function ($connections) use(&$count) {
++$count;
return current($connections);
});

$client = new \Elastica\Client($config);
$client->request('/_aliases');

$this->assertEquals(1, $count);
}
}
18 changes: 18 additions & 0 deletions test/lib/Elastica/Test/Connection/Strategy/EmptyStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Elastica\Test\Connection\Strategy;

use Elastica\Connection\Strategy\StrategyInterface;

/**
* Description of EmptyStrategy
*
* @author chabior
*/
class EmptyStrategy implements StrategyInterface
{
public function getConnection($connections)
{
return null;
}
}
20 changes: 20 additions & 0 deletions test/lib/Elastica/Test/Connection/Strategy/RoundRobinTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Elastica\Test\Connection\Strategy;

use Elastica\Test\Base;

/**
* Description of RoundRobinTest
*
* @author chabior
*/
class RoundRobinTest extends Base
{
public function testConnection()
{
$config = array('connectionStrategy' => 'RoundRobin');
$client = new \Elastica\Client($config);
$client->request('/_aliases');
}
}
19 changes: 19 additions & 0 deletions test/lib/Elastica/Test/Connection/Strategy/SimplyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Elastica\Test\Connection\Strategy;

use Elastica\Test\Base;

/**
* Description of SimplyTest
*
* @author chabior
*/
class SimplyTest extends Base
{
public function testConnection()
{
$client = new \Elastica\Client();
$client->request('/_aliases');
}
}
Loading

0 comments on commit 384e930

Please sign in to comment.