public void MultiOrderedOnSuccessCalled() { var token = new AsyncToken <TestResult>(); var successACalled = false; var successBCalled = false; var successCCalled = false; token .OnSuccess(result => successACalled = true) .OnSuccess(result => { Assert.IsTrue(successACalled); successBCalled = true; }) .OnSuccess(result => { Assert.IsTrue(successACalled); Assert.IsTrue(successBCalled); successCCalled = true; }); token.Succeed(new TestResult()); Assert.IsTrue(successCCalled); }
/// <inheritdoc cref="IAsyncToken{T}"/> public IAsyncToken <TR> Map <TR>(Func <T, TR> map) { var output = new AsyncToken <TR>(); OnSuccess(value => output.Succeed(map(value))); OnFailure(output.Fail); return(output); }
public void OnSuccessCalledAfterResolved() { var token = new AsyncToken <TestResult>(); var called = false; token.Succeed(new TestResult()); token.OnSuccess(_ => called = true); Assert.IsTrue(called); }
public void OnFinallyCalledFromSucceed() { var token = new AsyncToken <TestResult>(); var called = false; token.OnFinally(_ => called = true); token.Succeed(new TestResult()); Assert.IsTrue(called); }
public void OnSuccessAborted() { var token = new AsyncToken <TestResult>(); var called = false; token.OnSuccess(_ => called = true); token.Abort(); token.Succeed(new TestResult()); token.OnSuccess(_ => called = true); Assert.IsFalse(called); }
public void Token() { var token = new AsyncToken <TestResult>(); var successCalled = true; token .Token() .OnSuccess(_ => successCalled = true); token.Succeed(new TestResult()); Assert.IsTrue(successCalled); }
public void AbortInCallbackNotRespected() { var token = new AsyncToken <TestResult>(); var called = false; token .OnSuccess(_ => token.Abort()) .OnSuccess(_ => called = true) .OnFinally(_ => called = true); token.Succeed(new TestResult()); Assert.IsTrue(called); }
/// <summary> /// Creates a single token from a collection of tokens. /// /// A failure from any one of the tokens will result in a failure of /// the returned token. If only a single token fails, only that exception /// is returned. If multiple tokens fail, an <c>AggregateException</c> /// is returned. /// /// If no tokens are passed in, the returned token is a Success. /// /// TODO: TESTS. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="tokens"></param> /// <returns></returns> public static IAsyncToken <T[]> All <T>(params IAsyncToken <T>[] tokens) { var len = tokens.Length; if (0 == len) { return(new AsyncToken <T[]>(new T[0])); } var returnToken = new AsyncToken <T[]>(); var values = new T[len]; var exceptions = new List <Exception>(); var numReturned = 0; for (var i = 0; i < len; i++) { var token = tokens[i]; var index = i; token .OnSuccess(value => values[index] = value) .OnFailure(exception => exceptions.Add(exception)) .OnFinally(_ => { if (++numReturned == len) { if (exceptions.Count > 1) { var aggregate = new AggregateException(); aggregate.Exceptions.AddRange(exceptions); returnToken.Fail(aggregate); } else if (exceptions.Count == 1) { returnToken.Fail(exceptions[0]); } else { returnToken.Succeed(values); } } }); } return(returnToken); }
public void OnFinallyCalledInOrderOnSuccess() { var token = new AsyncToken <TestResult>(); var successCalled = false; var finallyCalled = false; token .OnSuccess(_ => successCalled = true) .OnFinally(_ => { Assert.IsTrue(successCalled); finallyCalled = true; }); token.Succeed(new TestResult()); Assert.IsTrue(finallyCalled); }
public void ExceptionIsCaughtOnFinally() { var token = new AsyncToken <TestResult>(); var exception = new Exception(); var called = false; try { token .OnSuccess(_ => throw exception) .OnFinally(_ => called = true); token.Succeed(new TestResult()); } catch (Exception thrown) { Assert.AreSame(exception, thrown); } Assert.IsTrue(called); }
public void AggregateExceptionCreated() { var token = new AsyncToken <TestResult>(); var exception1 = new Exception(); var exception2 = new Exception(); var called = false; try { token .OnSuccess(_ => throw exception1) .OnSuccess(_ => throw exception2) .OnSuccess(_ => called = true); token.Succeed(new TestResult()); } catch (AggregateException exception) { Assert.AreEqual(2, exception.Exceptions.Count); } Assert.IsTrue(called); }
public void OnSuccessCalledExclusively() { var token = new AsyncToken <TestResult>(); var value = new TestResult(); var successCalled = false; var failureCalled = false; token .OnSuccess(returnedValue => { successCalled = true; Assert.AreSame(value, returnedValue); }) .OnFailure(exception => { failureCalled = true; }); token.Succeed(value); Assert.IsTrue(successCalled); Assert.IsFalse(failureCalled); }
public async Task TaskSuccess() { var token = new AsyncToken <float>(); var callbackResult = 0f; token.OnSuccess(val => callbackResult = val); var taskResult = 0f; var task = token .AsTask() .ContinueWith(cTask => { taskResult = cTask.Result; }); var expectedValue = 2.26f; token.Succeed(expectedValue); await task; Assert.AreEqual(expectedValue, callbackResult); Assert.AreEqual(expectedValue, taskResult); }