diff --git a/gcloud/pubsub/test_topic.py b/gcloud/pubsub/test_topic.py index 7bce0e3c3198..85f8d7ad7ceb 100644 --- a/gcloud/pubsub/test_topic.py +++ b/gcloud/pubsub/test_topic.py @@ -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' @@ -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 @@ -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, diff --git a/gcloud/pubsub/topic.py b/gcloud/pubsub/topic.py index 6169ada84911..568434789ac4 100644 --- a/gcloud/pubsub/topic.py +++ b/gcloud/pubsub/topic.py @@ -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