Skip to content

Commit

Permalink
Merge pull request #39 from aflag/master
Browse files Browse the repository at this point in the history
Handle ConnectionError on Client.call
  • Loading branch information
thefab authored Jan 9, 2017
2 parents f62635b + efe1e28 commit 0e662e3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
4 changes: 3 additions & 1 deletion tests/test_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ def test_autoclose(self):

@tornado.testing.gen_test
def test_release_expired_client_disconnect(self):
with mock.patch.object(ClientPool, '_is_expired_client', return_value=True):
with mock.patch.object(ClientPool,
'_is_expired_client',
return_value=True):
c = ClientPool(max_size=5, client_timeout=60, autoclose=False)
client = yield c.get_connected_client()
self.assertTrue(client.is_connected())
Expand Down
18 changes: 17 additions & 1 deletion tests/test_pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from tornadis.pubsub import PubSubClient
from tornadis.client import Client
from support import test_redis_or_raise_skiptest
from support import test_redis_or_raise_skiptest, mock


class PubSubClientTestCase(tornado.testing.AsyncTestCase):
Expand Down Expand Up @@ -93,3 +93,19 @@ def test_empty_subscribe(self):
res = yield c.pubsub_subscribe()
self.assertFalse(res)
c.disconnect()

@tornado.testing.gen_test
def test_subscribe_no_redis(self):
c = PubSubClient()
with mock.patch.object(c, "is_connected", return_value=False):
res = yield c.pubsub_subscribe("foo")
self.assertFalse(res)
self.assertFalse(c.subscribed)

@tornado.testing.gen_test
def test_unsubscribe_no_redis(self):
c = PubSubClient()
yield c.pubsub_subscribe("foo")
with mock.patch.object(c, "is_connected", return_value=False):
res = yield c.pubsub_unsubscribe("foo")
self.assertFalse(res)
4 changes: 4 additions & 0 deletions tornadis/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ def _pubsub_subscribe(self, command, *args):
raise tornado.gen.Return(False)
results = yield Client.call(self, command, *args,
__multiple_replies=len(args))
if isinstance(results, ConnectionError):
raise tornado.gen.Return(False)
for reply in results:
if isinstance(reply, ConnectionError) or len(reply) != 3 or \
reply[0].lower() != command.lower() or reply[2] == 0:
Expand Down Expand Up @@ -124,6 +126,8 @@ def _pubsub_unsubscribe(self, command, *args):
args_len = len(args)
results = yield Client.call(self, command, *args,
__multiple_replies=args_len)
if isinstance(results, ConnectionError):
raise tornado.gen.Return(False)
for reply in results:
if isinstance(reply, ConnectionError) or len(reply) != 3 or \
reply[0].lower() != command.lower():
Expand Down

0 comments on commit 0e662e3

Please sign in to comment.