@@ -7,7 +7,9 @@ package kotlinx.coroutines
7
7
import kotlinx.coroutines.internal.*
8
8
import kotlinx.coroutines.scheduling.*
9
9
import org.junit.*
10
+ import java.util.*
10
11
import java.util.concurrent.atomic.*
12
+ import kotlin.test.*
11
13
12
14
private val VERBOSE = systemProp(" test.verbose" , false )
13
15
@@ -48,6 +50,12 @@ public actual open class TestBase actual constructor() {
48
50
private var finished = AtomicBoolean ()
49
51
private var error = AtomicReference <Throwable >()
50
52
53
+ // Shutdown sequence
54
+ private lateinit var threadsBefore: Set <Thread >
55
+ private val uncaughtExceptions = Collections .synchronizedList(ArrayList <Throwable >())
56
+ private var originalUncaughtExceptionHandler: Thread .UncaughtExceptionHandler ? = null
57
+ private val SHUTDOWN_TIMEOUT = 10_000L // 10s at most to wait
58
+
51
59
/* *
52
60
* Throws [IllegalStateException] like `error` in stdlib, but also ensures that the test will not
53
61
* complete successfully even if this exception is consumed somewhere in the test.
@@ -105,13 +113,12 @@ public actual open class TestBase actual constructor() {
105
113
finished.set(false )
106
114
}
107
115
108
- private lateinit var threadsBefore: Set <Thread >
109
- private val SHUTDOWN_TIMEOUT = 10_000L // 10s at most to wait
110
-
111
116
@Before
112
117
fun before () {
113
118
initPoolsBeforeTest()
114
119
threadsBefore = currentThreads()
120
+ originalUncaughtExceptionHandler = Thread .getDefaultUncaughtExceptionHandler()
121
+ Thread .setDefaultUncaughtExceptionHandler({ t, e -> uncaughtExceptions.add(e) })
115
122
}
116
123
117
124
@After
@@ -120,6 +127,8 @@ public actual open class TestBase actual constructor() {
120
127
check(actionIndex.get() == 0 || finished.get()) { " Expecting that 'finish(...)' was invoked, but it was not" }
121
128
shutdownPoolsAfterTest()
122
129
checkTestThreads(threadsBefore)
130
+ Thread .setDefaultUncaughtExceptionHandler(originalUncaughtExceptionHandler)
131
+ assertTrue(uncaughtExceptions.isEmpty(), " Expected no uncaught exceptions, but got $uncaughtExceptions " )
123
132
}
124
133
125
134
fun initPoolsBeforeTest () {
0 commit comments