File tree 6 files changed +25
-20
lines changed
binary-compatibility-validator/reference-public-api
kotlinx-coroutines-core/src/internal
ui/kotlinx-coroutines-android/android-unit-tests/test/ordered/tests
6 files changed +25
-20
lines changed Original file line number Diff line number Diff line change 1
1
public final class kotlinx/coroutines/test/TestDispatchers {
2
2
public static final fun resetMain (Lkotlinx/coroutines/Dispatchers;)V
3
3
public static final fun setMain (Lkotlinx/coroutines/Dispatchers;Lkotlinx/coroutines/CoroutineDispatcher;)V
4
- public static synthetic fun setMain$default (Lkotlinx/coroutines/Dispatchers;Lkotlinx/coroutines/CoroutineDispatcher;ILjava/lang/Object;)V
5
4
}
6
5
Original file line number Diff line number Diff line change @@ -37,8 +37,15 @@ public fun MainDispatcherFactory.tryCreateDispatcher(factories: List<MainDispatc
37
37
MissingMainCoroutineDispatcher (cause, hintOnError())
38
38
}
39
39
40
- private class MissingMainCoroutineDispatcher (private val cause : Throwable ? , private val errorHint : String? = null ) :
41
- MainCoroutineDispatcher (), Delay {
40
+ /* * @suppress */
41
+ @InternalCoroutinesApi
42
+ public fun MainCoroutineDispatcher.isMissing (): Boolean = this is MissingMainCoroutineDispatcher
43
+
44
+ private class MissingMainCoroutineDispatcher (
45
+ private val cause : Throwable ? ,
46
+ private val errorHint : String? = null
47
+ ) : MainCoroutineDispatcher(), Delay {
48
+
42
49
override val immediate: MainCoroutineDispatcher get() = this
43
50
44
51
override fun isDispatchNeeded (context : CoroutineContext ): Boolean {
Original file line number Diff line number Diff line change @@ -15,11 +15,12 @@ import kotlinx.coroutines.test.internal.*
15
15
*
16
16
* It is unsafe to call this method if alive coroutines launched in [Dispatchers.Main] exist.
17
17
*/
18
- public fun Dispatchers.setMain (dispatcher : CoroutineDispatcher = Dispatchers .Unconfined ) {
18
+ @ExperimentalCoroutinesApi
19
+ public fun Dispatchers.setMain (dispatcher : CoroutineDispatcher ) {
20
+ require(dispatcher !is TestMainDispatcher ) { " Dispatchers.setMain(Dispatchers.Main) is prohibited, probably Dispatchers.resetMain() should be used instead" }
19
21
val mainDispatcher = Dispatchers .Main
20
22
require(mainDispatcher is TestMainDispatcher ) { " TestMainDispatcher is not set as main dispatcher, have $mainDispatcher instead." }
21
23
mainDispatcher.setDispatcher(dispatcher)
22
-
23
24
}
24
25
25
26
/* *
@@ -29,6 +30,7 @@ public fun Dispatchers.setMain(dispatcher: CoroutineDispatcher = Dispatchers.Unc
29
30
*
30
31
* It is unsafe to call this method if alive coroutines launched in [Dispatchers.Main] exist.
31
32
*/
33
+ @ExperimentalCoroutinesApi
32
34
public fun Dispatchers.resetMain () {
33
35
val mainDispatcher = Dispatchers .Main
34
36
require(mainDispatcher is TestMainDispatcher ) { " TestMainDispatcher is not set as main dispatcher, have $mainDispatcher instead." }
Original file line number Diff line number Diff line change @@ -17,14 +17,13 @@ internal class TestMainDispatcher(private val mainFactory: MainDispatcherFactory
17
17
private val delegate: CoroutineDispatcher get() {
18
18
if (_delegate != null ) return _delegate !!
19
19
20
- val newInstance = createDispatcher( )
21
- if (newInstance != null ) {
22
- _delegate = newInstance
23
- return newInstance
20
+ val mainDispatcher = mainFactory.tryCreateDispatcher(emptyList() )
21
+ // If we've failed to create a dispatcher, do no set _delegate
22
+ if ( ! mainDispatcher.isMissing()) {
23
+ _delegate = mainDispatcher
24
24
}
25
25
26
- // Return missing dispatcher, but do not set _delegate
27
- return mainFactory.tryCreateDispatcher(emptyList())
26
+ return mainDispatcher
28
27
}
29
28
30
29
@Suppress(" INVISIBLE_MEMBER" )
@@ -60,14 +59,6 @@ internal class TestMainDispatcher(private val mainFactory: MainDispatcherFactory
60
59
public fun resetDispatcher () {
61
60
_delegate = null
62
61
}
63
-
64
- private fun createDispatcher (): CoroutineDispatcher ? {
65
- return try {
66
- mainFactory.createDispatcher(emptyList())
67
- } catch (cause: Throwable ) {
68
- null
69
- }
70
- }
71
62
}
72
63
73
64
internal class TestMainDispatcherFactory : MainDispatcherFactory {
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ package kotlinx.coroutines.test
6
6
import kotlinx.coroutines.*
7
7
import org.junit.*
8
8
import org.junit.Test
9
+ import java.lang.IllegalArgumentException
9
10
import kotlin.coroutines.*
10
11
import kotlin.test.*
11
12
@@ -16,6 +17,11 @@ class TestDispatchersTest : TestBase() {
16
17
Dispatchers .resetMain()
17
18
}
18
19
20
+ @Test(expected = IllegalArgumentException ::class )
21
+ fun testSelfSet () = runTest {
22
+ Dispatchers .setMain(Dispatchers .Main )
23
+ }
24
+
19
25
@Test
20
26
fun testSingleThreadExecutor () = runTest {
21
27
val mainThread = Thread .currentThread()
Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ open class FirstMockedMainTest : TestBase() {
15
15
16
16
@Before
17
17
fun setUp () {
18
- Dispatchers .setMain()
18
+ Dispatchers .setMain(Dispatchers . Unconfined )
19
19
}
20
20
21
21
@After
You can’t perform that action at this time.
0 commit comments