mirror of
https://github.com/predis/predis.git
synced 2026-10-03 18:39:30 +00:00
Fix client iteration with master/slave and Sentinel replication (#1730)
* Fix client iteration with master/slave replication * Fix client iteration with Sentinel replication --------- Co-authored-by: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com>
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
- Added optional `CELL_SIZE` argument for `CMS.INITBYDIM` and `CMS.INITBYPROB`
|
||||
### Changed
|
||||
### Fixed
|
||||
- Fixed client iteration with master/slave and Sentinel replication (#923)
|
||||
|
||||
## v3.6.1 (2026-09-17)
|
||||
### Fixed
|
||||
|
||||
@@ -588,6 +588,14 @@ class MasterSlaveReplication extends AbstractAggregateConnection implements Repl
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return implode(',', array_keys($this->pool));
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle connection exception.
|
||||
*
|
||||
|
||||
@@ -844,4 +844,12 @@ class SentinelReplication extends AbstractAggregateConnection implements Replica
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return implode(',', array_keys($this->pool));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
namespace Predis\Connection\Replication;
|
||||
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Predis\Client;
|
||||
use Predis\Command;
|
||||
use Predis\Connection;
|
||||
use Predis\Connection\Parameters;
|
||||
@@ -28,6 +29,25 @@ use RuntimeException;
|
||||
|
||||
class MasterSlaveReplicationTest extends PredisTestCase
|
||||
{
|
||||
/**
|
||||
* @group disconnected
|
||||
*/
|
||||
public function testClientIterationKeepsReplicationConnection(): void
|
||||
{
|
||||
$client = new Client([
|
||||
'tcp://127.0.0.1:6379?role=master',
|
||||
'tcp://127.0.0.1:6380?role=slave',
|
||||
], ['replication' => 'predis']);
|
||||
|
||||
$iterator = $client->getIterator();
|
||||
|
||||
$this->assertCount(1, $iterator);
|
||||
$this->assertSame('127.0.0.1:6379,127.0.0.1:6380', $iterator->key());
|
||||
$this->assertSame($client->getConnection(), $iterator->current()->getConnection());
|
||||
$this->assertSame($client->getOptions(), $iterator->current()->getOptions());
|
||||
$this->assertFalse($client->isConnected());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
*/
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
namespace Predis\Connection\Replication;
|
||||
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Predis\Client;
|
||||
use Predis\Command;
|
||||
use Predis\Connection;
|
||||
use Predis\Connection\Parameters;
|
||||
@@ -31,6 +32,47 @@ use RuntimeException;
|
||||
|
||||
class SentinelReplicationTest extends PredisTestCase
|
||||
{
|
||||
/**
|
||||
* @group disconnected
|
||||
*/
|
||||
public function testClientIterationKeepsReplicationConnectionBeforeDiscovery(): void
|
||||
{
|
||||
$factory = $this->getMockBuilder(Connection\FactoryInterface::class)->getMock();
|
||||
$factory->expects($this->never())->method('create');
|
||||
|
||||
$client = new Client([
|
||||
'tcp://127.0.0.1:26379',
|
||||
'tcp://127.0.0.1:26380',
|
||||
], ['replication' => 'sentinel', 'service' => 'svc', 'connections' => $factory]);
|
||||
|
||||
$iterator = $client->getIterator();
|
||||
|
||||
$this->assertCount(1, $iterator);
|
||||
$this->assertSame('', $iterator->key());
|
||||
$this->assertSame($client->getConnection(), $iterator->current()->getConnection());
|
||||
$this->assertSame($client->getOptions(), $iterator->current()->getOptions());
|
||||
$this->assertFalse($client->isConnected());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
*/
|
||||
public function testClientIterationKeepsReplicationConnectionWithKnownNodes(): void
|
||||
{
|
||||
$replication = $this->getReplicationConnection('svc', []);
|
||||
$replication->add(new StreamConnection(Parameters::create('tcp://127.0.0.1:6379?role=master')));
|
||||
$replication->add(new StreamConnection(Parameters::create('tcp://127.0.0.1:6380?role=slave')));
|
||||
|
||||
$client = new Client($replication);
|
||||
$iterator = $client->getIterator();
|
||||
|
||||
$this->assertCount(1, $iterator);
|
||||
$this->assertSame('127.0.0.1:6379,127.0.0.1:6380', $iterator->key());
|
||||
$this->assertSame($replication, $iterator->current()->getConnection());
|
||||
$this->assertSame($client->getOptions(), $iterator->current()->getOptions());
|
||||
$this->assertFalse($client->isConnected());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user