Exemplo n.º 1
0
 def test_get_slave(self):
     self.shuffle_mock.return_value = [(b'127.0.0.1', 12345),
                                       (b'127.0.0.2', 12345),
                                       (b'127.0.0.3', 12345)]
     pool = pyredis.pool.SentinelPool(sentinels=[('host1', 12345)],
                                      name='mymaster',
                                      slave_ok=True)
     pool._sentinel = Mock()
     pool._sentinel.get_slaves.return_value = [{
         b'ip': b'127.0.0.1',
         b'port': b'12345'
     }, {
         b'ip': b'127.0.0.2',
         b'port': b'12345'
     }, {
         b'ip': b'127.0.0.3',
         b'port': b'12345'
     }]
     client_mock1 = Mock()
     pool._get_client = Mock()
     pool._get_client.return_value = client_mock1
     client = pool._get_slave()
     self.shuffle_mock.assert_called_with([(b'127.0.0.1', 12345),
                                           (b'127.0.0.2', 12345),
                                           (b'127.0.0.3', 12345)])
     pool._get_client.assert_called_with(b'127.0.0.1', 12345)
     self.assertEqual(client, client_mock1)
Exemplo n.º 2
0
 def test_get_slave_stale_sentinel(self):
     self.shuffle_mock.return_value = [(b"127.0.0.1", 12345), (b"127.0.0.2", 12345), (b"127.0.0.3", 12345)]
     pool = pyredis.pool.SentinelPool(sentinels=[("host1", 12345)], name="mymaster", slave_ok=True)
     pool._sentinel = Mock()
     pool._sentinel.get_slaves.return_value = [
         {b"ip": b"127.0.0.1", b"port": b"12345"},
         {b"ip": b"127.0.0.2", b"port": b"12345"},
         {b"ip": b"127.0.0.3", b"port": b"12345"},
     ]
     client_mock1 = Mock()
     client_mock1.execute.return_value = b"blarg role:master blarg"
     client_mock2 = Mock()
     client_mock2.execute.return_value = b"blarg role:master blarg"
     client_mock3 = Mock()
     client_mock3.execute.return_value = b"blarg role:master blarg"
     pool._get_client = Mock()
     pool._get_client.side_effect = [client_mock1, client_mock2, client_mock3]
     client = pool._get_slave()
     self.shuffle_mock.assert_called_with([(b"127.0.0.1", 12345), (b"127.0.0.2", 12345), (b"127.0.0.3", 12345)])
     pool._get_client.assert_has_calls(
         [call(b"127.0.0.1", 12345), call(b"127.0.0.2", 12345), call(b"127.0.0.3", 12345)]
     )
     client_mock1.execute.assert_called_with("INFO", "replication")
     client_mock1.close.assert_called_with()
     client_mock2.execute.assert_called_with("INFO", "replication")
     client_mock2.close.assert_called_with()
     client_mock3.execute.assert_called_with("INFO", "replication")
     client_mock3.close.assert_called_with()
     self.assertIsNone(client)
     pool._sentinel.next_sentinel.assert_called_with()
Exemplo n.º 3
0
 def test_get_slave_stale_sentinel(self):
     self.shuffle_mock.return_value = [
         (b'127.0.0.1', 12345),
         (b'127.0.0.2', 12345),
         (b'127.0.0.3', 12345)
     ]
     pool = pyredis.pool.SentinelPool(sentinels=[('host1', 12345)], name='mymaster', slave_ok=True)
     pool._sentinel = Mock()
     pool._sentinel.get_slaves.return_value = [
         {
             b'ip': b'127.0.0.1',
             b'port': b'12345'
         },
         {
             b'ip': b'127.0.0.2',
             b'port': b'12345'
         },
         {
             b'ip': b'127.0.0.3',
             b'port': b'12345'
         }
     ]
     client_mock1 = Mock()
     client_mock1.execute.return_value = b'blarg role:master blarg'
     client_mock2 = Mock()
     client_mock2.execute.return_value = b'blarg role:master blarg'
     client_mock3 = Mock()
     client_mock3.execute.return_value = b'blarg role:master blarg'
     pool._get_client = Mock()
     pool._get_client.side_effect = [
         client_mock1,
         client_mock2,
         client_mock3
     ]
     client = pool._get_slave()
     self.shuffle_mock.assert_called_with(
         [
             (b'127.0.0.1', 12345),
             (b'127.0.0.2', 12345),
             (b'127.0.0.3', 12345)
         ]
     )
     pool._get_client.assert_has_calls([
         call(b'127.0.0.1', 12345),
         call(b'127.0.0.2', 12345),
         call(b'127.0.0.3', 12345)
     ])
     client_mock1.execute.assert_called_with('INFO', 'replication')
     client_mock1.close.assert_called_with()
     client_mock2.execute.assert_called_with('INFO', 'replication')
     client_mock2.close.assert_called_with()
     client_mock3.execute.assert_called_with('INFO', 'replication')
     client_mock3.close.assert_called_with()
     self.assertIsNone(client)
     pool._sentinel.next_sentinel.assert_called_with()
Exemplo n.º 4
0
 def test_get_slave(self):
     self.shuffle_mock.return_value = [(b"127.0.0.1", 12345), (b"127.0.0.2", 12345), (b"127.0.0.3", 12345)]
     pool = pyredis.pool.SentinelPool(sentinels=[("host1", 12345)], name="mymaster", slave_ok=True)
     pool._sentinel = Mock()
     pool._sentinel.get_slaves.return_value = [
         {b"ip": b"127.0.0.1", b"port": b"12345"},
         {b"ip": b"127.0.0.2", b"port": b"12345"},
         {b"ip": b"127.0.0.3", b"port": b"12345"},
     ]
     client_mock1 = Mock()
     client_mock1.execute.return_value = b"blarg role:slave blarg"
     pool._get_client = Mock()
     pool._get_client.return_value = client_mock1
     client = pool._get_slave()
     self.shuffle_mock.assert_called_with([(b"127.0.0.1", 12345), (b"127.0.0.2", 12345), (b"127.0.0.3", 12345)])
     pool._get_client.assert_called_with(b"127.0.0.1", 12345)
     client_mock1.execute.assert_called_with("INFO", "replication")
     self.assertEqual(client, client_mock1)