Skip to content

Commit

Permalink
add loaded events to IExtension method Loaded(..) for Async SM
Browse files Browse the repository at this point in the history
  • Loading branch information
wtjerry committed Oct 3, 2019
1 parent 6bf0099 commit 238749f
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 17 deletions.
64 changes: 57 additions & 7 deletions source/Appccelerate.StateMachine.Specs/Async/Persisting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,17 +257,41 @@ public void LoadingEventsForPassiveStateMachine(

"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>>
{
new EventInformation<int>(2, null),
new EventInformation<int>(3, null),
firstEvent,
secondEvent,
});
loader.SetPriorityEvents(new List<EventInformation<int>>
{
new EventInformation<int>(1, null)
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 () =>
Expand Down Expand Up @@ -377,17 +401,41 @@ public void LoadingEventsForActiveStateMachine(

"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>>
{
new EventInformation<int>(2, null),
new EventInformation<int>(3, null),
firstEvent,
secondEvent,
});
loader.SetPriorityEvents(new List<EventInformation<int>>
{
new EventInformation<int>(1, null)
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 () =>
Expand Down Expand Up @@ -484,7 +532,9 @@ public class FakeExtension : AsyncExtensionBase<State, Event>
public override Task Loaded(
IStateMachineInformation<State, Event> stateMachineInformation,
IInitializable<State> loadedCurrentState,
IReadOnlyDictionary<State, State> loadedHistoryStates)
IReadOnlyDictionary<State, State> loadedHistoryStates,
IReadOnlyCollection<EventInformation<Event>> events,
IReadOnlyCollection<EventInformation<Event>> priorityEvents)
{
this.LoadedCurrentState.Add(loadedCurrentState);
return Task.CompletedTask;
Expand Down
4 changes: 3 additions & 1 deletion source/Appccelerate.StateMachine/AsyncActiveStateMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,9 @@ void NotifyExtensions()
this.stateContainer.Extensions.ForEach(
extension => extension.Loaded(
loadedCurrentState,
historyStates));
historyStates,
events,
priorityEvents));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,9 @@ public virtual Task EnteringState(
public virtual Task Loaded(
IStateMachineInformation<TState, TEvent> stateMachineInformation,
IInitializable<TState> loadedCurrentState,
IReadOnlyDictionary<TState, TState> loadedHistoryStates)
IReadOnlyDictionary<TState, TState> loadedHistoryStates,
IReadOnlyCollection<EventInformation<TEvent>> events,
IReadOnlyCollection<EventInformation<TEvent>> priorityEvents)
{
return TaskEx.Completed;
}
Expand Down
4 changes: 3 additions & 1 deletion source/Appccelerate.StateMachine/AsyncMachine/IExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ Task EnteringState(
Task Loaded(
IStateMachineInformation<TState, TEvent> stateMachineInformation,
IInitializable<TState> loadedCurrentState,
IReadOnlyDictionary<TState, TState> loadedHistoryStates);
IReadOnlyDictionary<TState, TState> loadedHistoryStates,
IReadOnlyCollection<EventInformation<TEvent>> events,
IReadOnlyCollection<EventInformation<TEvent>> priorityEvents);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ Task ExecutedTransition(

Task Loaded(
IInitializable<TState> loadedCurrentState,
IReadOnlyDictionary<TState, TState> loadedHistoryStates);
IReadOnlyDictionary<TState, TState> loadedHistoryStates,
IReadOnlyCollection<EventInformation<TEvent>> events,
IReadOnlyCollection<EventInformation<TEvent>> priorityEvents);

Task EnteringState(
IStateDefinition<TState, TEvent> stateDefinition,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,11 @@ public Task ExecutedTransition(

public Task Loaded(
IInitializable<TState> loadedCurrentState,
IReadOnlyDictionary<TState, TState> loadedHistoryStates)
IReadOnlyDictionary<TState, TState> loadedHistoryStates,
IReadOnlyCollection<EventInformation<TEvent>> events,
IReadOnlyCollection<EventInformation<TEvent>> priorityEvents)
{
return this.apiExtension.Loaded(this.stateMachineInformation, loadedCurrentState, loadedHistoryStates);
return this.apiExtension.Loaded(this.stateMachineInformation, loadedCurrentState, loadedHistoryStates, events, priorityEvents);
}

public Task EnteringState(IStateDefinition<TState, TEvent> stateDefinition, ITransitionContext<TState, TEvent> context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ public virtual Task ExecutedTransition(

public virtual Task Loaded(
IInitializable<TState> loadedCurrentState,
IReadOnlyDictionary<TState, TState> loadedHistoryStates)
IReadOnlyDictionary<TState, TState> loadedHistoryStates,
IReadOnlyCollection<EventInformation<TEvent>> events,
IReadOnlyCollection<EventInformation<TEvent>> priorityEvents)
{
return TaskEx.Completed;
}
Expand Down
4 changes: 3 additions & 1 deletion source/Appccelerate.StateMachine/AsyncPassiveStateMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,9 @@ void NotifyExtensions()
this.stateContainer.Extensions.ForEach(
extension => extension.Loaded(
loadedCurrentState,
historyStates));
historyStates,
events,
priorityEvents));
}
}

Expand Down
4 changes: 2 additions & 2 deletions source/Appccelerate.StateMachine/IAsyncStateMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,14 @@ public interface IAsyncStateMachine<TState, TEvent>
void Report(IStateMachineReport<TState, TEvent> reportGenerator);

/// <summary>
/// Saves the current state and history states to a persisted state. Can be restored using <see cref="Load(Appccelerate.StateMachine.Persistence.IAsyncStateMachineLoader{TState})"/>.
/// Saves the current state and history states to a persisted state. Can be restored using <see cref="Load(IAsyncStateMachineLoader{TState,TEvent})"/>.
/// </summary>
/// <param name="stateMachineSaver">Data to be persisted is passed to the saver.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
Task Save(IAsyncStateMachineSaver<TState, TEvent> stateMachineSaver);

/// <summary>
/// Loads the current state and history states from a persisted state (<see cref="Save(Appccelerate.StateMachine.Persistence.IAsyncStateMachineSaver{TState})"/>).
/// Loads the current state and history states from a persisted state (<see cref="Save(IAsyncStateMachineSaver{TState, TEvent})"/>).
/// The loader should return exactly the data that was passed to the saver.
/// </summary>
/// <param name="stateMachineLoader">Loader providing persisted data.</param>
Expand Down

0 comments on commit 238749f

Please sign in to comment.