Example #1
0
        public void PriorityEventsQueueing(
            IAsyncStateMachine <string, int> machine,
            AutoResetEvent signal)
        {
            const int firstEvent  = 0;
            const int secondEvent = 1;

            "establish an active state machine with transitions".x(() =>
            {
                signal = new AutoResetEvent(false);

                var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <string, int>();
                stateMachineDefinitionBuilder.In("A").On(secondEvent).Goto("B");
                stateMachineDefinitionBuilder.In("B").On(firstEvent).Goto("C");
                stateMachineDefinitionBuilder.In("C").ExecuteOnEntry(() => signal.Set());
                machine = stateMachineDefinitionBuilder
                          .WithInitialState("A")
                          .Build()
                          .CreateActiveStateMachine();
            });

            "when firing a priority event onto the state machine".x(async() =>
            {
                await machine.Fire(firstEvent);
                await machine.FirePriority(secondEvent);
                await machine.Start();
            });

            "it should queue event at the front".x(() =>
                                                   signal.WaitOne(1000).Should().BeTrue("state machine should arrive at destination state"));
        }
        public void PriorityEventsQueueing(
            IAsyncStateMachine <string, int> machine)
        {
            const int FirstEvent  = 0;
            const int SecondEvent = 1;

            var arrived = false;

            "establish a passive state machine with transitions".x(() =>
            {
                var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <string, int>();
                stateMachineDefinitionBuilder.In("A").On(SecondEvent).Goto("B");
                stateMachineDefinitionBuilder.In("B").On(FirstEvent).Goto("C");
                stateMachineDefinitionBuilder.In("C").ExecuteOnEntry(() => arrived = true);
                machine = stateMachineDefinitionBuilder
                          .WithInitialState("A")
                          .Build()
                          .CreatePassiveStateMachine();
            });

            "when firing a priority event onto the state machine".x(() =>
            {
                machine.Fire(FirstEvent);
                machine.FirePriority(SecondEvent);
                machine.Start();
            });

            "it should queue event at the front".x(()
                                                   => arrived.Should().BeTrue("state machine should arrive at destination state"));
        }