Skip to content

Commit 49f522e

Browse files
committed
~ Fix FailingCoroutinesMachineryTest
Reset dispatchers after each test in FailingCoroutinesMachineryTest. Otherwise some tests were running normally, but others with dispatchers that were already shutdown.
1 parent ac153f4 commit 49f522e

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

kotlinx-coroutines-core/jvm/test/FailingCoroutinesMachineryTest.kt

+26-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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.
33
*/
44

55
package kotlinx.coroutines
@@ -15,8 +15,21 @@ import kotlin.test.*
1515
@RunWith(Parameterized::class)
1616
class FailingCoroutinesMachineryTest(
1717
private val element: CoroutineContext.Element,
18-
private val dispatcher: CoroutineDispatcher
18+
private val dispatcher: TestDispatcher
1919
) : 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+
}
2033

2134
private var caught: Throwable? = null
2235
private val latch = CountDownLatch(1)
@@ -75,7 +88,7 @@ class FailingCoroutinesMachineryTest(
7588

7689
@After
7790
fun tearDown() {
78-
runCatching { (dispatcher as? ExecutorCoroutineDispatcher)?.close() }
91+
dispatcher.reset()
7992
if (lazyOuterDispatcher.isInitialized()) lazyOuterDispatcher.value.close()
8093
}
8194

@@ -84,14 +97,14 @@ class FailingCoroutinesMachineryTest(
8497
@Parameterized.Parameters(name = "Element: {0}, dispatcher: {1}")
8598
fun dispatchers(): List<Array<Any>> {
8699
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 }
93107
)
94-
95108
return elements.flatMap { element ->
96109
dispatchers.map { dispatcher ->
97110
arrayOf(element, dispatcher)
@@ -102,13 +115,13 @@ class FailingCoroutinesMachineryTest(
102115

103116
@Test
104117
fun testElement() = runTest {
105-
launch(NonCancellable + dispatcher + exceptionHandler + element) {}
118+
launch(NonCancellable + dispatcher.value + exceptionHandler + element) {}
106119
checkException()
107120
}
108121

109122
@Test
110123
fun testNestedElement() = runTest {
111-
launch(NonCancellable + dispatcher + exceptionHandler) {
124+
launch(NonCancellable + dispatcher.value + exceptionHandler) {
112125
launch(element) { }
113126
}
114127
checkException()
@@ -117,7 +130,7 @@ class FailingCoroutinesMachineryTest(
117130
@Test
118131
fun testNestedDispatcherAndElement() = runTest {
119132
launch(lazyOuterDispatcher.value + NonCancellable + exceptionHandler) {
120-
launch(element + dispatcher) { }
133+
launch(element + dispatcher.value) { }
121134
}
122135
checkException()
123136
}

0 commit comments

Comments
 (0)