-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
208 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
kstatemachine/src/commonMain/kotlin/ru/nsk/kstatemachine/statemachine/IgnoredEventHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
tests/src/commonTest/kotlin/ru/nsk/kstatemachine/persistence/RestoreByRecordedEventsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
}) |