Skip to content

Commit

Permalink
Merge branch 'master' into multiple_ips
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaniv Kaul committed Dec 26, 2016
2 parents d7b462e + d62571f commit dbb538e
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions ovirtlago/testlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,13 @@ def _instance_of_any(obj, cls_list):
return any(True for cls in cls_list if isinstance(obj, cls))


def assert_true_within(func, timeout, allowed_exceptions=None):
def assert_equals_within(func, value, timeout, allowed_exceptions=None):
allowed_exceptions = allowed_exceptions or []
with utils.EggTimer(timeout) as timer:
while not timer.elapsed():
try:
if func():
res = func()
if res == value:
return
except Exception as exc:
if _instance_of_any(exc, allowed_exceptions):
Expand All @@ -224,23 +225,40 @@ def assert_true_within(func, timeout, allowed_exceptions=None):
raise

time.sleep(3)

raise AssertionError('Timed out after %s seconds' % timeout)
try:
raise AssertionError(
'%s != %s after %s seconds' % (res, value, timeout)
)
# if func repeatedly raises any of the allowed exceptions, res remains
# unbound throughout the function, resulting in an UnboundLocalError.
except UnboundLocalError:
raise AssertionError(
'%s failed to evaluate after %s seconds' %
(func.__name__, timeout)
)


def assert_true_within_short(func, allowed_exceptions=None):
def assert_equals_within_short(func, value, allowed_exceptions=None):
allowed_exceptions = allowed_exceptions or []
assert_true_within(
func,
SHORT_TIMEOUT,
allowed_exceptions=allowed_exceptions,
assert_equals_within(
func, value, SHORT_TIMEOUT, allowed_exceptions=allowed_exceptions
)


def assert_true_within_long(func, allowed_exceptions=None):
def assert_equals_within_long(func, value, allowed_exceptions=None):
allowed_exceptions = allowed_exceptions or []
assert_true_within(
func,
LONG_TIMEOUT,
allowed_exceptions=allowed_exceptions,
assert_equals_within(
func, value, LONG_TIMEOUT, allowed_exceptions=allowed_exceptions
)


def assert_true_within(func, timeout, allowed_exceptions=None):
assert_equals_within(func, True, timeout, allowed_exceptions)


def assert_true_within_short(func, allowed_exceptions=None):
assert_equals_within_short(func, True, allowed_exceptions)


def assert_true_within_long(func, allowed_exceptions=None):
assert_equals_within_long(func, True, allowed_exceptions)

0 comments on commit dbb538e

Please sign in to comment.