Skip to content

Commit

Permalink
Merge pull request #1956 from daspecster/fix-pupsub-exit-context-no-c…
Browse files Browse the repository at this point in the history
…ommits

Fix pubsub context manager exit error
  • Loading branch information
daspecster authored Jul 4, 2016
2 parents 628ee9a + d49b048 commit eddc4e0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
14 changes: 14 additions & 0 deletions gcloud/pubsub/test_topic.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,18 @@ def test_publish_multiple_w_bound_client(self):
self.assertEqual(api._topic_published,
(self.TOPIC_PATH, [MESSAGE1, MESSAGE2]))

def test_publish_w_no_messages(self):
client = _Client(project=self.PROJECT)
api = client.publisher_api = _FauxPublisherAPI()
api._topic_publish_response = []
topic = self._makeOne(self.TOPIC_NAME, client=client)

with topic.batch() as batch:
pass

self.assertEqual(list(batch.messages), [])
self.assertEqual(api._api_called, 0)

def test_publish_multiple_w_alternate_client(self):
import base64
PAYLOAD1 = b'This is the first message text'
Expand Down Expand Up @@ -716,6 +728,7 @@ def test_context_mgr_failure(self):


class _FauxPublisherAPI(object):
_api_called = 0

def topic_create(self, topic_path):
self._topic_created = topic_path
Expand All @@ -735,6 +748,7 @@ def topic_delete(self, topic_path):

def topic_publish(self, topic_path, messages):
self._topic_published = topic_path, messages
self._api_called += 1
return self._topic_publish_response

def topic_list_subscriptions(self, topic_path, page_size=None,
Expand Down
3 changes: 3 additions & 0 deletions gcloud/pubsub/topic.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,9 @@ def commit(self, client=None):
:param client: the client to use. If not passed, falls back to the
``client`` stored on the current batch.
"""
if not self.messages:
return

if client is None:
client = self.client
api = client.publisher_api
Expand Down

0 comments on commit eddc4e0

Please sign in to comment.