Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when using global_keyprefix with a sentinel connection #1838

Merged
merged 5 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion kombu/transport/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1429,7 +1429,7 @@ def _sentinel_managed_pool(self, asynchronous=False):

return sentinel_inst.master_for(
master_name,
self.Client,
redis.StrictRedis,
adam-homeboost marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StrictRedis is deprecated.
Better use Redis instead.

).connection_pool

def _get_pool(self, asynchronous=False):
Expand Down
34 changes: 34 additions & 0 deletions t/unit/transport/test_redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1734,6 +1734,40 @@ 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

connection = Connection(
'sentinel://localhost:65534/',
transport_options={
'global_keyprefix': 'some_prefix',
'master_name': 'not_important',
},
)
with pytest.raises(ConnectionError):
connection.channel()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we consider one more approach here:

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()  # Ensure connection is closed even if an exception occurs

Should we consider testing potential exceptions that could occur during connection creation??

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@50-Course

These tests are inside a test class. Try:

pytest t/unit/transport/test_redis.py::test_RedisSentinel::test_can_create_connection_with_global_keyprefix

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@50-Course

These tests are inside a test class. Try:

pytest t/unit/transport/test_redis.py::test_RedisSentinel::test_can_create_connection_with_global_keyprefix

class test_Redis:

    def setup(self):
        self.connection = Connection(transport=Transport)
        self.exchange = Exchange('test_Redis', type='direct')
        self.queue = Queue('test_Redis', self.exchange, 'test_Redis')

    def teardown(self):
        self.connection.close()

If I am not mistaken it means it does not apply to your connection obj so I agree with a safer approach like @50-Course proposed.

Also, reviewing a sample of the other tests shows most tests have consistent resource cleanup manually, so it would make sense to apply it here as well.


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'
)


class test_GlobalKeyPrefixMixin:

Expand Down