@@ -4,12 +4,14 @@ import io.kotest.assertions.throwables.shouldNotThrowAny
44import io.kotest.assertions.throwables.shouldThrow
55import io.kotest.assertions.throwables.shouldThrowWithMessage
66import io.kotest.core.spec.style.StringSpec
7+ import io.kotest.matchers.collections.shouldContainExactly
8+ import io.kotest.matchers.shouldBe
79import io.mockk.called
810import io.mockk.verifySequence
911import ru.nsk.kstatemachine.*
10- import ru.nsk.kstatemachine.state.initialState
11- import ru.nsk.kstatemachine.state.transition
12+ import ru.nsk.kstatemachine.state.*
1213import ru.nsk.kstatemachine.statemachine.StateMachine
14+ import ru.nsk.kstatemachine.statemachine.StateMachine.*
1315import ru.nsk.kstatemachine.statemachine.destroy
1416
1517class RestoreByRecordedEventsTest : StringSpec ({
@@ -68,7 +70,7 @@ class RestoreByRecordedEventsTest : StringSpec({
6870 " check event restoration on different machines without structure check" {
6971 val machine1 = createTestStateMachine(
7072 coroutineStarterType,
71- creationArguments = StateMachine . CreationArguments (eventRecordingArguments = StateMachine . EventRecordingArguments ())
73+ creationArguments = CreationArguments (eventRecordingArguments = EventRecordingArguments ())
7274 ) {
7375 initialState()
7476 }
@@ -85,7 +87,7 @@ class RestoreByRecordedEventsTest : StringSpec({
8587 " negative check event restoration on different machines throws" {
8688 val machine1 = createTestStateMachine(
8789 coroutineStarterType,
88- creationArguments = StateMachine . CreationArguments (eventRecordingArguments = StateMachine . EventRecordingArguments ())
90+ creationArguments = CreationArguments (eventRecordingArguments = EventRecordingArguments ())
8991 ) {
9092 initialState()
9193 }
@@ -102,15 +104,15 @@ class RestoreByRecordedEventsTest : StringSpec({
102104 " check event recording preconditions with structure check" {
103105 val machine1 = createTestStateMachine(
104106 coroutineStarterType,
105- creationArguments = StateMachine . CreationArguments (eventRecordingArguments = StateMachine . EventRecordingArguments ())
107+ creationArguments = CreationArguments (eventRecordingArguments = EventRecordingArguments ())
106108 ) {
107109 initialState()
108110 }
109111 val recordedEvents = machine1.eventRecorder.getRecordedEvents()
110112
111113 val machine2 = createTestStateMachine(
112114 coroutineStarterType,
113- creationArguments = StateMachine . CreationArguments (eventRecordingArguments = StateMachine . EventRecordingArguments ())
115+ creationArguments = CreationArguments (eventRecordingArguments = EventRecordingArguments ())
114116 ) {
115117 initialState()
116118 }
@@ -122,7 +124,7 @@ class RestoreByRecordedEventsTest : StringSpec({
122124
123125 val machine1 = createTestStateMachine(
124126 coroutineStarterType,
125- creationArguments = StateMachine . CreationArguments (eventRecordingArguments = StateMachine . EventRecordingArguments ())
127+ creationArguments = CreationArguments (eventRecordingArguments = EventRecordingArguments ())
126128 ) {
127129 initialState()
128130 transition<SwitchEvent >()
@@ -132,7 +134,7 @@ class RestoreByRecordedEventsTest : StringSpec({
132134
133135 val machine2 = createTestStateMachine(
134136 coroutineStarterType,
135- creationArguments = StateMachine . CreationArguments (eventRecordingArguments = StateMachine . EventRecordingArguments ())
137+ creationArguments = CreationArguments (eventRecordingArguments = EventRecordingArguments ())
136138 ) {
137139 initialState()
138140 transition<SwitchEvent > {
@@ -150,7 +152,7 @@ class RestoreByRecordedEventsTest : StringSpec({
150152
151153 val machine1 = createTestStateMachine(
152154 coroutineStarterType,
153- creationArguments = StateMachine . CreationArguments (eventRecordingArguments = StateMachine . EventRecordingArguments ())
155+ creationArguments = CreationArguments (eventRecordingArguments = EventRecordingArguments ())
154156 ) {
155157 initialState()
156158 transition<SwitchEvent >()
@@ -160,7 +162,7 @@ class RestoreByRecordedEventsTest : StringSpec({
160162
161163 val machine2 = createTestStateMachine(
162164 coroutineStarterType,
163- creationArguments = StateMachine . CreationArguments (eventRecordingArguments = StateMachine . EventRecordingArguments ())
165+ creationArguments = CreationArguments (eventRecordingArguments = EventRecordingArguments ())
164166 ) {
165167 initialState()
166168 transition<SwitchEvent > {
@@ -172,5 +174,90 @@ class RestoreByRecordedEventsTest : StringSpec({
172174 callbacks.onTransitionTriggered(SwitchEvent )
173175 }
174176 }
177+
178+ " restore the machine that is not running yet (processes all events as pending)" {
179+ val machine1 = createTestStateMachine(
180+ coroutineStarterType,
181+ creationArguments = CreationArguments (eventRecordingArguments = EventRecordingArguments ())
182+ ) {
183+ initialState()
184+ val state = state()
185+ transition<SwitchEvent >(targetState = state)
186+ }
187+ machine1.processEvent(SwitchEvent )
188+ val recordedEvents = machine1.eventRecorder.getRecordedEvents()
189+
190+ lateinit var state : State
191+ val machine2 = createTestStateMachine(
192+ coroutineStarterType,
193+ start = false,
194+ creationArguments = CreationArguments (eventRecordingArguments = EventRecordingArguments ())
195+ ) {
196+ initialState()
197+ state = state()
198+ transition<SwitchEvent >(targetState = state)
199+ }
200+ machine2.restoreByRecordedEvents(recordedEvents, muteListeners = false)
201+ machine2.start()
202+ machine2.activeStates().shouldContainExactly(state)
203+ }
204+
205+ " restore the machine that is not running yet with non queued PendingEventHandler (processes all events as pending)" {
206+ val machine1 = createTestStateMachine(
207+ coroutineStarterType,
208+ creationArguments = CreationArguments (eventRecordingArguments = EventRecordingArguments ())
209+ ) {
210+ pendingEventHandler = PendingEventHandler {}
211+ initialState()
212+ val state = state()
213+ transition<SwitchEvent >(targetState = state)
214+ }
215+ machine1.processEvent(SwitchEvent )
216+ val recordedEvents = machine1.eventRecorder.getRecordedEvents()
217+
218+ lateinit var state : State
219+ val machine2 = createTestStateMachine(
220+ coroutineStarterType,
221+ start = false,
222+ creationArguments = CreationArguments (eventRecordingArguments = EventRecordingArguments ())
223+ ) {
224+ pendingEventHandler = PendingEventHandler {}
225+ initialState()
226+ state = state()
227+ transition<SwitchEvent >(targetState = state)
228+ }
229+ machine2.restoreByRecordedEvents(recordedEvents, muteListeners = false)
230+ val exception = shouldThrow<RestorationResultValidationException > {
231+ machine2.start()
232+ }
233+ exception.result.results.single().warnings.single().warningType shouldBe WarningType .PendingEventMightBeIgnored
234+ }
235+
236+ " restore the machine that is not running yet with default QueuedPendingEventHandler (processes all events as pending)" {
237+ val machine1 = createTestStateMachine(
238+ coroutineStarterType,
239+ creationArguments = CreationArguments (eventRecordingArguments = EventRecordingArguments ())
240+ ) {
241+ initialState()
242+ val state = state()
243+ transition<SwitchEvent >(targetState = state)
244+ }
245+ machine1.processEvent(SwitchEvent )
246+ val recordedEvents = machine1.eventRecorder.getRecordedEvents()
247+
248+ lateinit var state : State
249+ val machine2 = createTestStateMachine(
250+ coroutineStarterType,
251+ start = false,
252+ creationArguments = CreationArguments (eventRecordingArguments = EventRecordingArguments ())
253+ ) {
254+ initialState()
255+ state = state()
256+ transition<SwitchEvent >(targetState = state)
257+ }
258+ machine2.restoreByRecordedEvents(recordedEvents, muteListeners = false)
259+ machine2.start()
260+ machine2.activeStates().shouldContainExactly(state)
261+ }
175262 }
176263})
0 commit comments