Ejemplo n.º 1
0
        public void LoadingNonInitializedStateMachine(
            AsyncPassiveStateMachine <State, Event> loadedMachine)
        {
            "when a non-initialized state machine is loaded".x(async() =>
            {
                var loader = new StateMachineLoader <State>();
                loader.SetCurrentState(new Initializable <State>());
                loader.SetHistoryStates(new Dictionary <State, State>());

                var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <State, Event>();
                SetupStates(stateMachineDefinitionBuilder);
                loadedMachine = stateMachineDefinitionBuilder
                                .WithInitialState(State.A)
                                .Build()
                                .CreatePassiveStateMachine();
                await loadedMachine.Load(loader);
            });

            "it should not be initialized already".x(async() =>
            {
                var stateMachineSaver = new StateMachineSaver <State>();
                await loadedMachine.Save(stateMachineSaver);
                stateMachineSaver
                .CurrentStateId
                .IsInitialized
                .Should()
                .BeFalse();
            });
        }
Ejemplo n.º 2
0
        public void SavingEventsForPassiveStateMachine(
            AsyncPassiveStateMachine <string, int> machine,
            StateMachineSaver <string, int> saver)
        {
            "establish a state machine".x(() =>
            {
                var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <string, int>();
                stateMachineDefinitionBuilder
                .In("A")
                .On(1)
                .Goto("B");
                stateMachineDefinitionBuilder
                .In("B")
                .On(2)
                .Goto("C");
                stateMachineDefinitionBuilder
                .In("C")
                .On(3)
                .Goto("A");
                machine = stateMachineDefinitionBuilder
                          .WithInitialState("A")
                          .Build()
                          .CreatePassiveStateMachine();
            });

            "when events are fired".x(async() =>
            {
                await machine.Fire(1);
                await machine.Fire(2);
                await machine.FirePriority(3);
                await machine.FirePriority(4);
            });

            "and it is saved".x(async() =>
            {
                saver = new StateMachineSaver <string, int>();
                await machine.Save(saver);
            });

            "it should save those events".x(() =>
            {
                saver
                .Events
                .Select(x => x.EventId)
                .Should()
                .HaveCount(2)
                .And
                .ContainInOrder(1, 2);
                saver
                .PriorityEvents
                .Select(x => x.EventId)
                .Should()
                .HaveCount(2)
                .And
                .ContainInOrder(4, 3);
            });
        }
Ejemplo n.º 3
0
        public void Loading(
            StateMachineSaver <State> saver,
            StateMachineLoader <State> loader,
            FakeExtension extension,
            State sourceState,
            State targetState)
        {
            "establish a saved state machine with history"._(async() =>
            {
                var machine = new AsyncPassiveStateMachine <State, Event>();
                DefineMachine(machine);
                await machine.Initialize(State.A);
                await machine.Start();
                await machine.Fire(Event.S2);     // set history of super state S
                await machine.Fire(Event.B);      // set current state to B

                saver  = new StateMachineSaver <State>();
                loader = new StateMachineLoader <State>();

                await machine.Save(saver);
            });

            "when state machine is loaded"._(async() =>
            {
                loader.SetCurrentState(saver.CurrentStateId);
                loader.SetHistoryStates(saver.HistoryStates);

                extension         = new FakeExtension();
                var loadedMachine = new AsyncPassiveStateMachine <State, Event>();
                loadedMachine.AddExtension(extension);

                DefineMachine(loadedMachine);
                await loadedMachine.Load(loader);

                loadedMachine.TransitionCompleted += (sender, args) =>
                {
                    sourceState = args.StateId;
                    targetState = args.NewStateId;
                };

                await loadedMachine.Start();
                await loadedMachine.Fire(Event.S);
            });

            "it should reset current state"._(() =>
                                              sourceState.Should().Be(State.B));

            "it should reset all history states of super states"._(() =>
                                                                   targetState.Should().Be(State.S2));

            "it should notify extensions"._(()
                                            => extension.LoadedCurrentState
                                            .Should().BeEquivalentTo(State.B));
        }