Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix orchestrated/recursive state computation to ensure proper state updates #347

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import com.fraktalio.fmodel.application.examples.numbers.even.command.evenNumber
import com.fraktalio.fmodel.application.examples.numbers.even.command.evenNumberStateRepository
import com.fraktalio.fmodel.application.examples.numbers.numberStateRepository
import com.fraktalio.fmodel.domain.IDecider
import com.fraktalio.fmodel.domain.ISaga
import com.fraktalio.fmodel.domain.combine
import com.fraktalio.fmodel.domain.examples.numbers.api.Description
import com.fraktalio.fmodel.domain.examples.numbers.api.EvenNumberState
import com.fraktalio.fmodel.domain.examples.numbers.api.NumberCommand.EvenNumberCommand.AddEvenNumber
import com.fraktalio.fmodel.domain.examples.numbers.api.NumberValue
import com.fraktalio.fmodel.domain.examples.numbers.api.OddNumberState
import com.fraktalio.fmodel.domain.examples.numbers.even.command.evenNumberDecider
import com.fraktalio.fmodel.domain.examples.numbers.numberSaga
import com.fraktalio.fmodel.domain.examples.numbers.odd.command.oddNumberDecider
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.FunSpec
Expand All @@ -38,6 +40,16 @@ private suspend fun <C, S, E, V> IDecider<C, S, E>.given(
stateRepository = repository
).handleOptimistically(command())

private suspend fun <C, S, E> IDecider<C, S, E>.given(
repository: StateRepository<C, S>,
saga: ISaga<E, C>,
command: () -> C
) = StateStoredOrchestratingAggregate(
decider = this,
saga = saga,
stateRepository = repository
).handle(command())

/**
* DSL - When
*/
Expand All @@ -61,6 +73,7 @@ class StateStoredAggregateTest : FunSpec({
val evenNumberStateRepository = evenNumberStateRepository() as EvenNumberStateRepository
val evenNumberLockingStateRepository = evenNumberLockingStateRepository() as EvenNumberLockingStateRepository
val numberStateRepository = numberStateRepository() as NumberStateRepository
val numberSaga = numberSaga()

test("State-stored aggregate - add even number") {
with(evenDecider) {
Expand Down Expand Up @@ -105,4 +118,16 @@ class StateStoredAggregateTest : FunSpec({
)
}
}

test("Orchestrated state-stored aggregate - add even and odd number") {
with(combinedDecider) {
numberStateRepository.deleteAll()
given(numberStateRepository, numberSaga) {
whenCommand(AddEvenNumber(Description("4"), NumberValue(4)))
} thenState Pair(
EvenNumberState(Description("4"), NumberValue(4)),
OddNumberState(Description("3"), NumberValue(3))
)
}
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ interface StateOrchestratingComputation<C, S, E> : ISaga<E, C>, StateComputation
override suspend fun S?.computeNewState(command: C): S {
val currentState = this ?: initialState
val events = decide(command, currentState)
val newState = events.fold(currentState) { s, e -> evolve(s, e) }
events.flatMapConcat { react(it) }.onEach { newState.computeNewState(it) }.collect()
var newState = events.fold(currentState) { s, e -> evolve(s, e) }
events.flatMapConcat { react(it) }.collect {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Thanks, Domenic.

newState = newState.computeNewState(it)
}
return newState
}
}
Expand Down
Loading