From 159e9df4307b0540a27a1a57e89dbfc194401889 Mon Sep 17 00:00:00 2001 From: Adam Sussman <127809317+adam-homeboost@users.noreply.github.com> Date: Tue, 9 Jan 2024 00:28:22 -0800 Subject: [PATCH] Fix crash when using global_keyprefix with a sentinel connection (#1838) * Fix partial closure error when instantiating redis pool for a global_keyprefix sentinel connection * add tests for redis sentinel connection with global_keyprefix setting * StrictRedis is deprecated, use Redis class * add some resource cleanup * add some resource cleanup --- kombu/transport/redis.py | 2 +- t/unit/transport/test_redis.py | 38 ++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/kombu/transport/redis.py b/kombu/transport/redis.py index b6fa101d9..912b0fe0e 100644 --- a/kombu/transport/redis.py +++ b/kombu/transport/redis.py @@ -1429,7 +1429,7 @@ def _sentinel_managed_pool(self, asynchronous=False): return sentinel_inst.master_for( master_name, - self.Client, + redis.Redis, ).connection_pool def _get_pool(self, asynchronous=False): diff --git a/t/unit/transport/test_redis.py b/t/unit/transport/test_redis.py index ecbed7eb0..993ae445d 100644 --- a/t/unit/transport/test_redis.py +++ b/t/unit/transport/test_redis.py @@ -1734,6 +1734,44 @@ def test_sentinel_with_ssl(self): assert (params['connection_class'] is SentinelManagedSSLConnection) + def test_can_create_connection_with_global_keyprefix(self): + from redis.exceptions import ConnectionError + + try: + connection = Connection( + 'sentinel://localhost:65534/', + transport_options={ + 'global_keyprefix': 'some_prefix', + 'master_name': 'not_important', + }, + ) + with pytest.raises(ConnectionError): + connection.channel() + finally: + connection.close() + + def test_can_create_correct_mixin_with_global_keyprefix(self): + from kombu.transport.redis import GlobalKeyPrefixMixin + + with patch('redis.sentinel.Sentinel'): + connection = Connection( + 'sentinel://localhost:65534/', + transport_options={ + 'global_keyprefix': 'some_prefix', + 'master_name': 'not_important', + }, + ) + + assert isinstance( + connection.channel().client, + GlobalKeyPrefixMixin + ) + assert ( + connection.channel().client.global_keyprefix + == 'some_prefix' + ) + connection.close() + class test_GlobalKeyPrefixMixin: