def test_external_timeout(): """ A TimeoutError is raised, not StatusTimeoutError or WaitTimeoutError, when set_exception(TimeoutError) has been set. """ st = StatusBase(timeout=1) st.set_exception(TimeoutError()) with pytest.raises(TimeoutError) as exc: st.wait(1) assert not isinstance(exc, WaitTimeoutError) assert not isinstance(exc, StatusTimeoutError)
def test_exception_fail_path(): st = StatusBase() class LocalException(Exception): ... exc = LocalException() st.set_exception(exc) assert exc is st.exception() with pytest.raises(LocalException): st.wait(1)
def test_exception_fail_path_with_class(): """ Python allows `raise Exception` and `raise Exception()` so we do as well. """ st = StatusBase() class LocalException(Exception): ... st.set_exception(LocalException) assert LocalException is st.exception() with pytest.raises(LocalException): st.wait(1)
def test_set_exception_after_timeout(): """ If an external callback (e.g. pyepics) calls set_exception after the status has timed out, ignore it. """ st = StatusBase(timeout=0) time.sleep(0.1) assert isinstance(st.exception(), StatusTimeoutError) class LocalException(Exception): ... # External callback reports failure, too late. st.set_exception(LocalException()) assert isinstance(st.exception(), StatusTimeoutError)
def test_set_exception_special_banned_exceptions(): """ Exceptions with special significant to StatusBase are banned. See comments in set_exception. """ st = StatusBase() # Test the class and the instance of each. with pytest.raises(ValueError): st.set_exception(StatusTimeoutError) with pytest.raises(ValueError): st.set_exception(StatusTimeoutError()) with pytest.raises(ValueError): st.set_exception(WaitTimeoutError) with pytest.raises(ValueError): st.set_exception(WaitTimeoutError())
def test_set_exception_wrong_type(): st = StatusBase() NOT_AN_EXCEPTION = object() with pytest.raises(ValueError): st.set_exception(NOT_AN_EXCEPTION)
def test_set_exception_twice(): st = StatusBase() exc = Exception() st.set_exception(exc) with pytest.raises(InvalidState): st.set_exception(exc)