mirror of
https://github.com/predis/predis.git
synced 2026-10-05 19:38:51 +00:00
Improved Relay integration (#1423)
* Fixed Relay objects structure (#1419) * Added OSS cluster tests (#1421) --------- Co-authored-by: Till Krüss <tillkruss@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
87b0d2fa15
commit
d56e32715b
@@ -12,16 +12,244 @@
|
||||
|
||||
namespace Predis\Connection;
|
||||
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Predis\Command\RawCommand;
|
||||
use Predis\Response\Error as ErrorResponse;
|
||||
use PredisTestCase;
|
||||
use Relay\Relay;
|
||||
|
||||
/**
|
||||
* @group ext-relay
|
||||
* @requires extension relay
|
||||
*/
|
||||
class RelayConnectionTest extends PredisConnectionTestCase
|
||||
class RelayConnectionTest extends PredisTestCase
|
||||
{
|
||||
/**
|
||||
* @var Relay
|
||||
*/
|
||||
private $mockClient;
|
||||
|
||||
/**
|
||||
* @var ParametersInterface
|
||||
*/
|
||||
private $parameters;
|
||||
|
||||
/**
|
||||
* @var RelayConnection
|
||||
*/
|
||||
private $connection;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->mockClient = $this->getMockBuilder(Relay::class)->getMock();
|
||||
$this->parameters = new Parameters();
|
||||
|
||||
$this->connection = new RelayConnection($this->parameters, $this->mockClient);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
* @return void
|
||||
*/
|
||||
public function testIsConnected(): void
|
||||
{
|
||||
$this->mockClient
|
||||
->expects($this->once())
|
||||
->method('isConnected')
|
||||
->withAnyParameters()
|
||||
->willReturn(true);
|
||||
|
||||
$this->assertTrue($this->connection->isConnected());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
* @return void
|
||||
*/
|
||||
public function testDisconnect(): void
|
||||
{
|
||||
$this->mockClient
|
||||
->expects($this->once())
|
||||
->method('isConnected')
|
||||
->withAnyParameters()
|
||||
->willReturn(true);
|
||||
|
||||
$this->mockClient
|
||||
->expects($this->once())
|
||||
->method('close')
|
||||
->withAnyParameters();
|
||||
|
||||
$this->connection->disconnect();
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
* @return void
|
||||
*/
|
||||
public function testGetClient(): void
|
||||
{
|
||||
$this->assertSame($this->mockClient, $this->connection->getClient());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
* @return void
|
||||
*/
|
||||
public function testExecuteCommandOnAlreadyConnectedClient(): void
|
||||
{
|
||||
$this->mockClient
|
||||
->expects($this->once())
|
||||
->method('isConnected')
|
||||
->withAnyParameters()
|
||||
->willReturn(true);
|
||||
|
||||
$this->mockClient
|
||||
->expects($this->once())
|
||||
->method('rawCommand')
|
||||
->with('GET', 'foo')
|
||||
->willReturn('bar');
|
||||
|
||||
$response = $this->connection->executeCommand(new RawCommand('GET', ['foo']));
|
||||
|
||||
$this->assertSame('bar', $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
* @return void
|
||||
*/
|
||||
public function testExecuteAtypicalCommandOnAlreadyConnectedClient(): void
|
||||
{
|
||||
$this->mockClient
|
||||
->expects($this->once())
|
||||
->method('isConnected')
|
||||
->withAnyParameters()
|
||||
->willReturn(true);
|
||||
|
||||
$this->mockClient
|
||||
->expects($this->once())
|
||||
->method('AUTH')
|
||||
->with('foo', 'bar')
|
||||
->willReturn(true);
|
||||
|
||||
$response = $this->connection->executeCommand(new RawCommand('AUTH', ['foo', 'bar']));
|
||||
|
||||
$this->assertTrue($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
* @return void
|
||||
*/
|
||||
public function testExecuteCommandOnNonConnectedClient(): void
|
||||
{
|
||||
$this->mockClient
|
||||
->expects($this->exactly(2))
|
||||
->method('isConnected')
|
||||
->withAnyParameters()
|
||||
->willReturn(false);
|
||||
|
||||
$this->mockClient
|
||||
->expects($this->once())
|
||||
->method('connect')
|
||||
->with($this->parameters->host, $this->parameters->port);
|
||||
|
||||
$this->mockClient
|
||||
->expects($this->once())
|
||||
->method('rawCommand')
|
||||
->with('GET', 'foo')
|
||||
->willReturn('bar');
|
||||
|
||||
$response = $this->connection->executeCommand(new RawCommand('GET', ['foo']));
|
||||
|
||||
$this->assertSame('bar', $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
* @return void
|
||||
*/
|
||||
public function testConnectExecutesOnConnectionCommand(): void
|
||||
{
|
||||
$this->mockClient
|
||||
->expects($this->exactly(3))
|
||||
->method('isConnected')
|
||||
->withAnyParameters()
|
||||
->willReturn(false);
|
||||
|
||||
$this->mockClient
|
||||
->expects($this->once())
|
||||
->method('connect')
|
||||
->with($this->parameters->host, $this->parameters->port);
|
||||
|
||||
$this->mockClient
|
||||
->expects($this->exactly(2))
|
||||
->method('rawCommand')
|
||||
->withConsecutive(['GET', 'foo'], ['GET', 'bar'])
|
||||
->willReturnOnConsecutiveCalls('baz', 'bad');
|
||||
|
||||
$this->connection->addConnectCommand(new RawCommand('GET', ['foo']));
|
||||
$this->connection->addConnectCommand(new RawCommand('GET', ['bar']));
|
||||
|
||||
$this->connection->connect();
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
* @return void
|
||||
*/
|
||||
public function testConnectThrowsExceptionOnErrorResponse(): void
|
||||
{
|
||||
$this->mockClient
|
||||
->expects($this->exactly(4))
|
||||
->method('isConnected')
|
||||
->withAnyParameters()
|
||||
->willReturn(false);
|
||||
|
||||
$this->mockClient
|
||||
->expects($this->once())
|
||||
->method('connect')
|
||||
->with($this->parameters->host, $this->parameters->port);
|
||||
|
||||
$this->mockClient
|
||||
->expects($this->exactly(2))
|
||||
->method('rawCommand')
|
||||
->withConsecutive(['GET', 'foo'], ['GET', 'bar'])
|
||||
->willReturnOnConsecutiveCalls('baz', new ErrorResponse('FooBar'));
|
||||
|
||||
$this->connection->addConnectCommand(new RawCommand('GET', ['foo']));
|
||||
$this->connection->addConnectCommand(new RawCommand('GET', ['bar']));
|
||||
|
||||
$this->expectException(ConnectionException::class);
|
||||
$this->expectExceptionMessage('`GET` failed: FooBar [tcp://127.0.0.1:6379]');
|
||||
|
||||
$this->connection->connect();
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
* @return void
|
||||
*/
|
||||
public function testConnectDoNotExecuteCommands(): void
|
||||
{
|
||||
$this->mockClient
|
||||
->expects($this->once())
|
||||
->method('isConnected')
|
||||
->withAnyParameters()
|
||||
->willReturn(false);
|
||||
|
||||
$this->mockClient
|
||||
->expects($this->once())
|
||||
->method('connect')
|
||||
->with($this->parameters->host, $this->parameters->port);
|
||||
|
||||
$this->mockClient
|
||||
->expects($this->never())
|
||||
->method('rawCommand')
|
||||
->withAnyParameters();
|
||||
|
||||
$this->connection->connect();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@@ -30,35 +258,6 @@ class RelayConnectionTest extends PredisConnectionTestCase
|
||||
return RelayConnection::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
*/
|
||||
public function testThrowsExceptionOnInitializationCommandFailure(): void
|
||||
{
|
||||
$this->expectException('Predis\Connection\ConnectionException');
|
||||
$this->expectExceptionMessage('`SELECT` 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'])
|
||||
->setConstructorArgs([new Parameters()])
|
||||
->getMock();
|
||||
$connection
|
||||
->method('executeCommand')
|
||||
->with($cmdSelect)
|
||||
->willReturn(
|
||||
new ErrorResponse('ERR invalid DB index')
|
||||
);
|
||||
|
||||
$connection->method('createResource');
|
||||
|
||||
$connection->addConnectCommand($cmdSelect);
|
||||
$connection->connect();
|
||||
}
|
||||
|
||||
// ******************************************************************** //
|
||||
// ---- INTEGRATION TESTS --------------------------------------------- //
|
||||
// ******************************************************************** //
|
||||
@@ -68,7 +267,7 @@ class RelayConnectionTest extends PredisConnectionTestCase
|
||||
*/
|
||||
public function testGetResourceForcesConnection(): void
|
||||
{
|
||||
$connection = $this->createConnection();
|
||||
$connection = new RelayConnection(new Parameters(), new \Relay\Relay());
|
||||
|
||||
$this->assertFalse($connection->isConnected());
|
||||
$connection->getResource();
|
||||
@@ -77,158 +276,34 @@ class RelayConnectionTest extends PredisConnectionTestCase
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
* @group slow
|
||||
* @requires PHP 5.4
|
||||
*/
|
||||
public function testThrowsExceptionOnReadWriteTimeout(): void
|
||||
public function testExecutesCommand(): void
|
||||
{
|
||||
$this->expectException('Predis\Connection\ConnectionException');
|
||||
$connection = new RelayConnection(new Parameters(), new \Relay\Relay());
|
||||
|
||||
$connection = $this->createConnectionWithParams([
|
||||
'read_write_timeout' => 0.5,
|
||||
], true);
|
||||
$this->assertEquals(
|
||||
'OK',
|
||||
$connection->executeCommand(new RawCommand('SET', ['key', 'value']))
|
||||
);
|
||||
|
||||
$connection->executeCommand(
|
||||
$this->getCommandFactory()->create('brpop', ['foo', 3])
|
||||
$this->assertEquals(
|
||||
'value',
|
||||
$connection->executeCommand(new RawCommand('GET', ['key']))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @medium
|
||||
* @group connected
|
||||
* @group relay-incompatible
|
||||
* @requiresRedisVersion >= 7.2.0
|
||||
*/
|
||||
public function testThrowsExceptionOnProtocolDesynchronizationErrors(): void
|
||||
public function testConnectWithOnConnectionCommands(): void
|
||||
{
|
||||
$this->expectException('Predis\Protocol\ProtocolException');
|
||||
$connection = new RelayConnection(new Parameters(), new \Relay\Relay());
|
||||
$connection->addConnectCommand(new RawCommand('CLIENT', ['SETNAME', 'predis']));
|
||||
|
||||
$connection = $this->createConnection();
|
||||
$stream = $connection->getResource();
|
||||
$connection->connect();
|
||||
|
||||
$connection->writeRequest($this->getCommandFactory()->create('ping'));
|
||||
stream_socket_recvfrom($stream, 1);
|
||||
|
||||
$connection->read();
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
* @group relay-incompatible
|
||||
* @requires PHP 5.4
|
||||
*/
|
||||
public function testPersistentParameterWithFalseLikeValues(): void
|
||||
{
|
||||
$connection1 = $this->createConnectionWithParams(['persistent' => 0]);
|
||||
$this->assertNonPersistentConnection($connection1);
|
||||
|
||||
$connection2 = $this->createConnectionWithParams(['persistent' => false]);
|
||||
$this->assertNonPersistentConnection($connection2);
|
||||
|
||||
$connection3 = $this->createConnectionWithParams(['persistent' => '0']);
|
||||
$this->assertNonPersistentConnection($connection3);
|
||||
|
||||
$connection4 = $this->createConnectionWithParams(['persistent' => 'false']);
|
||||
$this->assertNonPersistentConnection($connection4);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
* @group relay-incompatible
|
||||
* @requires PHP 5.4
|
||||
*/
|
||||
public function testPersistentParameterWithTrueLikeValues(): void
|
||||
{
|
||||
$connection1 = $this->createConnectionWithParams(['persistent' => 1]);
|
||||
$this->assertPersistentConnection($connection1);
|
||||
|
||||
$connection2 = $this->createConnectionWithParams(['persistent' => true]);
|
||||
$this->assertPersistentConnection($connection2);
|
||||
|
||||
$connection3 = $this->createConnectionWithParams(['persistent' => '1']);
|
||||
$this->assertPersistentConnection($connection3);
|
||||
|
||||
$connection4 = $this->createConnectionWithParams(['persistent' => 'true']);
|
||||
$this->assertPersistentConnection($connection4);
|
||||
|
||||
$connection1->disconnect();
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
* @group relay-incompatible
|
||||
* @requires PHP 5.4
|
||||
*/
|
||||
public function testPersistentConnectionsToSameNodeShareResource(): void
|
||||
{
|
||||
$connection1 = $this->createConnectionWithParams(['persistent' => true]);
|
||||
$connection2 = $this->createConnectionWithParams(['persistent' => true]);
|
||||
|
||||
$this->assertPersistentConnection($connection1);
|
||||
$this->assertPersistentConnection($connection2);
|
||||
|
||||
$this->assertSame($connection1->getResource(), $connection2->getResource());
|
||||
|
||||
$connection1->disconnect();
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
* @group relay-incompatible
|
||||
* @requires PHP 5.4
|
||||
*/
|
||||
public function testPersistentConnectionsToSameNodeDoNotShareResourceUsingDifferentPersistentID(): void
|
||||
{
|
||||
$connection1 = $this->createConnectionWithParams(['persistent' => 'conn1']);
|
||||
$connection2 = $this->createConnectionWithParams(['persistent' => 'conn2']);
|
||||
|
||||
$this->assertPersistentConnection($connection1);
|
||||
$this->assertPersistentConnection($connection2);
|
||||
|
||||
$this->assertNotSame($connection1->getResource(), $connection2->getResource());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
* @group relay-incompatible
|
||||
*/
|
||||
public function testTcpNodelayParameterSetsContextFlagWhenTrue()
|
||||
{
|
||||
$connection = $this->createConnectionWithParams(['tcp_nodelay' => true]);
|
||||
$options = stream_context_get_options($connection->getResource());
|
||||
|
||||
$this->assertIsArray($options);
|
||||
$this->assertArrayHasKey('socket', $options);
|
||||
$this->assertArrayHasKey('tcp_nodelay', $options['socket']);
|
||||
$this->assertTrue($options['socket']['tcp_nodelay']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
* @group relay-incompatible
|
||||
*/
|
||||
public function testTcpNodelayParameterDoesNotSetContextFlagWhenFalse()
|
||||
{
|
||||
$connection = $this->createConnectionWithParams(['tcp_nodelay' => false]);
|
||||
$options = stream_context_get_options($connection->getResource());
|
||||
|
||||
$this->assertIsArray($options);
|
||||
$this->assertArrayHasKey('socket', $options);
|
||||
$this->assertArrayHasKey('tcp_nodelay', $options['socket']);
|
||||
$this->assertFalse($options['socket']['tcp_nodelay']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
* @group relay-incompatible
|
||||
*/
|
||||
public function testTcpDelayContextFlagIsNotSetByDefault()
|
||||
{
|
||||
$connection = $this->createConnectionWithParams([]);
|
||||
$options = stream_context_get_options($connection->getResource());
|
||||
|
||||
$this->assertIsArray($options);
|
||||
$this->assertArrayHasKey('socket', $options);
|
||||
$this->assertArrayHasKey('tcp_nodelay', $options['socket']);
|
||||
$this->assertFalse($options['socket']['tcp_nodelay']);
|
||||
$response = $connection->executeCommand(new RawCommand('CLIENT', ['GETNAME']));
|
||||
$this->assertSame('predis', $response);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user