@@ -62,13 +62,20 @@ public actual open class TestBase actual constructor() {
62
62
*/
63
63
@Suppress(" ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS" )
64
64
public actual fun error (message : Any , cause : Throwable ? = null): Nothing {
65
- val exception = IllegalStateException (message.toString(), cause)
65
+ throw makeError(message, cause)
66
+ }
67
+
68
+ private fun makeError (message : Any , cause : Throwable ? = null): IllegalStateException =
69
+ IllegalStateException (message.toString(), cause).also {
70
+ setError(it)
71
+ }
72
+
73
+ private fun setError (exception : Throwable ) {
66
74
error.compareAndSet(null , exception)
67
- throw exception
68
75
}
69
76
70
77
private fun printError (message : String , cause : Throwable ) {
71
- error.compareAndSet( null , cause)
78
+ setError( cause)
72
79
println (" $message : $cause " )
73
80
cause.printStackTrace(System .out )
74
81
println (" --- Detected at ---" )
@@ -118,21 +125,35 @@ public actual open class TestBase actual constructor() {
118
125
initPoolsBeforeTest()
119
126
threadsBefore = currentThreads()
120
127
originalUncaughtExceptionHandler = Thread .getDefaultUncaughtExceptionHandler()
121
- Thread .setDefaultUncaughtExceptionHandler( { t, e ->
122
- println (" Uncaught exception in thread $t : $e " )
128
+ Thread .setDefaultUncaughtExceptionHandler { t, e ->
129
+ println (" Exception in thread $t : $e " ) // The same message as in default handler
123
130
e.printStackTrace()
124
131
uncaughtExceptions.add(e)
125
- })
132
+ }
126
133
}
127
134
128
135
@After
129
136
fun onCompletion () {
130
- error.get()?.let { throw it }
131
- check(actionIndex.get() == 0 || finished.get()) { " Expecting that 'finish(...)' was invoked, but it was not" }
137
+ // onCompletion should not throw exceptions before it finishes all cleanup, so that other tests always
138
+ // start in a clear, restored state
139
+ if (actionIndex.get() != 0 && ! finished.get()) {
140
+ makeError(" Expecting that 'finish(...)' was invoked, but it was not" )
141
+ }
142
+ // Shutdown all thread pools
132
143
shutdownPoolsAfterTest()
133
- checkTestThreads(threadsBefore)
144
+ // Check that that are now leftover threads
145
+ runCatching {
146
+ checkTestThreads(threadsBefore)
147
+ }.onFailure {
148
+ setError(it)
149
+ }
150
+ // Restore original uncaught exception handler
134
151
Thread .setDefaultUncaughtExceptionHandler(originalUncaughtExceptionHandler)
135
- assertTrue(uncaughtExceptions.isEmpty(), " Expected no uncaught exceptions, but got $uncaughtExceptions " )
152
+ if (uncaughtExceptions.isNotEmpty()) {
153
+ makeError(" Expected no uncaught exceptions, but got $uncaughtExceptions " )
154
+ }
155
+ // The very last action -- throw error if any was detected
156
+ error.get()?.let { throw it }
136
157
}
137
158
138
159
fun initPoolsBeforeTest () {
0 commit comments