Пример #1
0
        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);
        }
Пример #2
0
        /// <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);
        }
Пример #3
0
        public void OnSuccessNotCalledAfterResolved()
        {
            var token  = new AsyncToken <TestResult>();
            var called = false;

            token.Fail(new Exception());
            token.OnSuccess(_ => called = true);

            Assert.IsFalse(called);
        }
Пример #4
0
        public void OnFinallyCalledFromFail()
        {
            var token  = new AsyncToken <TestResult>();
            var called = false;

            token.OnFinally(_ => called = true);
            token.Fail(new Exception());

            Assert.IsTrue(called);
        }
Пример #5
0
        public void OnFinallyNotCalledAfterAbort()
        {
            var token  = new AsyncToken <TestResult>();
            var called = false;

            token.OnFinally(_ => called = true);
            token.Abort();
            token.Fail(new Exception());

            Assert.IsFalse(called);
        }
Пример #6
0
        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);
        }
Пример #7
0
        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);
        }
Пример #8
0
        public void ExceptionIsCaughtOnFail()
        {
            var token     = new AsyncToken <TestResult>();
            var exception = new Exception();
            var called    = false;

            try
            {
                token
                .OnFailure(_ => throw exception)
                .OnFailure(_ => called = true);
                token.Fail(new Exception());
            }
            catch (Exception thrown)
            {
                Assert.AreSame(exception, thrown);
            }

            Assert.IsTrue(called);
        }
Пример #9
0
        public void OnFailureCalledExclusively()
        {
            var token         = new AsyncToken <TestResult>();
            var exception     = new Exception();
            var successCalled = false;
            var failureCalled = false;

            token
            .OnSuccess(value =>
            {
                successCalled = true;
            })
            .OnFailure(returnedException =>
            {
                failureCalled = true;

                Assert.AreSame(exception, returnedException);
            });
            token.Fail(exception);

            Assert.IsTrue(failureCalled);
            Assert.IsFalse(successCalled);
        }
Пример #10
0
        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);
        }