Fix: Redis driver does not fall back to default port

The Redis driver requires a port in the URI string read from the
Zaqar configuration. However, it is common practice to fall back
to a default port in connection URIs, so this patch implements
support for that in the spirit of the Principle of Least Suprise.

Change-Id: I3205890bbd10b49b1be59f4465617214881e6cc0
This commit is contained in:
kgriffs 2014-09-08 11:36:54 -05:00
parent e215385381
commit f319e7b053
2 changed files with 8 additions and 3 deletions

View File

@ -26,6 +26,11 @@ class TestUtils(testing.TestBase):
def test_can_connect_suceeds_if_good_uri_mongo(self):
self.assertTrue(utils.can_connect('mongodb://localhost:27017'))
@testing.requires_redis
def test_can_connect_suceeds_if_good_uri_redis(self):
self.assertTrue(utils.can_connect('redis://localhost'))
self.assertTrue(utils.can_connect('redis://localhost:6379'))
def test_can_connect_suceeds_if_good_uri_sqlite(self):
self.assertTrue(utils.can_connect('sqlite://:memory:'))

View File

@ -21,8 +21,8 @@ from zaqar.queues import storage
from zaqar.queues.storage.redis import controllers
from zaqar.queues.storage.redis import options
LOG = logging.getLogger(__name__)
REDIS_DEFAULT_PORT = 6379
def _get_redis_client(conf):
@ -30,8 +30,8 @@ def _get_redis_client(conf):
parsed_url = urllib.parse.urlparse(conf.uri)
if parsed_url.hostname:
return redis.StrictRedis(host=parsed_url.hostname,
port=parsed_url.port)
port = parsed_url.port or REDIS_DEFAULT_PORT
return redis.StrictRedis(host=parsed_url.hostname, port=port)
else:
return redis.StrictRedis(unix_socket_path=parsed_url.path)