Skip to content

Commit

Permalink
Don't override explicit timestamp passed to 'Topic.publish()'.
Browse files Browse the repository at this point in the history
Addresses:
#809 (comment)
  • Loading branch information
tseaver committed Apr 10, 2015
1 parent 9264828 commit 627e0de
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
31 changes: 31 additions & 0 deletions gcloud/pubsub/test_topic.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,37 @@ def _utcnow():
self.assertEqual(req['path'], '/%s:publish' % PATH)
self.assertEqual(req['data'], {'messages': [MESSAGE]})

def test_publish_single_bytes_w_add_timestamp_w_ts_in_attrs(self):
import base64
import datetime
from gcloud.pubsub import topic as MUT
from gcloud._testing import _Monkey
NOW = datetime.datetime.utcnow()

def _utcnow(): # pragma: NO COVER
return NOW

TOPIC_NAME = 'topic_name'
PROJECT = 'PROJECT'
PAYLOAD = b'This is the message text'
B64 = base64.b64encode(PAYLOAD).decode('ascii')
MSGID = 'DEADBEEF'
OVERRIDE = '2015-04-10T16:46:22.868399Z'
MESSAGE = {'data': B64,
'attributes': {'timestamp': OVERRIDE}}
PATH = 'projects/%s/topics/%s' % (PROJECT, TOPIC_NAME)
conn = _Connection({'messageIds': [MSGID]})
topic = self._makeOne(TOPIC_NAME, project=PROJECT, connection=conn,
timestamp_messages=True)
with _Monkey(MUT, _NOW=_utcnow):
msgid = topic.publish(PAYLOAD, timestamp=OVERRIDE)
self.assertEqual(msgid, MSGID)
self.assertEqual(len(conn._requested), 1)
req = conn._requested[0]
self.assertEqual(req['method'], 'POST')
self.assertEqual(req['path'], '/%s:publish' % PATH)
self.assertEqual(req['data'], {'messages': [MESSAGE]})

def test_publish_single_w_attrs(self):
import base64
TOPIC_NAME = 'topic_name'
Expand Down
2 changes: 1 addition & 1 deletion gcloud/pubsub/topic.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def publish(self, message, **attrs):
:rtype: str
:returns: message ID assigned by the server to the published message
"""
if self.timestamp_messages:
if self.timestamp_messages and 'timestamp' not in attrs:
attrs['timestamp'] = '%sZ' % _NOW().isoformat()
message_b = base64.b64encode(message).decode('ascii')
message_data = {'data': message_b, 'attributes': attrs}
Expand Down

0 comments on commit 627e0de

Please sign in to comment.