Skip to content

Commit

Permalink
Merge pull request #2773 from daspecster/support-multiple-commits-if-…
Browse files Browse the repository at this point in the history
…failed-2771

Fix double encoding of messages when commit() fails.
  • Loading branch information
daspecster authored Nov 29, 2016
2 parents 8001d45 + 51d2c3f commit c40e0d4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
6 changes: 4 additions & 2 deletions pubsub/google/cloud/pubsub/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""Create / interact with Google Cloud Pub/Sub connections."""

import base64
import copy
import functools
import os

Expand Down Expand Up @@ -203,9 +204,10 @@ def topic_publish(self, topic_path, messages):
:rtype: list of string
:returns: list of opaque IDs for published messages.
"""
_transform_messages_base64(messages, _base64_unicode)
messages_to_send = copy.deepcopy(messages)
_transform_messages_base64(messages_to_send, _base64_unicode)
conn = self._connection
data = {'messages': messages}
data = {'messages': messages_to_send}
response = conn.api_request(
method='POST', path='/%s:publish' % (topic_path,), data=data)
return response['messageIds']
Expand Down
24 changes: 23 additions & 1 deletion pubsub/unit_tests/test__http.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,32 @@ def test_topic_publish_hit(self):
msg_data = connection._called_with['data']['messages'][0]['data']
self.assertEqual(msg_data, B64_PAYLOAD)

def test_topic_publish_twice(self):
import base64

PAYLOAD = b'This is the message text'
B64_PAYLOAD = base64.b64encode(PAYLOAD).decode('ascii')
MESSAGE = {'data': PAYLOAD, 'attributes': {}}
RETURNED = {'messageIds': []}
connection = _Connection(RETURNED, RETURNED)
client = _Client(connection, self.PROJECT)
api = self._make_one(client)

api.topic_publish(self.TOPIC_PATH, [MESSAGE])
api.topic_publish(self.TOPIC_PATH, [MESSAGE])

messages = connection._called_with['data']['messages']
self.assertEqual(len(messages), 1)
self.assertEqual(messages[0]['data'], B64_PAYLOAD)

def test_topic_publish_miss(self):
import base64
from google.cloud.exceptions import NotFound

PAYLOAD = b'This is the message text'
B64_PAYLOAD = base64.b64encode(PAYLOAD).decode('ascii')
MESSAGE = {'data': PAYLOAD, 'attributes': {}}
B64MSG = {'data': B64_PAYLOAD, 'attributes': {}}
connection = _Connection()
client = _Client(connection, self.PROJECT)
api = self._make_one(client)
Expand All @@ -299,7 +321,7 @@ def test_topic_publish_miss(self):
path = '/%s:publish' % (self.TOPIC_PATH,)
self.assertEqual(connection._called_with['path'], path)
self.assertEqual(connection._called_with['data'],
{'messages': [MESSAGE]})
{'messages': [B64MSG]})

def test_topic_list_subscriptions_no_paging(self):
from google.cloud.pubsub.topic import Topic
Expand Down

0 comments on commit c40e0d4

Please sign in to comment.