def assert_equals_within( func, value, timeout, allowed_exceptions=None, initial_wait=10 ): allowed_exceptions = allowed_exceptions or [] with utils.EggTimer(timeout) as timer: while not timer.elapsed(): try: res = func() if res == value: return except Exception as exc: if _instance_of_any(exc, allowed_exceptions): time.sleep(3) continue LOGGER.exception("Unhandled exception in %s", func) raise if initial_wait == 0: time.sleep(3) else: time.sleep(initial_wait) initial_wait = 0 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 allow_exceptions_within_timeout(func, timeout, allowed_exceptions=None): allowed_exceptions = allowed_exceptions or [Exception] with utils.EggTimer(timeout=timeout) as timer: while not timer.elapsed(): try: return func() except Exception as exc: if not _instance_of_any(exc, allowed_exceptions): raise time.sleep(3)
def assert_true_within(func, timeout, allowed_exceptions=None): allowed_exceptions = allowed_exceptions or [] with utils.EggTimer(timeout) as timer: while not timer.elapsed(): try: if func(): return except Exception as exc: if _instance_of_any(exc, allowed_exceptions): continue LOGGER.exception("Unhandled exception in %s", func) raise time.sleep(3) raise AssertionError('Timed out after %s seconds' % timeout)