Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        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);
        }