Improved connection handshake session (#1431)

* Added pipelining for on-connection commands

* Added server version condition for tests

* Fixed static analysis and codestyle errors

* Improved handshake session

* Updated HELLO retry logic

* Changed incorrect variable

* Added return statement

* Added more test coverage, codestyle fixes
This commit is contained in:
Vladyslav Vildanov
2024-03-04 09:18:46 +02:00
committed by GitHub
parent 4b28ab1c74
commit 8b464f2a60
16 changed files with 398 additions and 55 deletions
@@ -33,19 +33,21 @@ class StreamConnectionTest extends PredisConnectionTestCase
public function testThrowsExceptionOnInitializationCommandFailure(): void
{
$this->expectException('Predis\Connection\ConnectionException');
$this->expectExceptionMessage('`SELECT` failed: ERR invalid DB index [tcp://127.0.0.1:6379]');
$this->expectExceptionMessage('Failed: ERR invalid DB index [tcp://127.0.0.1:6379]');
$cmdSelect = RawCommand::create('SELECT', '1000');
/** @var NodeConnectionInterface|MockObject */
$connection = $this
->getMockBuilder($this->getConnectionClass())
->onlyMethods(['executeCommand', 'createResource'])
->onlyMethods(['write', 'read', 'createResource'])
->setConstructorArgs([new Parameters()])
->getMock();
$connection
->method('executeCommand')
->with($cmdSelect)
->expects($this->once())
->method('write');
$connection
->method('read')
->willReturn(
new ErrorResponse('ERR invalid DB index')
);
@@ -215,6 +217,105 @@ class StreamConnectionTest extends PredisConnectionTestCase
$this->assertTrue(true);
}
/**
* @group connected
* @return void
* @requiresRedisVersion >= 6.2.0
*/
public function testSetClientIdOnResp2Connection(): void
{
$connection = $this->createConnectionWithParams([]);
$connection->addConnectCommand(
new RawCommand('HELLO', [2])
);
$connection->connect();
$this->assertNotNull($connection->getClientId());
}
/**
* @group connected
* @return void
* @requiresRedisVersion >= 6.2.0
*/
public function testDoNotSetClientIdOnResp2ConnectionIfNotHelloCommand(): void
{
$connection = $this->createConnectionWithParams([]);
$connection->addConnectCommand(
new RawCommand('INFO')
);
$connection->connect();
$this->assertNull($connection->getClientId());
}
/**
* @group connected
* @return void
* @requiresRedisVersion >= 6.2.0
*/
public function testSetClientIdOnResp3Connection(): void
{
$connection = $this->createConnectionWithParams(['protocol' => 3]);
$connection->addConnectCommand(
new RawCommand('HELLO', [3])
);
$connection->connect();
$this->assertNotNull($connection->getClientId());
}
/**
* @group connected
* @return void
*/
public function testConnectionDoesNotThrowsExceptionOnClientCommandFail(): void
{
$failedCommand = new RawCommand('CLIENT', ['FOOBAR']);
$connection = $this->createConnection();
$connection->addConnectCommand($failedCommand);
$connection->connect();
$this->assertTrue(true);
}
/**
* @group connected
* @return void
* @requiresRedisVersion >= 6.2.0
*/
public function testConnectionRetriesOnFailingHelloCommand(): void
{
$failedCommand = new RawCommand('HELLO', ['FOOBAR']);
$connection = $this->createConnection();
$connection->addConnectCommand($failedCommand);
$connection->connect();
$clientName = $connection->executeCommand(new RawCommand('CLIENT', ['GETNAME']));
$this->assertSame('predis', $clientName);
}
/**
* @group connected
* @return void
* @requiresRedisVersion >= 6.2.0
*/
public function testConnectionRetriesOnFailingHelloCommandButFailsOnAuth(): void
{
$failedCommand = new RawCommand('HELLO', ['FOOBAR', 'AUTH', 'foobar']);
$connection = $this->createConnection();
$connection->addConnectCommand($failedCommand);
$this->expectException(ConnectionException::class);
$connection->connect();
}
/**
* @group disconnected
* @return void