Skip to content

Commit

Permalink
Fix connections.py: allow to connect to Redis using a Unix socket URL… (
Browse files Browse the repository at this point in the history
#392)

* Fix connections.py: allow to connect to Redis using a Unix socket URL without specifying the database number

* add tests

---------

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
  • Loading branch information
drygdryg and samuelcolvin committed Apr 1, 2024
1 parent 8fe4fc5 commit d4a37f3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
4 changes: 3 additions & 1 deletion arq/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ def from_dsn(cls, dsn: str) -> 'RedisSettings':
if query_db:
# e.g. redis://localhost:6379?db=1
database = int(query_db[0])
else:
elif conf.scheme != 'unix':
database = int(conf.path.lstrip('/')) if conf.path else 0
else:
database = 0
return RedisSettings(
host=conf.hostname or 'localhost',
port=conf.port or 6379,
Expand Down
56 changes: 56 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import re
import sys
from dataclasses import asdict
from datetime import timedelta

import pytest
Expand Down Expand Up @@ -197,3 +198,58 @@ def test_import_string_invalid_short():
def test_import_string_invalid_missing():
with pytest.raises(ImportError, match='Module "math" does not define a "foobar" attribute'):
arq.utils.import_string('math.foobar')


def test_settings_plain():
settings = RedisSettings()
assert asdict(settings) == {
'host': 'localhost',
'port': 6379,
'unix_socket_path': None,
'database': 0,
'username': None,
'password': None,
'ssl': False,
'ssl_keyfile': None,
'ssl_certfile': None,
'ssl_cert_reqs': 'required',
'ssl_ca_certs': None,
'ssl_ca_data': None,
'ssl_check_hostname': False,
'conn_timeout': 1,
'conn_retries': 5,
'conn_retry_delay': 1,
'sentinel': False,
'sentinel_master': 'mymaster',
'retry_on_timeout': False,
'retry_on_error': None,
'retry': None,
}


def test_settings_from_socket_dsn():
settings = RedisSettings.from_dsn('unix:///run/redis/redis.sock')
# insert_assert(asdict(settings))
assert asdict(settings) == {
'host': 'localhost',
'port': 6379,
'unix_socket_path': '/run/redis/redis.sock',
'database': 0,
'username': None,
'password': None,
'ssl': False,
'ssl_keyfile': None,
'ssl_certfile': None,
'ssl_cert_reqs': 'required',
'ssl_ca_certs': None,
'ssl_ca_data': None,
'ssl_check_hostname': False,
'conn_timeout': 1,
'conn_retries': 5,
'conn_retry_delay': 1,
'sentinel': False,
'sentinel_master': 'mymaster',
'retry_on_timeout': False,
'retry_on_error': None,
'retry': None,
}

0 comments on commit d4a37f3

Please sign in to comment.