From 973e8592e3adcd9d16bc7780423580664a44d9fc Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Tue, 17 May 2016 16:56:34 +0200 Subject: [PATCH] Throw when command sent in connect() returns error. Common failures are the use of SELECT with a database index outside the bound of the configured number of databases in redis.conf or the use of a wrong password for authentication with AUTH. This resolves #322. --- src/Connection/PhpiredisSocketConnection.php | 7 ++++- src/Connection/StreamConnection.php | 7 ++++- .../PhpiredisSocketConnectionTest.php | 30 +++++++++++++++++++ .../PhpiredisStreamConnectionTest.php | 30 +++++++++++++++++++ .../Connection/StreamConnectionTest.php | 30 +++++++++++++++++++ 5 files changed, 102 insertions(+), 2 deletions(-) diff --git a/src/Connection/PhpiredisSocketConnection.php b/src/Connection/PhpiredisSocketConnection.php index f93af8a5..f52ae71d 100644 --- a/src/Connection/PhpiredisSocketConnection.php +++ b/src/Connection/PhpiredisSocketConnection.php @@ -14,6 +14,7 @@ namespace Predis\Connection; use Predis\Command\CommandInterface; use Predis\NotSupportedException; use Predis\Response\Error as ErrorResponse; +use Predis\Response\ErrorInterface as ErrorResponseInterface; use Predis\Response\Status as StatusResponse; /** @@ -316,7 +317,11 @@ class PhpiredisSocketConnection extends AbstractConnection { if (parent::connect() && $this->initCommands) { foreach ($this->initCommands as $command) { - $this->executeCommand($command); + $response = $this->executeCommand($command); + + if ($response instanceof ErrorResponseInterface) { + $this->onConnectionError("`{$command->getId()}` failed: $response", 0); + } } } } diff --git a/src/Connection/StreamConnection.php b/src/Connection/StreamConnection.php index b1c682c7..e79bcdd9 100644 --- a/src/Connection/StreamConnection.php +++ b/src/Connection/StreamConnection.php @@ -13,6 +13,7 @@ namespace Predis\Connection; use Predis\Command\CommandInterface; use Predis\Response\Error as ErrorResponse; +use Predis\Response\ErrorInterface as ErrorResponseInterface; use Predis\Response\Status as StatusResponse; /** @@ -256,7 +257,11 @@ class StreamConnection extends AbstractConnection { if (parent::connect() && $this->initCommands) { foreach ($this->initCommands as $command) { - $this->executeCommand($command); + $response = $this->executeCommand($command); + + if ($response instanceof ErrorResponseInterface) { + $this->onConnectionError("`{$command->getId()}` failed: $response", 0); + } } } } diff --git a/tests/Predis/Connection/PhpiredisSocketConnectionTest.php b/tests/Predis/Connection/PhpiredisSocketConnectionTest.php index 7d3459ef..a2b5d9db 100644 --- a/tests/Predis/Connection/PhpiredisSocketConnectionTest.php +++ b/tests/Predis/Connection/PhpiredisSocketConnectionTest.php @@ -11,6 +11,9 @@ namespace Predis\Connection; +use Predis\Command\RawCommand; +use Predis\Response\Error as ErrorResponse; + /** * @group ext-phpiredis * @requires extension phpiredis @@ -43,6 +46,33 @@ class PhpiredisSocketConnectionTest extends PredisConnectionTestCase $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection); } + /** + * @group disconnected + * @expectedException \Predis\Connection\ConnectionException + * @expectedExceptionMessage `SELECT` failed: ERR invalid DB index [tcp://127.0.0.1:6379] + */ + public function testThrowsExceptionOnInitializationCommandFailure() + { + $cmdSelect = RawCommand::create('SELECT', '1000'); + $responseError = + + $connection = $this->getMockBuilder(static::CONNECTION_CLASS) + ->setMethods(array('executeCommand', 'createResource')) + ->setConstructorArgs(array(new Parameters())) + ->getMock(); + + $connection->method('executeCommand') + ->with($cmdSelect) + ->will($this->returnValue( + new ErrorResponse("ERR invalid DB index") + )); + + $connection->method('createResource'); + + $connection->addConnectCommand($cmdSelect); + $connection->connect(); + } + // ******************************************************************** // // ---- INTEGRATION TESTS --------------------------------------------- // // ******************************************************************** // diff --git a/tests/Predis/Connection/PhpiredisStreamConnectionTest.php b/tests/Predis/Connection/PhpiredisStreamConnectionTest.php index 5ac3678a..6e453b76 100644 --- a/tests/Predis/Connection/PhpiredisStreamConnectionTest.php +++ b/tests/Predis/Connection/PhpiredisStreamConnectionTest.php @@ -11,6 +11,9 @@ namespace Predis\Connection; +use Predis\Command\RawCommand; +use Predis\Response\Error as ErrorResponse; + /** * @group ext-phpiredis * @requires extension phpiredis @@ -43,6 +46,33 @@ class PhpiredisStreamConnectionTest extends PredisConnectionTestCase $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection); } + /** + * @group disconnected + * @expectedException \Predis\Connection\ConnectionException + * @expectedExceptionMessage `SELECT` failed: ERR invalid DB index [tcp://127.0.0.1:6379] + */ + public function testThrowsExceptionOnInitializationCommandFailure() + { + $cmdSelect = RawCommand::create('SELECT', '1000'); + $responseError = + + $connection = $this->getMockBuilder(static::CONNECTION_CLASS) + ->setMethods(array('executeCommand', 'createResource')) + ->setConstructorArgs(array(new Parameters())) + ->getMock(); + + $connection->method('executeCommand') + ->with($cmdSelect) + ->will($this->returnValue( + new ErrorResponse("ERR invalid DB index") + )); + + $connection->method('createResource'); + + $connection->addConnectCommand($cmdSelect); + $connection->connect(); + } + // ******************************************************************** // // ---- INTEGRATION TESTS --------------------------------------------- // // ******************************************************************** // diff --git a/tests/Predis/Connection/StreamConnectionTest.php b/tests/Predis/Connection/StreamConnectionTest.php index ccc87964..a21fc24a 100644 --- a/tests/Predis/Connection/StreamConnectionTest.php +++ b/tests/Predis/Connection/StreamConnectionTest.php @@ -11,6 +11,9 @@ namespace Predis\Connection; +use Predis\Command\RawCommand; +use Predis\Response\Error as ErrorResponse; + /** * */ @@ -18,6 +21,33 @@ class StreamConnectionTest extends PredisConnectionTestCase { const CONNECTION_CLASS = 'Predis\Connection\StreamConnection'; + /** + * @group disconnected + * @expectedException \Predis\Connection\ConnectionException + * @expectedExceptionMessage `SELECT` failed: ERR invalid DB index [tcp://127.0.0.1:6379] + */ + public function testThrowsExceptionOnInitializationCommandFailure() + { + $cmdSelect = RawCommand::create('SELECT', '1000'); + $responseError = + + $connection = $this->getMockBuilder(static::CONNECTION_CLASS) + ->setMethods(array('executeCommand', 'createResource')) + ->setConstructorArgs(array(new Parameters())) + ->getMock(); + + $connection->method('executeCommand') + ->with($cmdSelect) + ->will($this->returnValue( + new ErrorResponse("ERR invalid DB index") + )); + + $connection->method('createResource'); + + $connection->addConnectCommand($cmdSelect); + $connection->connect(); + } + // ******************************************************************** // // ---- INTEGRATION TESTS --------------------------------------------- // // ******************************************************************** //