Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
fix: overwrite existing messages instead of throwing an error
Browse files Browse the repository at this point in the history
DynamoDB throws a condition check failure if you put_item and
it already exists. This can occur when we save a message that
may already exist when a user hops servers without ack'ing. We
should allow it since it consumes the same resources.

Closes #535
  • Loading branch information
bbangert committed Jul 19, 2016
1 parent a79f3a6 commit f5d3c9a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion autopush/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ def store_message(self, uaid, channel_id, message_id, ttl, data=None,
if data:
item["headers"] = headers
item["data"] = data
self.table.put_item(data=item)
self.table.put_item(data=item, overwrite=True)
return True

@track_provisioned
Expand Down
29 changes: 29 additions & 0 deletions autopush/tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,35 @@ def test_message_storage(self):
all_messages = list(message.fetch_messages(self.uaid))
eq_(len(all_messages), 0)

def test_message_storage_overwrite(self):
"""Test that store_message can overwrite existing messages which
can occur in some reconnect cases but shouldn't error"""
chid = str(uuid.uuid4())
chid2 = str(uuid.uuid4())
m = get_rotating_message_table()
message = Message(m, SinkMetrics())
message.register_channel(self.uaid, chid)
message.register_channel(self.uaid, chid2)

data1 = str(uuid.uuid4())
data2 = str(uuid.uuid4())
ttl = int(time.time())+100
time1, time2 = self._nstime(), self._nstime()+1
message.store_message(self.uaid, chid, time1, ttl, data1, {})
message.store_message(self.uaid, chid, time1, ttl, data2, {})
message.store_message(self.uaid, chid2, time2, ttl, data1, {})

all_messages = list(message.fetch_messages(self.uaid))
eq_(len(all_messages), 2)

message.delete_messages_for_channel(self.uaid, chid2)
all_messages = list(message.fetch_messages(self.uaid))
eq_(len(all_messages), 1)

message.delete_message(self.uaid, chid, time1)
all_messages = list(message.fetch_messages(self.uaid))
eq_(len(all_messages), 0)

def test_delete_user(self):
chid = str(uuid.uuid4())
chid2 = str(uuid.uuid4())
Expand Down

0 comments on commit f5d3c9a

Please sign in to comment.