Added RESP3 protocol option, establish RESP3 connection (#1267)

* Added RESP3 protocol option, establish RESP3 connection

* Removed method from the interface
This commit is contained in:
Vladyslav Vildanov
2023-05-05 11:52:11 +03:00
committed by GitHub
parent e9431f788d
commit d18e20c98f
7 changed files with 51 additions and 0 deletions
+8
View File
@@ -101,6 +101,14 @@ abstract class AbstractConnection implements NodeConnectionInterface
$this->initCommands[] = $command;
}
/**
* {@inheritdoc}
*/
public function getInitCommands(): array
{
return $this->initCommands;
}
/**
* {@inheritdoc}
*/
+6
View File
@@ -174,6 +174,12 @@ class Factory implements FactoryInterface
);
}
if (isset($parameters->protocol) && (int) $parameters->protocol > 2) {
$connection->addConnectCommand(
new RawCommand('HELLO', [$parameters->protocol, 'SETNAME', 'predis'])
);
}
if (isset($parameters->database) && strlen($parameters->database)) {
$connection->addConnectCommand(
new RawCommand('SELECT', [$parameters->database])
+1
View File
@@ -25,6 +25,7 @@ class Parameters implements ParametersInterface
'scheme' => 'tcp',
'host' => '127.0.0.1',
'port' => 6379,
'protocol' => 2,
];
/**
+1
View File
@@ -22,6 +22,7 @@ namespace Predis\Connection;
* @property string $scheme Connection scheme, such as 'tcp' or 'unix'.
* @property string $host IP address or hostname of Redis.
* @property int $port TCP port on which Redis is listening to.
* @property int $protocol Version of RESP protocol.
* @property string $path Path of a UNIX domain socket file.
* @property string $alias Alias for the connection.
* @property float $timeout Timeout for the connect() operation.
+17
View File
@@ -12,6 +12,7 @@
namespace Predis\Connection;
use Predis\Command\RawCommand;
use PredisTestCase;
use ReflectionObject;
use stdClass;
@@ -538,6 +539,22 @@ class FactoryTest extends PredisTestCase
$factory->create('test://127.0.0.1');
}
/**
* @group disconnected
* @return void
*/
public function testCreatesResp3ConnectionOnProtocolParameterGiven(): void
{
$parameters = ['protocol' => 3];
$factory = new Factory();
$connection = $factory->create($parameters);
$initCommands = $connection->getInitCommands();
$this->assertInstanceOf(RawCommand::class, $initCommands[0]);
$this->assertSame('HELLO', $initCommands[0]->getId());
}
// ******************************************************************** //
// ---- HELPER METHODS ------------------------------------------------ //
// ******************************************************************** //
@@ -401,6 +401,7 @@ class ParametersTest extends PredisTestCase
'scheme' => 'tcp',
'host' => '127.0.0.1',
'port' => 6379,
'protocol' => 2,
];
}
@@ -195,4 +195,21 @@ class StreamConnectionTest extends PredisConnectionTestCase
$this->assertArrayHasKey('tcp_nodelay', $options['socket']);
$this->assertFalse($options['socket']['tcp_nodelay']);
}
/**
* @group disconnected
* @return void
*/
public function testGetInitCommandsReturnsGivenInitCommands(): void
{
$command = new RawCommand('HELLO', [3]);
$connection = $this->createConnection();
$connection->addConnectCommand($command);
$initCommands = $connection->getInitCommands();
$this->assertInstanceOf(RawCommand::class, $initCommands[0]);
$this->assertSame('HELLO', $initCommands[0]->getId());
}
}