2
2
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3
3
*/
4
4
5
+ import kotlinx.atomicfu.*
5
6
import kotlinx.coroutines.*
6
7
import kotlinx.coroutines.test.*
7
8
import kotlin.coroutines.*
8
9
import kotlin.test.*
9
10
10
- class TestDispatchersTest : TestBase () {
11
+ class TestDispatchersTest {
12
+ private val actionIndex = atomic(0 )
13
+ private val finished = atomic(false )
14
+
15
+ private fun expect (index : Int ) {
16
+ val wasIndex = actionIndex.incrementAndGet()
17
+ println (" expect($index ), wasIndex=$wasIndex " )
18
+ check(index == wasIndex) { " Expecting action index $index but it is actually $wasIndex " }
19
+ }
20
+
21
+ private fun finish (index : Int ) {
22
+ expect(index)
23
+ check(! finished.getAndSet(true )) { " Should call 'finish(...)' at most once" }
24
+ }
11
25
12
26
@BeforeTest
13
27
fun setUp () {
14
28
Dispatchers .resetMain()
15
29
}
16
30
17
31
@Test
18
- fun testSelfSet () = runTest {
32
+ fun testSelfSet () {
19
33
assertFailsWith<IllegalArgumentException > { Dispatchers .setMain(Dispatchers .Main ) }
20
34
}
21
35
22
36
@Test
23
- fun testImmediateDispatcher () = runTest {
37
+ fun testImmediateDispatcher () = runBlockingTest {
24
38
Dispatchers .setMain(ImmediateDispatcher ())
25
39
expect(1 )
26
40
withContext(Dispatchers .Main ) {
@@ -41,7 +55,7 @@ class TestDispatchersTest : TestBase() {
41
55
return false
42
56
}
43
57
44
- override fun dispatch (context : CoroutineContext , block : Runnable ) = expectUnreached( )
58
+ override fun dispatch (context : CoroutineContext , block : Runnable ) = throw RuntimeException ( " Shouldn't be reached " )
45
59
}
46
60
47
61
private inner class RegularDispatcher : CoroutineDispatcher () {
0 commit comments