public void EventsQueueing( IAsyncStateMachine <string, int> machine, AutoResetEvent signal) { const int firstEvent = 0; const int secondEvent = 1; "establish an active state machine with transitions"._(() => { signal = new AutoResetEvent(false); machine = new AsyncActiveStateMachine <string, int>(); machine.In("A").On(firstEvent).Goto("B"); machine.In("B").On(secondEvent).Goto("C"); machine.In("C").ExecuteOnEntry(() => signal.Set()); machine.Initialize("A"); }); "when firing an event onto the state machine"._(async() => { await machine.Fire(firstEvent); await machine.Fire(secondEvent); await machine.Start(); }); "it should queue event at the end"._(() => signal.WaitOne(1000).Should().BeTrue("state machine should arrive at destination state")); }
public void SavingEventsForActiveStateMachine( AsyncActiveStateMachine <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() .CreateActiveStateMachine(); }); "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); }); }
public DeadlockRepro() { var builder = new StateMachineDefinitionBuilder <int, int>(); builder .In(0).On(1).Execute(() => myTcs.SetResult(0)); machine = builder .WithInitialState(0) .Build() .CreateActiveStateMachine(); }
public void CustomFactory( StandardFactory <string, int> factory) { "establish a custom factory"._(() => factory = A.Fake <StandardFactory <string, int> >()); "when creating an active state machine"._(() => { var machine = new AsyncActiveStateMachine <string, int>("_", factory); machine.In("initial").On(42).Goto("answer"); }); "it should use custom factory to create internal instances"._(() => A.CallTo(factory).MustHaveHappened()); }
public void DefaultStateMachineName( AsyncActiveStateMachine <string, int> machine, StateMachineNameReporter reporter) { "establish an instantiated active state machine"._(() => machine = new AsyncActiveStateMachine <string, int>()); "establish a state machine reporter"._(() => reporter = new StateMachineNameReporter()); "when the state machine report is generated"._(() => machine.Report(reporter)); "it should use the type of the state machine as name for state machine"._(() => reporter.StateMachineName .Should().Be("Appccelerate.StateMachine.AsyncActiveStateMachine<System.String,System.Int32>")); }
public void CustomStateMachineName( AsyncActiveStateMachine <string, int> machine, StateMachineNameReporter reporter) { const string name = "custom name"; "establish an instantiated active state machine with custom name"._(() => machine = new AsyncActiveStateMachine <string, int>(name)); "establish a state machine reporter"._(() => reporter = new StateMachineNameReporter()); "when the state machine report is generated"._(() => machine.Report(reporter)); "it should use custom name for state machine"._(() => reporter.StateMachineName .Should().Be(name)); }
public void CustomStateMachineName( AsyncActiveStateMachine <string, int> machine, StateMachineNameReporter reporter) { const string name = "custom name"; "establish an instantiated active state machine with custom name".x(() => machine = new StateMachineDefinitionBuilder <string, int>() .WithInitialState("initial") .Build() .CreateActiveStateMachine(name)); "establish a state machine reporter".x(() => reporter = new StateMachineNameReporter()); "when the state machine report is generated".x(() => machine.Report(reporter)); "it should use custom name for state machine".x(() => reporter.StateMachineName .Should().Be(name)); }
public void LoadingEventsForActiveStateMachine( AsyncActiveStateMachine <string, int> machine) { "establish a passive 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() .CreateActiveStateMachine(); }); "when it is loaded with Events".x(async() => { var firstEvent = new EventInformation <int>(2, null); var secondEvent = new EventInformation <int>(3, null); var priorityEvent = new EventInformation <int>(1, null); var loader = new StateMachineLoader <string, int>(); loader.SetEvents(new List <EventInformation <int> > { firstEvent, secondEvent, }); loader.SetPriorityEvents(new List <EventInformation <int> > { priorityEvent }); var extension = A.Fake <IExtension <string, int> >(); machine.AddExtension(extension); await machine.Load(loader); A.CallTo(() => extension.Loaded( A <IStateMachineInformation <string, int> > .Ignored, A <Initializable <string> > .Ignored, A <IReadOnlyDictionary <string, string> > .Ignored, A <IReadOnlyCollection <EventInformation <int> > > .That .Matches(c => c.Count == 2 && c.Contains(firstEvent) && c.Contains(secondEvent)), A <IReadOnlyCollection <EventInformation <int> > > .That .Matches(c => c.Count == 1 && c.Contains(priorityEvent)))) .MustHaveHappenedOnceExactly(); }); "it should process those events".x(async() => { var transitionRecords = new List <TransitionRecord>(); machine.TransitionCompleted += (sender, args) => transitionRecords.Add( new TransitionRecord(args.EventId, args.StateId, args.NewStateId)); var signal = SetUpWaitForAllTransitions(machine, 3); await machine.Start(); WaitForAllTransitions(signal); transitionRecords .Should() .HaveCount(3) .And .IsEquivalentInOrder(new List <TransitionRecord> { new TransitionRecord(1, "A", "B"), new TransitionRecord(2, "B", "C"), new TransitionRecord(3, "C", "A") }); }); }