1
1
/*
2
- * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
2
+ * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3
3
*/
4
4
5
5
package kotlinx.coroutines
@@ -15,8 +15,21 @@ import kotlin.test.*
15
15
@RunWith(Parameterized ::class )
16
16
class FailingCoroutinesMachineryTest (
17
17
private val element : CoroutineContext .Element ,
18
- private val dispatcher : CoroutineDispatcher
18
+ private val dispatcher : TestDispatcher
19
19
) : TestBase() {
20
+ class TestDispatcher (val name : String , val block : () -> CoroutineDispatcher ) {
21
+ private var _value : CoroutineDispatcher ? = null
22
+
23
+ val value: CoroutineDispatcher
24
+ get() = _value ? : block().also { _value = it }
25
+
26
+ override fun toString (): String = name
27
+
28
+ fun reset () {
29
+ runCatching { (_value as ? ExecutorCoroutineDispatcher )?.close() }
30
+ _value = null
31
+ }
32
+ }
20
33
21
34
private var caught: Throwable ? = null
22
35
private val latch = CountDownLatch (1 )
@@ -75,7 +88,7 @@ class FailingCoroutinesMachineryTest(
75
88
76
89
@After
77
90
fun tearDown () {
78
- runCatching { ( dispatcher as ? ExecutorCoroutineDispatcher )?.close() }
91
+ dispatcher.reset()
79
92
if (lazyOuterDispatcher.isInitialized()) lazyOuterDispatcher.value.close()
80
93
}
81
94
@@ -84,14 +97,14 @@ class FailingCoroutinesMachineryTest(
84
97
@Parameterized.Parameters (name = " Element: {0}, dispatcher: {1}" )
85
98
fun dispatchers (): List <Array <Any >> {
86
99
val elements = listOf<Any >(FailingRestore , FailingUpdate )
87
- val dispatchers = listOf<Any >(
88
- Dispatchers .Unconfined ,
89
- Dispatchers .Default ,
90
- Executors .newFixedThreadPool(1 ).asCoroutineDispatcher(),
91
- Executors .newScheduledThreadPool(1 ).asCoroutineDispatcher(),
92
- ThrowingDispatcher , ThrowingDispatcher2
100
+ val dispatchers = listOf<TestDispatcher >(
101
+ TestDispatcher (" Dispatchers.Unconfined" ) { Dispatchers .Unconfined },
102
+ TestDispatcher (" Dispatchers.Default" ) { Dispatchers .Default },
103
+ TestDispatcher (" Executors.newFixedThreadPool(1)" ) { Executors .newFixedThreadPool(1 ).asCoroutineDispatcher() },
104
+ TestDispatcher (" Executors.newScheduledThreadPool(1)" ) { Executors .newScheduledThreadPool(1 ).asCoroutineDispatcher() },
105
+ TestDispatcher (" ThrowingDispatcher" ) { ThrowingDispatcher },
106
+ TestDispatcher (" ThrowingDispatcher2" ) { ThrowingDispatcher2 }
93
107
)
94
-
95
108
return elements.flatMap { element ->
96
109
dispatchers.map { dispatcher ->
97
110
arrayOf(element, dispatcher)
@@ -102,13 +115,13 @@ class FailingCoroutinesMachineryTest(
102
115
103
116
@Test
104
117
fun testElement () = runTest {
105
- launch(NonCancellable + dispatcher + exceptionHandler + element) {}
118
+ launch(NonCancellable + dispatcher.value + exceptionHandler + element) {}
106
119
checkException()
107
120
}
108
121
109
122
@Test
110
123
fun testNestedElement () = runTest {
111
- launch(NonCancellable + dispatcher + exceptionHandler) {
124
+ launch(NonCancellable + dispatcher.value + exceptionHandler) {
112
125
launch(element) { }
113
126
}
114
127
checkException()
@@ -117,7 +130,7 @@ class FailingCoroutinesMachineryTest(
117
130
@Test
118
131
fun testNestedDispatcherAndElement () = runTest {
119
132
launch(lazyOuterDispatcher.value + NonCancellable + exceptionHandler) {
120
- launch(element + dispatcher) { }
133
+ launch(element + dispatcher.value ) { }
121
134
}
122
135
checkException()
123
136
}
0 commit comments