Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pubsub context manager exit error #1956

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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