Exemple #1
0
        public async Task StartAndEndAreAlwaysRaised()
        {
            var runOrder = new List <string>();
            var task     = new ActionTask(Token, _ => { throw new Exception(); })
                           .Finally((s, d) => { });

            task.OnStart += _ => runOrder.Add("start");
            task.OnEnd   += _ => runOrder.Add("end");
            await task.StartAsAsyncWithoutThrowing();

            CollectionAssert.AreEqual(new string[] { "start", "end" }, runOrder);
        }
Exemple #2
0
        public async Task Inlining()
        {
            var runOrder = new List <string>();
            var act      = new ActionTask(Token, _ => runOrder.Add($"started"))
                           .Then(TaskEx.FromResult(1), TaskAffinity.Exclusive)
                           .Then((_, n) => n + 1)
                           .Then((_, n) => runOrder.Add(n.ToString()))
                           .Then(TaskEx.FromResult(20f), TaskAffinity.Exclusive)
                           .Then((_, n) => n + 1)
                           .Then((_, n) => runOrder.Add(n.ToString()))
                           .Finally((_, t) => runOrder.Add("done"))
            ;
            await act.StartAsAsyncWithoutThrowing();

            CollectionAssert.AreEqual(new string[] { "started", "2", "21", "done" }, runOrder);
        }
Exemple #3
0
        public async Task CanWrapATask()
        {
            var uiThread = 0;

            await new ActionTask(Token, _ => uiThread = Thread.CurrentThread.ManagedThreadId)
            {
                Affinity = TaskAffinity.UI
            }
            .StartAsAsyncWithoutThrowing();

            var runOrder = new List <string>();
            var task     = new Task(() => runOrder.Add($"ran {Thread.CurrentThread.ManagedThreadId}"));
            var act      = new ActionTask(task)
            {
                Affinity = TaskAffinity.UI
            };
            await act.StartAsAsyncWithoutThrowing();

            CollectionAssert.AreEqual(new string[] { $"ran {uiThread}" }, runOrder);
        }
Exemple #4
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.StartAsAsyncWithoutThrowing();

            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);
        }