Skip to content

Commit e437c9e

Browse files
committed
Fix falling tests and pending event handler behaviour
Fix a bug that machine was processing pending event if PendingEventHandler was set to no-throwing implementation.
1 parent 3319bb2 commit e437c9e

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

kstatemachine/src/main/kotlin/ru/nsk/kstatemachine/StateMachineImpl.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,14 @@ internal class StateMachineImpl(name: String?, childMode: ChildMode, override va
106106
check(!isDestroyed) { "$this is already destroyed" }
107107
check(isRunning) { "$this is not started, call start() first" }
108108

109+
if (isProcessingEvent) {
110+
pendingEventHandler.onPendingEvent(event, argument)
111+
// pending event cannot be processed while previous event is still processing
112+
// even if PendingEventHandler does not throw
113+
return
114+
}
115+
109116
runCheckingExceptions {
110-
if (isProcessingEvent)
111-
pendingEventHandler.onPendingEvent(event, argument)
112117
isProcessingEvent = true
113118

114119
try {

kstatemachine/src/test/kotlin/ru/nsk/kstatemachine/ListenerExceptionHandlerTest.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,11 @@ class ListenerExceptionHandlerTest : StringSpec({
3737

3838
val machine = createStateMachine(start = false) {
3939
onStarted { callbacks.onStarted(this) }
40-
41-
onEntry {
42-
callbacks.listen(this)
43-
error("test exception")
44-
}
40+
callbacks.listen(this)
41+
onEntry { error("test exception") }
4542

4643
state1 = initialState {
47-
onEntry { callbacks.listen(this) }
44+
callbacks.listen(this)
4845
}
4946
}
5047
shouldThrow<IllegalStateException> { machine.start() }

kstatemachine/src/test/kotlin/ru/nsk/kstatemachine/StateMachineTest.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,10 @@ class StateMachineTest : StringSpec({
167167
shouldThrowUnit<IllegalStateException> { machine.setInitialState(first) }
168168
}
169169

170-
"pending event handler" {
170+
"throwing PendingEventHandler does not destroy machine" {
171171
val machine = createStateMachine {
172+
logger = StateMachine.Logger { println(it) }
173+
172174
val second = state("second")
173175
initialState("first") {
174176
transition<SwitchEvent> {
@@ -184,8 +186,8 @@ class StateMachineTest : StringSpec({
184186
}
185187
}
186188

187-
shouldThrow<IllegalStateException> { machine.processEvent(SwitchEvent) }
188-
machine.isDestroyed shouldBe true
189+
machine.processEvent(SwitchEvent)
190+
machine.isDestroyed shouldBe false
189191
}
190192

191193
"process event before started" {

0 commit comments

Comments
 (0)