1
1
package kotlinx.coroutines.test
2
2
3
- import kotlinx.coroutines.CoroutineScope
4
- import kotlinx.coroutines.delay
5
- import kotlinx.coroutines.launch
3
+ import kotlinx.coroutines.*
6
4
import org.junit.Test
7
5
import java.util.concurrent.TimeUnit
8
6
import kotlin.test.assertEquals
7
+ import kotlin.test.assertNotSame
8
+ import kotlin.test.assertSame
9
9
10
10
class TestCoroutineDispatcherTest {
11
11
@Test
@@ -100,4 +100,42 @@ class TestCoroutineDispatcherTest {
100
100
}
101
101
subject.cleanupTestCoroutines()
102
102
}
103
+
104
+ @Test
105
+ fun whenDispatchCalled_runsOnCurrentThread () {
106
+ val currentThread = Thread .currentThread()
107
+ val subject = TestCoroutineDispatcher ()
108
+ val scope = TestCoroutineScope (subject)
109
+
110
+ val deferred = scope.async(Dispatchers .Default ) {
111
+ withContext(subject) {
112
+ assertNotSame(currentThread, Thread .currentThread())
113
+ 3
114
+ }
115
+ }
116
+
117
+ runBlocking {
118
+ // just to ensure the above code terminates
119
+ assertEquals(3 , deferred.await())
120
+ }
121
+ }
122
+
123
+ @Test
124
+ fun whenAllDispatchersMocked_runsOnSameThread () {
125
+ val currentThread = Thread .currentThread()
126
+ val subject = TestCoroutineDispatcher ()
127
+ val scope = TestCoroutineScope (subject)
128
+
129
+ val deferred = scope.async(subject) {
130
+ withContext(subject) {
131
+ assertSame(currentThread, Thread .currentThread())
132
+ 3
133
+ }
134
+ }
135
+
136
+ runBlocking {
137
+ // just to ensure the above code terminates
138
+ assertEquals(3 , deferred.await())
139
+ }
140
+ }
103
141
}
0 commit comments