public void OnFailureAborted() { var token = new AsyncToken <TestResult>(); var called = false; token.OnFailure(_ => called = true); token.Abort(); token.OnFailure(_ => called = true); token.Fail(new Exception()); Assert.IsFalse(called); }
public void MultiOrderedOnFailureCalled() { var token = new AsyncToken <TestResult>(); var failureACalled = false; var failureBCalled = false; var failureCCalled = false; token .OnFailure(exception => failureACalled = true) .OnFailure(exception => { Assert.IsTrue(failureACalled); failureBCalled = true; }) .OnFailure(exception => { Assert.IsTrue(failureACalled); Assert.IsTrue(failureBCalled); failureCCalled = true; }); token.Fail(new Exception()); Assert.IsTrue(failureCCalled); }
public async Task TaskCancelled() { var token = new AsyncToken <float>(); Exception callbackException = null; token.OnFailure(exception => callbackException = exception); var cancellation = new CancellationTokenSource(); var startTime = DateTime.Now; var timeoutTime = 60000; var task = token.AsTask(cancellation.Token, timeoutTime); var taskSuccess = false; cancellation.Cancel(); try { await task; taskSuccess = true; } catch (Exception exception) { Assert.IsTrue(exception is OperationCanceledException); } Assert.IsFalse(taskSuccess); // Ensure the backing token has not failed. Assert.IsNull(callbackException); // Finally, ensure that the cancellation came from the token and not a timeout! Assert.IsTrue((DateTime.Now - startTime).TotalMilliseconds <= timeoutTime); }
public async Task TaskTimeout() { var token = new AsyncToken <float>(); Exception callbackException = null; token.OnFailure(exception => callbackException = exception); var task = token.AsTask(5000); var taskSuccess = false; try { await task; taskSuccess = true; } catch (Exception exception) { Assert.IsTrue(exception is TimeoutException); } Assert.IsFalse(taskSuccess); // Ensure the backing token fails as well. Assert.IsTrue(callbackException is TimeoutException); }
public void OnFailureNotCalledAfterResolved() { var token = new AsyncToken <TestResult>(); var called = false; token.Succeed(new TestResult()); token.OnFailure(_ => called = true); Assert.IsFalse(called); }
public void OnFailureCalledAfterResolved() { var token = new AsyncToken <TestResult>(); var called = false; token.Fail(new Exception()); token.OnFailure(_ => called = true); Assert.IsTrue(called); }
public void OnFinallyCalledInOrderOnFailure() { var token = new AsyncToken <TestResult>(); var failCalled = false; var finallyCalled = false; token .OnFailure(_ => failCalled = true) .OnFinally(_ => { Assert.IsTrue(failCalled); finallyCalled = true; }); token.Fail(new Exception()); Assert.IsTrue(finallyCalled); }
public async Task TaskAborted() { var token = new AsyncToken <float>(); Exception callbackException = null; token.OnFailure(exception => callbackException = exception); var task = token.AsTask(); var taskSuccess = false; token.Abort(); try { await task; taskSuccess = true; } catch (Exception exception) { Assert.IsTrue(exception is OperationCanceledException); } Assert.IsFalse(taskSuccess); }
public async Task TaskFail() { var token = new AsyncToken <float>(); Exception callbackException = null; token.OnFailure(exception => callbackException = exception); var task = token.AsTask(); var expectedException = new InvalidOperationException("Test exception"); token.Fail(expectedException); try { await task; } catch (Exception exception) { Assert.AreEqual(expectedException, exception); } Assert.AreEqual(expectedException, callbackException); }