def test_states(ev): with ev: future = futuremod.BaseFuture() assert future._state == futuremod.PENDING assert not future.done() assert not future.running() assert not future.cancelled() future.set_running_or_notify_cancel() assert future._state == futuremod.RUNNING assert not future.done() assert future.running() assert not future.cancelled() future.set_result(None) assert future._state == futuremod.FINISHED assert future.done() assert not future.running() assert not future.cancelled() future = futuremod.BaseFuture.with_cancel() assert future._state == futuremod.CANCELLED assert future.done() assert not future.running() assert future.cancelled()
def test_set_result(ev): with ev: future = futuremod.BaseFuture() future.set_result('TestResult') assert future._result == 'TestResult' assert future.result() == 'TestResult'
def test_repr(): future = futuremod.BaseFuture() assert re.match(r'<BaseFuture at .* state=pending>', repr(future)) future.set_result('TestResult') assert re.match(r'<BaseFuture at .* state=finished returned TestResult', repr(future))
def test_cancel(): future = futuremod.BaseFuture() assert future.cancel() # the second time should just return as already cancelled assert future.cancel() future = futuremod.BaseFuture.with_result(True) assert False == future.cancel()
def test_set_exception(): future = futuremod.BaseFuture() err = RuntimeError('TestError') future.set_exception(err) assert future._exception == err assert future._traceback == None assert future.done() assert future.exception() == err with pytest.raises(RuntimeError): future.result()
def test_set_exception_then_task(ev, callback_info): with ev: future = futuremod.BaseFuture() task = AsyncTask(callback_info.task_callback, (future, )) future.set_exception(RuntimeError('TestError')) future.add_task(task) ev.execute_all_tasks() assert 1 == len(callback_info.callbacks) assert callback_info.callbacks[0][0] == (future, ) assert future.done()
def test_task_then_set_result(ev, callback_info): with ev: future = futuremod.BaseFuture() task = AsyncTask(callback_info.task_callback, (future, )) future.add_task(task) assert not callback_info.callbacks future.set_result(12) ev.execute_all_tasks() assert 1 == len(callback_info.callbacks) assert callback_info.callbacks[0][0] == (future, ) assert future.done()
def test_constructor(ev): with ev: future = futuremod.BaseFuture() assert future._state == futuremod.PENDING assert future._result == None assert future._tasks == []