def backend_layer(self): # type: () -> BaseRedisClient if self._backend_layer is None: cache_key = (self.backend_type, dict_to_hashable(cast(Dict[Hashable, Any], self.backend_layer_kwargs))) if cache_key not in self._backend_layer_cache: with self._get_timer('backend.initialize'): backend_layer_kwargs = deepcopy(self.backend_layer_kwargs) if self.backend_type == REDIS_BACKEND_TYPE_SENTINEL: self._backend_layer_cache[cache_key] = SentinelRedisClient(**backend_layer_kwargs) else: self._backend_layer_cache[cache_key] = StandardRedisClient(**backend_layer_kwargs) self._backend_layer = self._backend_layer_cache[cache_key] # Each time the backend layer is accessed, use _this_ transport's metrics recorder for the backend layer self._backend_layer.metrics_counter_getter = self._get_counter return self._backend_layer
def test_no_hosts_yields_single_default_host(self): client = StandardRedisClient() payload = {'test': 'test_no_hosts_yields_single_default_host'} client.send_message_to_queue( queue_key='test_no_hosts_yields_single_default_host', message=msgpack.packb(payload, use_bin_type=True), expiry=10, capacity=10, connection=client.get_connection('test_no_hosts_yields_single_default_host'), ) message = client.get_connection( 'test_no_hosts_yields_single_default_host', ).lpop('test_no_hosts_yields_single_default_host') self.assertIsNotNone(message) self.assertEqual(payload, msgpack.unpackb(message, raw=False))
def test_string_host_yields_single_host(self): client = StandardRedisClient(hosts=['redis://localhost:1234/0']) payload = {'test': 'test_string_host_yields_single_host'} client.send_message_to_queue( queue_key='test_string_host_yields_single_host', message=msgpack.packb(payload), expiry=10, capacity=10, connection=client.get_connection('test_string_host_yields_single_host'), ) message = client.get_connection( 'test_string_host_yields_single_host', ).lpop('test_string_host_yields_single_host') self.assertIsNotNone(message) self.assertEqual(payload, msgpack.unpackb(message, encoding='utf-8'))
def test_string_host_yields_single_host(self): with warnings.catch_warnings(record=True) as w: client = StandardRedisClient(hosts=['redis://localhost:1234/0']) assert len(w) == 1 issubclass(w[0].category, DeprecationWarning) assert 'Redis host syntax is deprecated' in str(w[0].message) payload = {'test': 'test_string_host_yields_single_host'} client.send_message_to_queue( queue_key='test_string_host_yields_single_host', message=msgpack.packb(payload, use_bin_type=True), expiry=10, capacity=10, connection=client.get_connection('test_string_host_yields_single_host'), ) message = client.get_connection( 'test_string_host_yields_single_host', ).lpop('test_string_host_yields_single_host') self.assertIsNotNone(message) self.assertEqual(payload, msgpack.unpackb(message, raw=False))
def test_invalid_hosts(self): with self.assertRaises(ValueError): StandardRedisClient(hosts='redis://localhost:1234/0')
def _set_up_client(**kwargs): return StandardRedisClient( hosts=[('169.254.7.12', 6379), ('169.254.8.12', 6379), ('169.254.9.12', 6379)], **kwargs )