Skip to content

Commit a69f736

Browse files
committed
Add test about threading for immediate dispatch.
1 parent f1f7057 commit a69f736

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

core/kotlinx-coroutines-test/test/TestCoroutineDispatcherTest.kt

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package kotlinx.coroutines.test
22

3-
import kotlinx.coroutines.CoroutineScope
4-
import kotlinx.coroutines.delay
5-
import kotlinx.coroutines.launch
3+
import kotlinx.coroutines.*
64
import org.junit.Test
75
import java.util.concurrent.TimeUnit
86
import kotlin.test.assertEquals
7+
import kotlin.test.assertNotSame
8+
import kotlin.test.assertSame
99

1010
class TestCoroutineDispatcherTest {
1111
@Test
@@ -100,4 +100,42 @@ class TestCoroutineDispatcherTest {
100100
}
101101
subject.cleanupTestCoroutines()
102102
}
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+
}
103141
}

0 commit comments

Comments
 (0)