Skip to content

Commit

Permalink
change initialization behaviour
Browse files Browse the repository at this point in the history
Previously to initialize an Active/Passive SM the method Initialize(TState initialState) had to be called. The SM would then remember this initial State but NOT yet transition to it. The transition would only be done once Start() was called.

After this change, the initalState will already have to be configured at definition time via StateMachineDefinitionBuilder.WithInitialState(TState initialStateToUse).
The SM will no longer need an Initialize method. Same as before this change, the transition to the initial state would only be done once Start() was called.
  • Loading branch information
wtjerry committed Oct 3, 2019
1 parent 732e041 commit 7696de3
Show file tree
Hide file tree
Showing 32 changed files with 408 additions and 520 deletions.
106 changes: 13 additions & 93 deletions source/Appccelerate.StateMachine.Facts/ExtensionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,6 @@ namespace Appccelerate.StateMachine.Facts
/// </summary>
public class ExtensionTest
{
/// <summary>
/// When the state machine is initialized then the extensions get notified.
/// </summary>
[Fact]
public void Initialize()
{
var initialState = States.A;

var information = A.Fake<IStateMachineInformation<States, Events>>();
var extension = A.Fake<IExtension<States, Events>>();
var stateContainer = new StateContainer<States, Events>();
stateContainer.Extensions.Add(extension);

var standardFactory = new StandardFactory<States, Events>();
var testee = new StateMachine<States, Events>(
standardFactory,
A.Fake<IStateLogic<States, Events>>());

testee.Initialize(initialState, stateContainer, information);

A.CallTo(() => extension.InitializingStateMachine(information, ref initialState))
.MustHaveHappened();

A.CallTo(() => extension.InitializedStateMachine(information, initialState))
.MustHaveHappened();
}

[Fact]
public void EnterInitialState()
{
Expand All @@ -77,8 +50,7 @@ public void EnterInitialState()
.WithStateContainer(stateContainer)
.Build();

testee.Initialize(InitialState, stateContainer, information);
testee.EnterInitialState(stateContainer, information, stateDefinitions);
testee.EnterInitialState(stateContainer, information, stateDefinitions, InitialState);

A.CallTo(() => extension.EnteringInitialState(information, InitialState))
.MustHaveHappened();
Expand All @@ -90,38 +62,6 @@ public void EnterInitialState()
.MustHaveHappened();
}

/// <summary>
/// An extension can override the state to which the state machine is initialized.
/// </summary>
[Fact]
public void OverrideInitialState()
{
var overrideExtension = new OverrideExtension { OverriddenState = States.B };
var stateContainer = new StateContainer<States, Events>();
stateContainer.Extensions.Add(overrideExtension);

var stateDefinitionsBuilder = new StateDefinitionsBuilder<States, Events>();
stateDefinitionsBuilder.In(States.A);
stateDefinitionsBuilder.In(States.B);
var stateDefinitions = stateDefinitionsBuilder.Build();

var testee = new StateMachineBuilder<States, Events>()
.WithStateContainer(stateContainer)
.Build();

testee.Initialize(
States.A,
stateContainer,
A.Fake<IStateMachineInformation<States, Events>>());

testee.EnterInitialState(
stateContainer,
A.Fake<IStateMachineInformation<States, Events>>(),
stateDefinitions);

stateContainer.CurrentStateId.Should().Be(States.B);
}

/// <summary>
/// When an event is fired on the state machine then the extensions are notified.
/// </summary>
Expand All @@ -143,8 +83,7 @@ public void Fire()
.WithStateContainer(stateContainer)
.Build();

testee.Initialize(States.A, stateContainer, stateContainer);
testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions);
testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A);

var eventId = Events.B;
var eventArgument = new object();
Expand Down Expand Up @@ -187,8 +126,7 @@ public void OverrideFiredEvent()
.WithStateContainer(stateContainer)
.Build();

testee.Initialize(States.A, stateContainer, stateContainer);
testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions);
testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A);

const Events NewEvent = Events.C;
var newEventArgument = new object();
Expand Down Expand Up @@ -226,8 +164,7 @@ public void NotifiesAboutTransitions()
.WithStateContainer(stateContainer)
.Build();

testee.Initialize(Source, stateContainer, stateContainer);
testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions);
testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A);

testee.Fire(Event, stateContainer, stateContainer, stateDefinitions);

Expand Down Expand Up @@ -274,8 +211,7 @@ public void ExceptionThrowingGuard()

testee.TransitionExceptionThrown += (s, e) => { };

testee.Initialize(States.A, stateContainer, stateContainer);
testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions);
testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A);

testee.Fire(Events.B, stateContainer, stateContainer, stateDefinitions);

Expand Down Expand Up @@ -323,8 +259,7 @@ public void OverrideGuardException()

testee.TransitionExceptionThrown += (s, e) => { };

testee.Initialize(States.A, stateContainer, stateContainer);
testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions);
testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A);

overrideExtension.OverriddenException = overriddenException;

Expand Down Expand Up @@ -363,8 +298,7 @@ public void ExceptionThrowingAction()

testee.TransitionExceptionThrown += (s, e) => { };

testee.Initialize(States.A, stateContainer, stateContainer);
testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions);
testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A);

testee.Fire(Events.B, stateContainer, stateContainer, stateDefinitions);

Expand Down Expand Up @@ -411,8 +345,7 @@ public void OverrideActionException()

testee.TransitionExceptionThrown += (s, e) => { };

testee.Initialize(States.A, stateContainer, stateContainer);
testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions);
testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A);

overrideExtension.OverriddenException = overriddenException;

Expand Down Expand Up @@ -454,8 +387,7 @@ public void EntryActionException()

testee.TransitionExceptionThrown += (s, e) => { };

testee.Initialize(States.A, stateContainer, stateContainer);
testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions);
testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A);

testee.Fire(Events.B, stateContainer, stateContainer, stateDefinitions);

Expand Down Expand Up @@ -505,8 +437,7 @@ public void OverrideEntryActionException()

testee.TransitionExceptionThrown += (s, e) => { };

testee.Initialize(States.A, stateContainer, stateContainer);
testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions);
testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A);

overrideExtension.OverriddenException = overriddenException;

Expand Down Expand Up @@ -546,8 +477,7 @@ public void ExitActionException()

testee.TransitionExceptionThrown += (s, e) => { };

testee.Initialize(States.A, stateContainer, stateContainer);
testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions);
testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A);

testee.Fire(Events.B, stateContainer, stateContainer, stateDefinitions);

Expand Down Expand Up @@ -595,8 +525,7 @@ public void OverrideExitActionException()

testee.TransitionExceptionThrown += (s, e) => { };

testee.Initialize(States.A, stateContainer, stateContainer);
testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions);
testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A);

overrideExtension.OverriddenException = overriddenException;

Expand Down Expand Up @@ -634,8 +563,7 @@ public void EntryActionExceptionDuringInitialization()

testee.TransitionExceptionThrown += (s, e) => { };

testee.Initialize(States.A, stateContainer, stateContainer);
testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions);
testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A);

A.CallTo(() => extension.HandlingEntryActionException(
stateContainer,
Expand Down Expand Up @@ -677,14 +605,6 @@ private class OverrideExtension : Extensions.ExtensionBase<States, Events>

public Exception OverriddenException { get; set; }

public override void InitializingStateMachine(IStateMachineInformation<States, Events> stateMachine, ref States initialState)
{
if (this.OverriddenState.HasValue)
{
initialState = this.OverriddenState.Value;
}
}

public override void FiringEvent(IStateMachineInformation<States, Events> stateMachine, ref Events eventId, ref object eventArgument)
{
if (this.OverriddenEvent.HasValue)
Expand Down
Loading

0 comments on commit 7696de3

Please sign in to comment.