diff options
author | Andy McCurdy <andy@andymccurdy.com> | 2016-06-14 13:43:43 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-06-14 13:43:43 -0400 |
commit | 98e1133422b8514656e244ae5d332e65aa97124e (patch) | |
tree | 6280543cc5d1fd4b9fdb24528183f76b06aa92cb | |
parent | 3bdf81ab41464d8e918080234912fc8fd86629af (diff) | |
parent | 86ec8d0458e39c795321348f189457c52ebcceca (diff) | |
download | redis-py-98e1133422b8514656e244ae5d332e65aa97124e.tar.gz |
Merge pull request #752 from gward/issue716
pubsub: improve error reporting if caller forgets to subscribe (issue #716)
-rwxr-xr-x | redis/client.py | 4 | ||||
-rw-r--r-- | tests/test_pubsub.py | 8 |
2 files changed, 12 insertions, 0 deletions
diff --git a/redis/client.py b/redis/client.py index d6bd5c3..d84c8b3 100755 --- a/redis/client.py +++ b/redis/client.py @@ -2226,6 +2226,10 @@ class PubSub(object): def parse_response(self, block=True, timeout=0): "Parse the response from a publish/subscribe command" connection = self.connection + if connection is None: + raise RuntimeError( + 'pubsub connection not set: ' + 'did you forget to call subscribe() or psubscribe()?') if not block and not connection.can_read(timeout=timeout): return None return self._execute(connection, connection.read_response) diff --git a/tests/test_pubsub.py b/tests/test_pubsub.py index 5486b75..dacac83 100644 --- a/tests/test_pubsub.py +++ b/tests/test_pubsub.py @@ -283,6 +283,14 @@ class TestPubSubMessages(object): assert self.message == make_message('pmessage', channel, 'test message', pattern=pattern) + def test_get_message_without_subscribe(self, r): + p = r.pubsub() + with pytest.raises(RuntimeError) as info: + p.get_message() + expect = ('connection not set: ' + 'did you forget to call subscribe() or psubscribe()?') + assert expect in info.exconly() + class TestPubSubAutoDecoding(object): "These tests only validate that we get unicode values back" |