diff --git a/autopush/db.py b/autopush/db.py index 90e0aae4..251c3359 100644 --- a/autopush/db.py +++ b/autopush/db.py @@ -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" diff --git a/autopush/tests/test_db.py b/autopush/tests/test_db.py index 516d165d..2186583d 100644 --- a/autopush/tests/test_db.py +++ b/autopush/tests/test_db.py @@ -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, @@ -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): @@ -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 @@ -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) @@ -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): @@ -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): diff --git a/autopush/tests/test_websocket.py b/autopush/tests/test_websocket.py index 9dcafadb..3ec7c868 100644 --- a/autopush/tests/test_websocket.py +++ b/autopush/tests/test_websocket.py @@ -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 @@ -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)