示例#1
0
文件: Tests.cs 项目: willofd02/Unity
        public async Task StartAndEndAreAlwaysRaised()
        {
            var   runOrder = new List <string>();
            ITask task     = new ActionTask(Token, _ => { throw new Exception(); });

            task.OnStart += _ => runOrder.Add("start");
            task.OnEnd   += (_, __, ___) => runOrder.Add("end");
            // we want to run a Finally on a new task (and not in-thread) so that the StartAndSwallowException handler runs after this
            // one, proving that the exception is propagated after everything is done
            task = task.Finally((_, __) => {}, TaskAffinity.Concurrent);

            await task.StartAndSwallowException();

            CollectionAssert.AreEqual(new string[] { "start", "end" }, runOrder);
        }
示例#2
0
文件: Tests.cs 项目: Lw960201/Unity-1
        public async Task TaskOnFailureGetsCalledWhenExceptionHappensUpTheChain()
        {
            var runOrder   = new List <string>();
            var exceptions = new List <Exception>();
            var task       = new ActionTask(Token, _ => { throw new InvalidOperationException(); })
                             .Then(_ => { runOrder.Add("1"); })
                             .Catch(ex => exceptions.Add(ex))
                             .Then(() => runOrder.Add("OnFailure"), TaskRunOptions.OnFailure)
                             .Finally((s, e) => { });
            await task.StartAndSwallowException();

            CollectionAssert.AreEqual(
                new string[] { typeof(InvalidOperationException).Name },
                exceptions.Select(x => x.GetType().Name).ToArray());
            CollectionAssert.AreEqual(
                new string[] { "OnFailure" },
                runOrder);
        }
示例#3
0
文件: Tests.cs 项目: Lw960201/Unity-1
        public async Task ContinueAfterException()
        {
            var runOrder   = new List <string>();
            var exceptions = new List <Exception>();
            var task       = new ActionTask(Token, _ => { throw new InvalidOperationException(); })
                             .Catch(e => { runOrder.Add("1"); exceptions.Add(e); return(true); })
                             .Then(_ => { throw new InvalidCastException(); })
                             .Catch(e => { runOrder.Add("2"); exceptions.Add(e); return(true); })
                             .Then(_ => { throw new ArgumentNullException(); })
                             .Catch(e => { runOrder.Add("3"); exceptions.Add(e); return(true); })
                             .Finally((s, e) => { });
            await task.StartAndSwallowException();

            CollectionAssert.AreEqual(
                new string[] { typeof(InvalidOperationException).Name, typeof(InvalidCastException).Name, typeof(ArgumentNullException).Name },
                exceptions.Select(x => x.GetType().Name).ToArray());
            CollectionAssert.AreEqual(
                new string[] { "1", "2", "3" },
                runOrder);
        }
示例#4
0
文件: Tests.cs 项目: willofd02/Unity
        public async Task MultipleCatchStatementsCanHappen()
        {
            var runOrder   = new List <string>();
            var exceptions = new List <Exception>();
            var task       = new ActionTask(Token, _ => { throw new InvalidOperationException(); })
                             .Catch(e => { runOrder.Add("1"); exceptions.Add(e); })
                             .Then(_ => { throw new InvalidCastException(); })
                             .Catch(e => { runOrder.Add("2"); exceptions.Add(e); })
                             .Then(_ => { throw new ArgumentNullException(); })
                             .Catch(e => { runOrder.Add("3"); exceptions.Add(e); })
                             .Finally((b, e) => { }, TaskAffinity.Concurrent);
            await task.StartAndSwallowException();

            CollectionAssert.AreEqual(
                new string[] { typeof(InvalidOperationException).Name, typeof(InvalidOperationException).Name, typeof(InvalidOperationException).Name },
                exceptions.Select(x => x.GetType().Name).ToArray());
            CollectionAssert.AreEqual(
                new string[] { "1", "2", "3" },
                runOrder);
        }