Skip to content

Commit c81dfa1

Browse files
committed
Support disabling reporting of uncaught exceptions in tests
The solution for #1205 may be undesirable for those who already have their own solution, like setting the default exception handlers. In this case, there's a usability issue without the corresponding benefit: instead of all tests being ran and the exceptions from them being reported, unrelated tests may fail, making looking for problems more difficult. This is probably a very rare issue, so we don't provide public API for that, instead introducing a need-to-know internal variable.
1 parent 25a3553 commit c81dfa1

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

kotlinx-coroutines-test/api/kotlinx-coroutines-test.api

+2
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,11 @@ public final class kotlinx/coroutines/test/TestScopeKt {
121121
public static final fun advanceTimeBy (Lkotlinx/coroutines/test/TestScope;J)V
122122
public static final fun advanceTimeBy-HG0u8IE (Lkotlinx/coroutines/test/TestScope;J)V
123123
public static final fun advanceUntilIdle (Lkotlinx/coroutines/test/TestScope;)V
124+
public static final fun getCatchNonTestRelatedExceptions ()Z
124125
public static final fun getCurrentTime (Lkotlinx/coroutines/test/TestScope;)J
125126
public static final fun getTestTimeSource (Lkotlinx/coroutines/test/TestScope;)Lkotlin/time/TimeSource$WithComparableMarks;
126127
public static final fun runCurrent (Lkotlinx/coroutines/test/TestScope;)V
128+
public static final fun setCatchNonTestRelatedExceptions (Z)V
127129
}
128130

129131
public abstract interface class kotlinx/coroutines/test/UncaughtExceptionCaptor {

kotlinx-coroutines-test/common/src/TestScope.kt

+12-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,9 @@ internal class TestScopeImpl(context: CoroutineContext) :
233233
* after the previous one, and learning about such exceptions as soon is possible is nice. */
234234
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
235235
run { ensurePlatformExceptionHandlerLoaded(ExceptionCollector) }
236-
ExceptionCollector.addOnExceptionCallback(lock, this::reportException)
236+
if (catchNonTestRelatedExceptions) {
237+
ExceptionCollector.addOnExceptionCallback(lock, this::reportException)
238+
}
237239
uncaughtExceptions
238240
}
239241
if (exceptions.isNotEmpty()) {
@@ -321,3 +323,12 @@ internal class UncaughtExceptionsBeforeTest : IllegalStateException(
321323
*/
322324
@ExperimentalCoroutinesApi
323325
internal class UncompletedCoroutinesError(message: String) : AssertionError(message)
326+
327+
/**
328+
* A flag that controls whether [TestScope] should attempt to catch arbitrary exceptions flying through the system.
329+
* If it is enabled, then any exception that is not caught by the user code will be reported as a test failure.
330+
* By default, it is enabled, but some tests may want to disable it to test the behavior of the system when they have
331+
* their own exception handling procedures.
332+
*/
333+
@PublishedApi
334+
internal var catchNonTestRelatedExceptions: Boolean = true

0 commit comments

Comments
 (0)