Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_protocol_error_with_custom_class(self):
self.reader = hiredis.Reader(protocolError=RuntimeError)
self.reader.feed(b"x")
self.assertRaises(RuntimeError, self.reply)
def setUp(self):
self.reader = hiredis.Reader()
def test_bulk_string_with_invalid_encoding(self):
self.reader = hiredis.Reader(encoding="unknown")
self.reader.feed(b"$5\r\nhello\r\n")
self.assertRaises(LookupError, self.reply)
def test_execute_ReplyError_to_many_retries(self):
self.client._get_slot_info = Mock()
self.client._get_slot_info.side_effect = [
'host1_12345',
'host2_12345',
'host3_12345',
'host4_12345'
]
conn1 = Mock()
conn1.read.side_effect = [ReplyError('MOVED 42 host2:12345')]
conn2 = Mock()
conn2.read.side_effect = [ReplyError('MOVED 42 host2:12345')]
conn3 = Mock()
conn3.read.side_effect = [ReplyError('MOVED 42 host2:12345')]
conn4 = Mock()
conn4.read.side_effect = [ReplyError('MOVED 42 host2:12345')]
self.connection_mock.side_effect = [conn1, conn2, conn3, conn4]
self.client._cleanup_conns = Mock()
self.assertRaises(PyRedisError, self.client.execute, 'GET', 'test', shard_key='test')
conn1.write.assert_called_with('GET', 'test')
conn2.write.assert_called_with('GET', 'test')
conn3.write.assert_called_with('GET', 'test')
self.assertFalse(conn4.write.called)
def test_execute_ReplyError_ASK(self):
self.client._get_slot_info = Mock()
self.client._get_slot_info.return_value = 'host1_12345'
conn1 = Mock()
conn1.read.side_effect = [ReplyError('ASK 42 host2:12345')]
conn2 = Mock()
conn2.read.side_effect = ['success']
self.connection_mock.side_effect = [conn1, conn2]
result = self.client.execute('GET', 'test', shard_key='test')
self.assertEqual(result, 'success')
conn1.write.assert_called_with('GET', 'test')
conn2.write.assert_called_with('ASKING', 'GET', 'test')
def test_execute_ReplyError_MOVED(self):
self.client._get_slot_info = Mock()
self.client._get_slot_info.side_effect = ['host1_12345', 'host2_12345']
conn1 = Mock()
conn1.read.side_effect = [ReplyError('MOVED 42 host2:12345')]
conn2 = Mock()
conn2.read.side_effect = ['success']
self.connection_mock.side_effect = [conn1, conn2]
self.client._cleanup_conns = Mock()
id = self.client._map_id
result = self.client.execute('GET', 'test', shard_key='test')
self.assertEqual(result, 'success')
conn1.write.assert_called_with('GET', 'test')
conn2.write.assert_called_with('GET', 'test')
self.assertTrue(self.client._cleanup_conns.called)
self.clustermap_inst.update.assert_called_with(id)
def test_error_string(self):
self.reader.feed(b"-error\r\n")
error = self.reply()
self.assertEquals(hiredis.ReplyError, type(error))
self.assertEquals(("error",), error.args)
m = t.talk_raw(CMD_INFO)
cluster_enabled = PAT_CLUSTER_ENABLED.findall(m)
if len(cluster_enabled) == 0 or int(cluster_enabled[0]) == 0:
raise hiredis.ProtocolError(
'Node %s:%d is not cluster enabled' % (t.host, t.port))
m = t.talk_raw(CMD_CLUSTER_NODES)
if len(filter(None, m.split('\n'))) != 1:
raise hiredis.ProtocolError(
'Node %s:%d is already in a cluster' % (t.host, t.port))
m = t.talk_raw(CMD_CLUSTER_INFO)
cluster_state = PAT_CLUSTER_STATE.findall(m)
cluster_slot_assigned = PAT_CLUSTER_SLOT_ASSIGNED.findall(m)
if cluster_state[0] != 'fail' or int(cluster_slot_assigned[0]) != 0:
raise hiredis.ProtocolError(
'Node %s:%d is already in a cluster' % (t.host, t.port))
def _quit(_, cluster_id, host, port):
try:
me = redistrib.command.list_nodes(host, port, host)[1]
if len(me.assigned_slots) != 0:
raise ValueError('node still holding slots')
redistrib.command.quit_cluster(host, port)
except SocketError, e:
logging.exception(e)
logging.info('Remove instance from cluster on exception')
except ProtocolError, e:
if NOT_IN_CLUSTER_MESSAGE not in e.message:
raise
remove_empty_cluster(cluster_id)
n = get_node_by_host_port(host, port)
if n is not None:
n.assignee_id = None
db.session.add(n)
commit_session()
return True
def execute(self, task_map):
if self.start_time is None:
self.start_time = datetime.now()
db.session.add(self)
db.session.commit()
try:
if task_map[self.command](self, **self.args):
self.complete(None)
return True
except (ValueError, LookupError, IOError, SocketError, HiredisError,
ProtocolError, ReplyError, RedisStatusError):
self.complete(traceback.format_exc())
return False