Skip to content

Commit

Permalink
Merge pull request #901 from tseaver/899-pubsub-subscription_pull_wo_…
Browse files Browse the repository at this point in the history
…receivedMessages

#899: Harden 'Subscription.pull' against missing 'receivedMessages' in response
  • Loading branch information
tseaver committed May 29, 2015
2 parents 6bc06ce + b49a7c6 commit c28fce8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gcloud/pubsub/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def pull(self, return_immediately=False, max_messages=1, connection=None):
path='%s:pull' % self.path,
data=data)
return [(info['ackId'], Message.from_api_repr(info['message']))
for info in response['receivedMessages']]
for info in response.get('receivedMessages', ())]

def acknowledge(self, ack_ids, connection=None):
"""API call: acknowledge retrieved messages for the subscription.
Expand Down
17 changes: 17 additions & 0 deletions gcloud/pubsub/test_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,23 @@ def test_pull_w_return_immediately_w_max_messages_w_explicit_conn(self):
self.assertEqual(req['data'],
{'returnImmediately': True, 'maxMessages': 3})

def test_pull_wo_receivedMessages(self):
PROJECT = 'PROJECT'
SUB_NAME = 'sub_name'
SUB_PATH = 'projects/%s/subscriptions/%s' % (PROJECT, SUB_NAME)
TOPIC_NAME = 'topic_name'
conn = _Connection({})
topic = _Topic(TOPIC_NAME, project=PROJECT)
subscription = self._makeOne(SUB_NAME, topic)
pulled = subscription.pull(return_immediately=False, connection=conn)
self.assertEqual(len(pulled), 0)
self.assertEqual(len(conn._requested), 1)
req = conn._requested[0]
self.assertEqual(req['method'], 'POST')
self.assertEqual(req['path'], '/%s:pull' % SUB_PATH)
self.assertEqual(req['data'],
{'returnImmediately': False, 'maxMessages': 1})

def test_acknowledge_w_implicit_connection(self):
from gcloud.pubsub._testing import _monkey_defaults
PROJECT = 'PROJECT'
Expand Down

0 comments on commit c28fce8

Please sign in to comment.