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

Commit

Permalink
Merge pull request #514 from mozilla-services/feature/issue-433
Browse files Browse the repository at this point in the history
Feature/issue 433
  • Loading branch information
jrconlin authored Jul 13, 2016
2 parents a17679f + ed7a69f commit d1132b2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
9 changes: 9 additions & 0 deletions autopush/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ def preflight_check(storage, router, uaid="deadbeef00000000deadbeef00000000"):
Failure to run correctly will raise an exception.
"""
# Verify tables are ready for use if they just got created
ready = False
while not ready:
tbl_status = [x.describe()["Table"]["TableStatus"]
for x in [storage.table, router.table]]
ready = all([status == "ACTIVE" for status in tbl_status])
if not ready:
time.sleep(1)

# Use a distinct UAID so it doesn't interfere with metrics
chid = uuid.uuid4().hex
node_id = "mynode:2020"
Expand Down
34 changes: 28 additions & 6 deletions autopush/tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from boto.dynamodb2.items import Item
from mock import Mock
from moto import mock_dynamodb2
from nose.tools import eq_
from nose.tools import eq_, assert_raises

from autopush.db import (
get_rotating_message_table,
Expand Down Expand Up @@ -51,7 +51,7 @@ def raise_exc(*args, **kwargs): # pragma: no cover
router.clear_node = Mock()
router.clear_node.side_effect = raise_exc

with self.assertRaises(Exception):
with assert_raises(Exception):
preflight_check(storage, router)

def test_preflight_check(self):
Expand All @@ -63,7 +63,29 @@ def test_preflight_check(self):
# now check that the database reports no entries.
notifs = storage.fetch_notifications(pf_uaid)
eq_(len(notifs), 0)
self.assertRaises(ItemNotFound, router.get_uaid, pf_uaid)
assert_raises(ItemNotFound, router.get_uaid, pf_uaid)

def test_preflight_check_wait(self):
router = Router(get_router_table(), SinkMetrics())
storage = Storage(get_storage_table(), SinkMetrics())

storage.table.describe = mock_describe = Mock()

values = [
dict(Table=dict(TableStatus="PENDING")),
dict(Table=dict(TableStatus="ACTIVE")),
]

def return_vals(*args, **kwargs):
return values.pop(0)

mock_describe.side_effect = return_vals
pf_uaid = "deadbeef00000000deadbeef01010101"
preflight_check(storage, router, pf_uaid)
# now check that the database reports no entries.
notifs = storage.fetch_notifications(pf_uaid)
eq_(len(notifs), 0)
assert_raises(ItemNotFound, router.get_uaid, pf_uaid)

def test_get_month(self):
from autopush.db import get_month
Expand All @@ -86,7 +108,7 @@ def test_normalize_id(self):
abnormal = "deadbeef00000000decafbad00000000"
normal = "deadbeef-0000-0000-deca-fbad00000000"
eq_(db.normalize_id(abnormal), normal)
self.assertRaises(ValueError, db.normalize_id, "invalid")
assert_raises(ValueError, db.normalize_id, "invalid")
eq_(db.normalize_id(abnormal.upper()), normal)


Expand Down Expand Up @@ -139,7 +161,7 @@ def raise_error(*args, **kwargs):
raise ProvisionedThroughputExceededException(None, None)

storage.table.connection.put_item.side_effect = raise_error
with self.assertRaises(ProvisionedThroughputExceededException):
with assert_raises(ProvisionedThroughputExceededException):
storage.save_notification(dummy_uaid, dummy_chid, 12)

def test_save_over_provisioned(self):
Expand All @@ -151,7 +173,7 @@ def raise_error(*args, **kwargs):
raise ProvisionedThroughputExceededException(None, None)

storage.table.query_2.side_effect = raise_error
with self.assertRaises(ProvisionedThroughputExceededException):
with assert_raises(ProvisionedThroughputExceededException):
storage.fetch_notifications(dummy_uaid)

def test_delete_over_provisioned(self):
Expand Down
12 changes: 8 additions & 4 deletions autopush/tests/test_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -1690,9 +1690,11 @@ def test_notification_dont_deliver_after_ack(self):

d = Deferred()

def wait_for_clear():
def wait_for_clear(count=0.0):
if self.proto.ps.updates_sent: # pragma: nocover
reactor.callLater(0.1, wait_for_clear)
if count > 5.0:
raise Exception("Time-out waiting")
reactor.callLater(0.1, wait_for_clear, count+0.1)
return

# Accepting again
Expand Down Expand Up @@ -1739,10 +1741,12 @@ def test_notification_dont_deliver(self):

d = Deferred()

def check_mock_call():
def check_mock_call(count=0.0):
calls = self.proto.process_notifications.mock_calls
if len(calls) < 1:
reactor.callLater(0.1, check_mock_call)
if count > 5.0: # pragma: nocover
raise Exception("Time-out waiting")
reactor.callLater(0.1, check_mock_call, count+0.1)
return

eq_(len(calls), 1)
Expand Down

0 comments on commit d1132b2

Please sign in to comment.