@@ -2,8 +2,8 @@ package ru.nsk.kstatemachine
2
2
3
3
import io.kotest.assertions.throwables.shouldThrow
4
4
import io.kotest.core.spec.style.StringSpec
5
+ import io.kotest.matchers.shouldBe
5
6
import kotlinx.coroutines.*
6
- import java.lang.UnsupportedOperationException
7
7
import kotlin.coroutines.EmptyCoroutineContext
8
8
9
9
class CoroutinesTest : StringSpec ({
@@ -50,7 +50,7 @@ class CoroutinesTest : StringSpec({
50
50
shouldThrow<UnsupportedOperationException > {
51
51
createStdLibStateMachine {
52
52
initialState()
53
- onStarted { delay(1 ) }
53
+ onStarted { delay(100 ) }
54
54
}
55
55
}
56
56
}
@@ -76,4 +76,50 @@ class CoroutinesTest : StringSpec({
76
76
scope.cancel()
77
77
}
78
78
}
79
+
80
+ " test context preserving by suspend methods called from threads" test@{
81
+ val thread = Thread .currentThread()
82
+ withContext(Dispatchers .IO ) {
83
+ println("${Thread .currentThread()}")
84
+ createStateMachine(this@test) {
85
+ onStarted { Thread .currentThread() shouldBe thread }
86
+ initialState()
87
+ }
88
+ }
89
+ }
90
+
91
+ " empty context does not preserve machine if suspend methods called from threads" {
92
+ val scope = CoroutineScope (EmptyCoroutineContext )
93
+ withContext(Dispatchers .IO ) {
94
+ val thread = Thread .currentThread()
95
+ createStateMachine(scope) {
96
+ onStarted { Thread .currentThread() shouldBe thread }
97
+ initialState()
98
+ }
99
+ }
100
+ }
101
+
102
+ " threaded context preserving by suspend methods called from threads" {
103
+ val scope = CoroutineScope (Dispatchers .Default .limitedParallelism(1))
104
+ val thread = runBlocking(scope.coroutineContext) { Thread .currentThread() }
105
+
106
+ withContext(Dispatchers .IO ) {
107
+ createStateMachine(scope) {
108
+ onStarted { Thread .currentThread() shouldBe thread }
109
+ initialState()
110
+ }
111
+ }
112
+ }
113
+
114
+ " current thread context preserving by suspend methods called from threads" {
115
+ runBlocking {
116
+ val thread = Thread .currentThread()
117
+ withContext(Dispatchers .IO ) {
118
+ createStateMachine(this@runBlocking) {
119
+ onStarted { Thread .currentThread() shouldBe thread }
120
+ initialState()
121
+ }
122
+ }
123
+ }
124
+ }
79
125
})
0 commit comments