Skip to content

Commit

Permalink
Handle exceptions in wait_for_table_activation
Browse files Browse the repository at this point in the history
  • Loading branch information
jacquev6 committed May 8, 2015
1 parent 6b4d773 commit 450e344
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions LowVoltage/compounds/wait_for_table_activation.py
Expand Up @@ -33,16 +33,27 @@ def wait_for_table_activation(connection, table):
wait_for_table_deletion(connection, table)
"""

r = connection(_lv.DescribeTable(table))
while not _table_is_fully_active(r.table):
desc = _describe(connection, table)
while desc is None or not _table_is_fully_active(desc):
time.sleep(3)
r = connection(_lv.DescribeTable(table))
desc = _describe(connection, table)
# Unfortunately, DescribeTable seems to perform an eventually consistent read.
# Without the next line, I've seen the doctest fail with a table status == "CREATING"
# after the wait. So, let's wait a bit more :-/
time.sleep(3)


def _describe(connection, table):
try:
return connection(_lv.DescribeTable(table)).table
except _lv.ResourceNotFoundException:
# DescribeTable seems to perform an eventually consistent read.
# So from time to time you create a table (on server A), then ask for its description
# (on server B) and server B doesn't know yet it exists.
# Seen in https://travis-ci.org/jacquev6/LowVoltage/jobs/61754722#L962
return None


def _table_is_fully_active(table):
if table.global_secondary_indexes is not None:
for gsi in table.global_secondary_indexes:
Expand All @@ -57,6 +68,22 @@ def setUp(self):
self.connection = self.mocks.create("connection")
self.sleep = self.mocks.replace("time.sleep")

def test_table_not_yet_created_on_first_describe(self):
self.connection.expect._call_.withArguments(
self.ActionChecker("DescribeTable", {"TableName": "Table"})
).andRaise(
_lv.ResourceNotFoundException
)
self.sleep.expect(3)
self.connection.expect._call_.withArguments(
self.ActionChecker("DescribeTable", {"TableName": "Table"})
).andReturn(
_lv.DescribeTableResponse(Table={"TableStatus": "ACTIVE"})
)
self.sleep.expect(3)

wait_for_table_activation(self.connection.object, "Table")

def test_table_creating(self):
self.connection.expect._call_.withArguments(
self.ActionChecker("DescribeTable", {"TableName": "Table"})
Expand Down

0 comments on commit 450e344

Please sign in to comment.