示例#1
0
        public void TwoParallelStatemachines()
        {
            var log = new List <string>();

            var inner1 = new LoggingStateMachine("inner1", 1000, log);
            var inner2 = new LoggingStateMachine("inner2", 100, log);

            var outer = new StateMachine()
                        .AddNode(new State("Start", () => log.Add("outer.Start")))
                        .AddNode(new State("State1", () =>
            {
                log.Add("outer.State1");
                var t1    = (new Invoker(inner1)).Invoke();
                var t2    = (new Invoker(inner2)).Invoke();
                var tasks = new List <Task>()
                {
                    t1, t2
                };
                Task.WaitAll(tasks.ToArray());
            }))
                        .AddNode(new State("End", () => log.Add("outer.End")))
                        .AddEdge("Start", "State1", () => { return(true); })
                        .AddEdge("State1", "End", () => { return(true); });

            var invoker = new Invoker(outer);

            invoker.Completed += (sender, args) =>
            {
                log.Add("outer.Ended");
            };
            invoker.Faulted += (sender, args) =>
            {
                log.Add("outer.Canceled");
            };

            var t = invoker.Invoke();

            t.Wait();

            Assert.AreEqual("outer.Start->outer.State1->" +
                            "inner2.Start->inner2.State1->inner2.State2->inner2.End->" +
                            "inner1.Start->inner1.State1->inner1.State2->inner1.End->" +
                            "outer.End->outer.Ended", string.Join("->", log));
        }
示例#2
0
        public void Timeout()
        {
            var       log       = new List <string>();
            FaultType faultType = FaultType.None;

            var inner1 = new LoggingStateMachine("inner1", 1000, log);
            var inner2 = new LoggingStateMachine("inner2", 100, log);

            var stateMachine = new StateMachine()
                               .AddNode(new State("Start", () =>
            {
                log.Add("Start");
                Thread.Sleep(1100);
            }))
                               .AddNode(new State("State1", () =>
            {
                log.Add("State1");
                Thread.Sleep(1100);
            }))
                               .AddNode(new State("End", () => log.Add("End")))
                               .AddEdge("Start", "State1", () => { return(true); })
                               .AddEdge("State1", "End", () => { return(true); });

            var invoker = new Invoker(stateMachine);

            invoker.Completed += (sender, args) =>
            {
                log.Add("Ended");
            };
            invoker.Faulted += (sender, args) =>
            {
                faultType = args.FaultType;
                log.Add("Canceled");
            };

            var t = invoker.Invoke(TimeSpan.FromSeconds(1));

            t.Wait();

            Assert.AreEqual("Start->Canceled", string.Join("->", log));
            Assert.AreEqual(FaultType.ByTimeout, faultType);
        }