def test_exception_on_error(self): # If the service is in an error state, an exception is raised. def get_service_state(): return 'error: unnecessary zebra' with self.assertRaises(Exception): wait_for_service(get_service_state)
def test_service_started(self): # If the service has already started, the function returns immediately. def get_service_state(): return 'started' # The fact that no exception is raised shows that no unexpected # sleeping was done. def sleep(seconds): raise RuntimeError('narcolepsy') wait_for_service(get_service_state, sleep)
def test_returns_when_transitions_to_started(self): # get_service_state and sleep are called repeatedly until the service # is started. statuses = ['installing', 'pending', 'pending', 'started'] def get_service_state(): return statuses[0] def sleep(seconds): statuses.pop(0) wait_for_service(get_service_state, sleep)
def test_sleep_if_service_not_started(self): # If the service has not started, the sleep function is called. def get_service_state(): return 'not started' class ZZZZ(Exception): pass def sleep(seconds): raise ZZZZ with self.assertRaises(ZZZZ): wait_for_service(get_service_state, sleep)