def get_future_status(future: TimedFuture) -> str: try: future.result(timeout=0) return STATUS_SUCCESS except futures.CancelledError: return "cancelled" # neither succeeded nor failed except futures.TimeoutError: raise # tried to check before ready except Exception: return "failure"
def test_timed_future_success(): future = TimedFuture() assert future.get_timing() == (None, None) expected_result = mock.sentinel.RESULT_VALUE start_time, finish_time = expected_timing = (1.0, 2.0) callback_results = [] callback = lambda future: callback_results.append( (future.result(), future.get_timing())) future.add_done_callback(callback) with timestamp(start_time): future.set_running_or_notify_cancel() assert future.get_timing() == (start_time, None) assert len(callback_results) == 0 with timestamp(finish_time): future.set_result(expected_result) assert future.get_timing() == expected_timing assert len(callback_results) == 1 assert callback_results[0] == (expected_result, expected_timing)