def test_cluster_slots_error_expected_responseerror(): """ Check that exception is not raised if initialize can't execute 'CLUSTER SLOTS' command but can hit other nodes. """ with patch.object(Redis, 'execute_command') as execute_command_mock: execute_command_mock.side_effect = ResponseError("MASTERDOWN") with pytest.raises(RedisClusterException): n = NodeManager(startup_nodes=[ { "host": "127.0.0.1", "port": 7000 }, ]) n.initialize() try: n = NodeManager(startup_nodes=[ { "host": "127.0.0.1", "port": 7000 }, ]) n.initialize() except RedisClusterException as e: assert "Redis Cluster cannot be connected" in e.args[0]
def _assert_dict(self, val): if val is None: return {} if isinstance(val, dict): return val else: raise ResponseError("Operation against a key holding the wrong kind of value")
def _assert_list(self, val): if val is None: return [] if isinstance(val, (list, tuple)): return val else: raise ResponseError("Operation against a key holding the wrong kind of value")
def _assert_str(self, val): if val is None: return None if isinstance(val, str): return val elif isinstance(val, bytes): return val else: raise ResponseError("Operation against a key holding the wrong kind of value")
def _assert_str(self, val): if val is None: return None if isinstance(val, str): return val elif isinstance(val, unicode): return val.decode(self._charset, self._errors) else: raise ResponseError( "Operation against a key holding the wrong kind of value")
def _assert_int(self, val): if val is None: return 0 try: val = int(val) except ValueError: raise ResponseError("ResponseError: value is not an integer or out of range") else: return val
def __init__(self, host='127.0.0.1', port=6379, key='redis', extra_fields={}, flush_threshold=128, flush_time=1, level=NOTSET, filter=None, password=False, bubble=True, context=None, push_method='rpush'): Handler.__init__(self, level, filter, bubble) try: import redis from redis import ResponseError except ImportError: raise RuntimeError('The redis library is required for ' 'the RedisHandler') self.redis = redis.Redis(host=host, port=port, password=password, decode_responses=True) try: self.redis.ping() except ResponseError: raise ResponseError( 'The password provided is apparently incorrect') self.key = key self.extra_fields = extra_fields self.flush_threshold = flush_threshold self.queue = [] self.lock = Lock() self.push_method = push_method # Set up a thread that flushes the queue every specified seconds self._stop_event = threading.Event() self._flushing_t = threading.Thread(target=self._flush_task, args=(flush_time, self._stop_event)) self._flushing_t.daemon = True self._flushing_t.start()
def _hdel(name, *keys): with self._lock.writer(): # Emulate Redis < 2.4 for now # TODO: Behavior based on _server_verison name = self._to_str(name) if len(keys) != 1: # When no keys are passed emulate an error # returned from the server. raise ResponseError("wrong number of arguments for 'hdel' command") val = self._assert_dict(self._cache.get(name, None)) deleted_count = 0 for k in keys: k = self._to_str(k) if k in val: deleted_count+=1 del val[k] # Emulate Redis < 2.4 for now return deleted_count > 0
def test_queue_length(self, mocker): redis_client_mock = mocker.MagicMock() redis_client_mock.keys.return_value = [ b'_kombu.binding.celery', b'_kombu.binding.myqueue', b'_kombu.binding.sometechincalstuff', ] redis_client_mock.llen.side_effect = [1, 2, ResponseError(), 3, 4] monitor = QueueLenghtMonitoringThread(redis_client_mock) monitor.update_lengths() assert redis_client_mock.llen.call_count == 3 assert self.get_queue_len('celery') == 1 assert self.get_queue_len('myqueue') == 2 assert self.get_queue_len('sometechincalstuff') is None monitor.update_lengths() assert redis_client_mock.llen.call_count == 5 assert self.get_queue_len('celery') == 3 assert self.get_queue_len('myqueue') == 4 assert self.get_queue_len('sometechincalstuff') is None