diff options
author | Andy McCurdy <andy@andymccurdy.com> | 2013-11-26 18:44:09 -0800 |
---|---|---|
committer | Andy McCurdy <andy@andymccurdy.com> | 2013-11-26 18:44:09 -0800 |
commit | f946f468bc516eccbebeebe805d8c9b602d97e9f (patch) | |
tree | 8a9c2f2594e4aa5b25045cea0ea83ee939dbdd4c | |
parent | c93acfdc0496761f2b68598e3cc80bf3ef464b10 (diff) | |
parent | 64827106e5233e54ee43c2b1e475c9270534f197 (diff) | |
download | redis-py-f946f468bc516eccbebeebe805d8c9b602d97e9f.tar.gz |
Merge branch 'pr/399'
-rw-r--r-- | redis/client.py | 3 | ||||
-rw-r--r-- | redis/connection.py | 28 | ||||
-rw-r--r-- | tests/test_connection_pool.py | 53 |
3 files changed, 83 insertions, 1 deletions
diff --git a/redis/client.py b/redis/client.py index 675c784..0c898bd 100644 --- a/redis/client.py +++ b/redis/client.py @@ -368,6 +368,9 @@ class StrictRedis(object): self.response_callbacks = self.__class__.RESPONSE_CALLBACKS.copy() + def __repr__(self): + return "%s<%s>" % (type(self).__name__, repr(self.connection_pool)) + def set_response_callback(self, command, callback): "Set a custom Response Callback" self.response_callbacks[command] = callback diff --git a/redis/connection.py b/redis/connection.py index 3205414..03d9d2b 100644 --- a/redis/connection.py +++ b/redis/connection.py @@ -204,6 +204,8 @@ else: class Connection(object): "Manages TCP communication to and from a Redis server" + description_format = "Connection<host=%(host)s,port=%(port)s,db=%(db)s>" + def __init__(self, host='localhost', port=6379, db=0, password=None, socket_timeout=None, encoding='utf-8', encoding_errors='strict', decode_responses=False, @@ -219,6 +221,14 @@ class Connection(object): self.decode_responses = decode_responses self._sock = None self._parser = parser_class() + self._description_args = { + 'host': self.host, + 'port': self.port, + 'db': self.db, + } + + def __repr__(self): + return self.description_format % self._description_args def __del__(self): try: @@ -346,6 +356,8 @@ class Connection(object): class UnixDomainSocketConnection(Connection): + description_format = "UnixDomainSocketConnection<path=%(path)s,db=%(db)s>" + def __init__(self, path='', db=0, password=None, socket_timeout=None, encoding='utf-8', encoding_errors='strict', decode_responses=False, @@ -360,6 +372,10 @@ class UnixDomainSocketConnection(Connection): self.decode_responses = decode_responses self._sock = None self._parser = parser_class() + self._description_args = { + 'path': self.path, + 'db': self.db, + } def _connect(self): "Create a Unix domain socket connection" @@ -392,6 +408,12 @@ class ConnectionPool(object): self._available_connections = [] self._in_use_connections = set() + def __repr__(self): + return "%s<%s>" % ( + type(self).__name__, + self.connection_class.description_format % self.connection_kwargs, + ) + def _checkpid(self): if self.pid != os.getpid(): self.disconnect() @@ -501,6 +523,12 @@ class BlockingConnectionPool(object): # disconnect them later. self._connections = [] + def __repr__(self): + return "%s<%s>" % ( + type(self).__name__, + self.connection_class.description_format % self.connection_kwargs, + ) + def _checkpid(self): """ Check the current process id. If it has changed, disconnect and diff --git a/tests/test_connection_pool.py b/tests/test_connection_pool.py index 64b4deb..80deb64 100644 --- a/tests/test_connection_pool.py +++ b/tests/test_connection_pool.py @@ -3,12 +3,15 @@ import os import pytest import redis import time +import re from threading import Thread from redis._compat import Queue class DummyConnection(object): + description_format = "DummyConnection<>" + def __init__(self, **kwargs): self.kwargs = kwargs self.pid = os.getpid() @@ -48,6 +51,28 @@ class TestConnectionPoolCase(object): c2 = pool.get_connection('_') assert c1 == c2 + def test_repr_contains_db_info_tcp(self): + pool = redis.ConnectionPool(host='localhost', port=6379, db=0) + + assert re.match('(.*)<(.*)<(.*)>>', repr(pool)).groups() == ( + 'ConnectionPool', + 'Connection', + 'host=localhost,port=6379,db=0', + ) + + def test_repr_contains_db_info_unix(self): + pool = redis.ConnectionPool( + connection_class=redis.UnixDomainSocketConnection, + path='abc', + db=0, + ) + + assert re.match('(.*)<(.*)<(.*)>>', repr(pool)).groups() == ( + 'ConnectionPool', + 'UnixDomainSocketConnection', + 'path=abc,db=0', + ) + class TestBlockingConnectionPool(object): def get_pool(self, connection_info=None, max_connections=10, timeout=20): @@ -107,9 +132,35 @@ class TestBlockingConnectionPool(object): c2 = pool.get_connection('_') assert c1 == c2 + def test_repr_contains_db_info_tcp(self): + pool = redis.BlockingConnectionPool( + host='localhost', + port=6379, + db=0, + ) + + assert re.match('(.*)<(.*)<(.*)>>', repr(pool)).groups() == ( + 'BlockingConnectionPool', + 'Connection', + 'host=localhost,port=6379,db=0', + ) + + def test_repr_contains_db_info_unix(self): + pool = redis.BlockingConnectionPool( + connection_class=redis.UnixDomainSocketConnection, + path='abc', + db=0 + ) + + assert re.match('(.*)<(.*)<(.*)>>', repr(pool)).groups() == ( + 'BlockingConnectionPool', + 'UnixDomainSocketConnection', + 'path=abc,db=0', + ) + class TestConnection(object): - def test_on_connect_error(self, r): + def test_on_connect_error(self): """ An error in Connection.on_connect should disconnect from the server see for details: https://github.com/andymccurdy/redis-py/issues/368 |