public async Task RunningDifferentTasksDependingOnPreviousResult() { var callOrder = new List <string>(); var taskEnd = new ActionTask(Token, () => callOrder.Add("chain completed")) { Name = "Chain Completed" }; var final = taskEnd.Finally((_, __) => { }, TaskAffinity.Concurrent); var taskStart = new FuncTask <bool>(Token, _ => { callOrder.Add("chain start"); return(false); }) { Name = "Chain Start" } .Then(new ActionTask <bool>(Token, (_, __) => { callOrder.Add("failing"); throw new InvalidOperationException(); }) { Name = "Failing" }); taskStart.Then(new ActionTask(Token, () => { callOrder.Add("on failure"); }) { Name = "On Failure" }, runOptions: TaskRunOptions.OnFailure) .Then(taskEnd, taskIsTopOfChain: true); taskStart.Then(new ActionTask(Token, () => { callOrder.Add("on success"); }) { Name = "On Success" }, runOptions: TaskRunOptions.OnSuccess) .Then(taskEnd, taskIsTopOfChain: true); await final.StartAndSwallowException(); Console.WriteLine(String.Join(",", callOrder.ToArray())); CollectionAssert.AreEqual(new string[] { "chain start", "failing", "on failure", "chain completed" }, callOrder); }
public async Task StartAndEndAreAlwaysRaised() { var runOrder = new List <string>(); var task = new ActionTask(Token, _ => { throw new Exception(); }); task.OnStart += _ => runOrder.Add("start"); task.OnEnd += _ => runOrder.Add("end"); await task.Finally((s, d) => { }).StartAndSwallowException(); CollectionAssert.AreEqual(new string[] { "start", "end" }, runOrder); }
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); }