Skip to content

Commit

Permalink
Pass EventRecorder basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nsk90 committed Mar 26, 2024
1 parent 2709b8c commit 62d1849
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 97 deletions.
2 changes: 2 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,8 @@ createStateMachine(scope) {
}
```
The library provides implementation of such throwing handler by `throwingIgnoredEventHandler()` function.
### Pending events
Pending events are such events that are posted for processing while another event is already processing, for example
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ data class RestorationResult(
val results: List<RestoredEventResult>
)

fun RestorationResult.hasNoErrors(): Boolean {
return results.firstOrNull { it.processingResult.isFailure } == null
}

data class RestoredEventResult(
val record: Record,
val processingResult: Result<ProcessingResult>,
Expand Down Expand Up @@ -90,6 +94,10 @@ suspend fun StateMachine.restoreByRecordedEvents(
/**
* May be called on started ([StateMachine.isRunning] == true) [StateMachine] only,
* and returns [RestorationResult] allowing to inspect how the restoration was processed.
*
* There is no way on library side to decide if some exceptions during event processing are errors or not.
* For instance [StateMachine] may be configured with [throwingIgnoredEventHandler] so some exceptions might
* be expected and are not really errors.
*/
suspend fun StateMachine.restoreRunningMachineByRecordedEvents(
recordedEvents: RecordedEvents,
Expand All @@ -113,19 +121,15 @@ suspend fun StateMachine.restoreRunningMachineByRecordedEvents(
}

this as InternalStateMachine
val mutationSection = if (muteListeners) openListenersMutationSection() else EmptyListenersMutationSection

val results = mutableListOf<RestoredEventResult>()
val mutationSection = if (muteListeners) openListenersMutationSection() else EmptyListenersMutationSection
mutationSection.use {
recordedEvents.records.forEach {
val (event, argument) = it.eventAndArgument
if (event is StartEvent && !isRunning) { // fixme always false isRunning is checked on method entry
start(argument)
results += RestoredEventResult(it, Result.success(ProcessingResult.PROCESSED))
} else {
val processingResult = runCatching { processEvent(event, argument) }
results += RestoredEventResult(it, processingResult)
}
for (record in recordedEvents.records) {
val (event, argument) = record.eventAndArgument
if (event is StartEvent)
continue // fixme вызов start мог иметь argument что с ним делать?
val processingResult = runCatching { processEvent(event, argument) }
results += RestoredEventResult(record, processingResult)
}
}
RestorationResult(results)// fixme check processingResults are equal
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ru.nsk.kstatemachine.statemachine

/**
* Returns [StateMachine.IgnoredEventHandler] implementation that throws exception.
* This might be useful if you want to control that all events are handled (not skipped) by your [StateMachine].
*/
fun StateMachine.throwingIgnoredEventHandler(): StateMachine.IgnoredEventHandler {
return StateMachine.IgnoredEventHandler {
error(
"${this@throwingIgnoredEventHandler} received ${it.event} that is going to be ignored. " +
"The machine was configured with ${StateMachine::throwingIgnoredEventHandler::name}, " +
"that forbids such behaviour."
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,92 +33,6 @@ class EventRecorderTest : StringSpec({
) { machine.eventRecorder }
}

"negative check ${StateMachine::restoreRunningMachineByRecordedEvents.name} on not running machine throws" {
val recordedEvents = RecordedEvents(0, emptyList())

val machine = createTestStateMachine(coroutineStarterType, start = false) {
initialState()
shouldThrowWithMessage<IllegalStateException>(
"$machine is not running, ${StateMachine::restoreRunningMachineByRecordedEvents.name}() " +
"operation only makes sense on created and started ${StateMachine::class.simpleName}, " +
"please call it after the machine is started"
) { restoreRunningMachineByRecordedEvents(recordedEvents) }
}
val message = "$machine is not running, ${StateMachine::restoreRunningMachineByRecordedEvents.name}() " +
"operation only makes sense on created and started ${StateMachine::class.simpleName}, " +
"please call it after the machine is started"
shouldThrowWithMessage<IllegalStateException>(message) {
machine.restoreRunningMachineByRecordedEvents(recordedEvents)
}
shouldThrowWithMessage<IllegalStateException>(message) {
machine.restoreRunningMachineByRecordedEventsBlocking(recordedEvents)
}
}

"negative check ${StateMachine::restoreRunningMachineByRecordedEvents.name} on destroyed machine throws" {
val recordedEvents = RecordedEvents(0, emptyList())

val machine = createTestStateMachine(coroutineStarterType) {
initialState()
}
machine.destroy(stop = false)

shouldThrowWithMessage<IllegalStateException>("$machine is already destroyed") {
machine.restoreRunningMachineByRecordedEvents(recordedEvents)
}
}

"negative check ${StateMachine::restoreRunningMachineByRecordedEvents.name} on machine that already processed events throws" {
val recordedEvents = RecordedEvents(0, emptyList())

val machine = createTestStateMachine(coroutineStarterType) {
initialState()
transition<SwitchEvent>()
}
machine.processEvent(SwitchEvent)

shouldThrowWithMessage<IllegalStateException>(
"$machine has already processed events, ${StateMachine::restoreRunningMachineByRecordedEvents.name}() " +
"operation only makes sense on initially clear ${StateMachine::class.simpleName}, please call it before " +
"processing any other events"
) { machine.restoreRunningMachineByRecordedEvents(recordedEvents) }
}

"check event recording preconditions without structure check" {
val machine1 = createTestStateMachine(
coroutineStarterType,
creationArguments = CreationArguments(eventRecordingArguments = EventRecordingArguments())
) {
initialState()
}
val recordedEvents = machine1.eventRecorder.getRecordedEvents()

val machine2 = createTestStateMachine(coroutineStarterType) {
initialState()
}
shouldNotThrowAny {
machine2.restoreByRecordedEventsBlocking(recordedEvents, disableStructureHashCodeCheck = true)
}
}

"check event recording preconditions with structure check" {
val machine1 = createTestStateMachine(
coroutineStarterType,
creationArguments = CreationArguments(eventRecordingArguments = EventRecordingArguments())
) {
initialState()
}
val recordedEvents = machine1.eventRecorder.getRecordedEvents()

val machine2 = createTestStateMachine(
coroutineStarterType,
creationArguments = CreationArguments(eventRecordingArguments = EventRecordingArguments())
) {
initialState()
}
shouldNotThrowAny { machine2.restoreByRecordedEventsBlocking(recordedEvents) }
}

"check recorded events with arguments" {
val machine = createTestStateMachine(
coroutineStarterType,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package ru.nsk.kstatemachine.persistence

import io.kotest.assertions.throwables.shouldNotThrowAny
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.assertions.throwables.shouldThrowWithMessage
import io.kotest.core.spec.style.StringSpec
import io.mockk.called
import io.mockk.verifySequence
import ru.nsk.kstatemachine.*
import ru.nsk.kstatemachine.state.initialState
import ru.nsk.kstatemachine.state.transition
import ru.nsk.kstatemachine.statemachine.StateMachine
import ru.nsk.kstatemachine.statemachine.destroy

class RestoreByRecordedEventsTest : StringSpec({
CoroutineStarterType.entries.forEach { coroutineStarterType ->
"negative check ${StateMachine::restoreRunningMachineByRecordedEvents.name} on not running machine throws" {
val recordedEvents = RecordedEvents(0, emptyList())

val machine = createTestStateMachine(coroutineStarterType, start = false) {
initialState()
shouldThrowWithMessage<IllegalStateException>(
"$machine is not running, ${StateMachine::restoreRunningMachineByRecordedEvents.name}() " +
"operation only makes sense on created and started ${StateMachine::class.simpleName}, " +
"please call it after the machine is started"
) { restoreRunningMachineByRecordedEvents(recordedEvents) }
}
val message = "$machine is not running, ${StateMachine::restoreRunningMachineByRecordedEvents.name}() " +
"operation only makes sense on created and started ${StateMachine::class.simpleName}, " +
"please call it after the machine is started"
shouldThrowWithMessage<IllegalStateException>(message) {
machine.restoreRunningMachineByRecordedEvents(recordedEvents)
}
shouldThrowWithMessage<IllegalStateException>(message) {
machine.restoreRunningMachineByRecordedEventsBlocking(recordedEvents)
}
}

"negative check ${StateMachine::restoreRunningMachineByRecordedEvents.name} on destroyed machine throws" {
val recordedEvents = RecordedEvents(0, emptyList())

val machine = createTestStateMachine(coroutineStarterType) {
initialState()
}
machine.destroy(stop = false)

shouldThrowWithMessage<IllegalStateException>("$machine is already destroyed") {
machine.restoreRunningMachineByRecordedEvents(recordedEvents)
}
}

"negative check ${StateMachine::restoreRunningMachineByRecordedEvents.name} on machine that already processed events throws" {
val recordedEvents = RecordedEvents(0, emptyList())

val machine = createTestStateMachine(coroutineStarterType) {
initialState()
transition<SwitchEvent>()
}
machine.processEvent(SwitchEvent)

shouldThrowWithMessage<IllegalStateException>(
"$machine has already processed events, ${StateMachine::restoreRunningMachineByRecordedEvents.name}() " +
"operation only makes sense on initially clear ${StateMachine::class.simpleName}, please call it before " +
"processing any other events"
) { machine.restoreRunningMachineByRecordedEvents(recordedEvents) }
}

"check event restoration on different machines without structure check" {
val machine1 = createTestStateMachine(
coroutineStarterType,
creationArguments = StateMachine.CreationArguments(eventRecordingArguments = StateMachine.EventRecordingArguments())
) {
initialState()
}
val recordedEvents = machine1.eventRecorder.getRecordedEvents()

val machine2 = createTestStateMachine(coroutineStarterType) {
initialState()
}
shouldNotThrowAny {
machine2.restoreByRecordedEventsBlocking(recordedEvents, disableStructureHashCodeCheck = true)
}
}

"negative check event restoration on different machines throws" {
val machine1 = createTestStateMachine(
coroutineStarterType,
creationArguments = StateMachine.CreationArguments(eventRecordingArguments = StateMachine.EventRecordingArguments())
) {
initialState()
}
val recordedEvents = machine1.eventRecorder.getRecordedEvents()

val machine2 = createTestStateMachine(coroutineStarterType) {
initialState()
}
shouldThrow<IllegalStateException> {
machine2.restoreByRecordedEventsBlocking(recordedEvents)
}
}

"check event recording preconditions with structure check" {
val machine1 = createTestStateMachine(
coroutineStarterType,
creationArguments = StateMachine.CreationArguments(eventRecordingArguments = StateMachine.EventRecordingArguments())
) {
initialState()
}
val recordedEvents = machine1.eventRecorder.getRecordedEvents()

val machine2 = createTestStateMachine(
coroutineStarterType,
creationArguments = StateMachine.CreationArguments(eventRecordingArguments = StateMachine.EventRecordingArguments())
) {
initialState()
}
shouldNotThrowAny { machine2.restoreByRecordedEventsBlocking(recordedEvents) }
}

"restore machine with muted callbacks" {
val callbacks = mockkCallbacks()

val machine1 = createTestStateMachine(
coroutineStarterType,
creationArguments = StateMachine.CreationArguments(eventRecordingArguments = StateMachine.EventRecordingArguments())
) {
initialState()
transition<SwitchEvent>()
}
machine1.processEvent(SwitchEvent)
val recordedEvents = machine1.eventRecorder.getRecordedEvents()

val machine2 = createTestStateMachine(
coroutineStarterType,
creationArguments = StateMachine.CreationArguments(eventRecordingArguments = StateMachine.EventRecordingArguments())
) {
initialState()
transition<SwitchEvent> {
callbacks.listen(this)
}
}
machine2.restoreByRecordedEvents(recordedEvents)
verifySequence {
callbacks wasNot called
}
}

"restore machine with not muted callbacks" {
val callbacks = mockkCallbacks()

val machine1 = createTestStateMachine(
coroutineStarterType,
creationArguments = StateMachine.CreationArguments(eventRecordingArguments = StateMachine.EventRecordingArguments())
) {
initialState()
transition<SwitchEvent>()
}
machine1.processEvent(SwitchEvent)
val recordedEvents = machine1.eventRecorder.getRecordedEvents()

val machine2 = createTestStateMachine(
coroutineStarterType,
creationArguments = StateMachine.CreationArguments(eventRecordingArguments = StateMachine.EventRecordingArguments())
) {
initialState()
transition<SwitchEvent> {
callbacks.listen(this)
}
}
machine2.restoreByRecordedEvents(recordedEvents, muteListeners = false)
verifySequence {
callbacks.onTransitionTriggered(SwitchEvent)
}
}
}
})

0 comments on commit 62d1849

Please sign in to comment.