Browse Source

Use custom ID when adding connection with no alias.

Daniele Alessandri 9 years ago
parent
commit
89a1e236ce

+ 1 - 1
src/Connection/Aggregate/MasterSlaveReplication.php

@@ -71,7 +71,7 @@ class MasterSlaveReplication implements ReplicationInterface
         if ($alias === 'master') {
             $this->master = $connection;
         } else {
-            $this->slaves[$alias ?: count($this->slaves)] = $connection;
+            $this->slaves[$alias ?: "slave-$connection"] = $connection;
         }
 
         $this->reset();

+ 16 - 0
tests/Predis/Connection/Aggregate/MasterSlaveReplicationTest.php

@@ -63,6 +63,22 @@ class MasterSlaveReplicationTest extends PredisTestCase
         $this->assertSame(array(), $replication->getSlaves());
     }
 
+    /**
+     * @group disconnected
+     */
+    public function testAddingConnectionsToReplicationWithoutAliasesResultsInCustomId()
+    {
+        $slave1 = $this->getMockConnection('tcp://host1');
+        $slave2 = $this->getMockConnection('tcp://host2:6380');
+
+        $replication = new MasterSlaveReplication();
+        $replication->add($slave1);
+        $replication->add($slave2);
+
+        $this->assertSame($slave1, $replication->getConnectionById('slave-host1:6379'));
+        $this->assertSame($slave2, $replication->getConnectionById('slave-host2:6380'));
+    }
+
     /**
      * @group disconnected
      * @expectedException \Predis\ClientException