Skip to content

Commit 38f3bab

Browse files
committed
Don't attempt a pretty stacktrace on runTest timeout
1 parent 9fd86bb commit 38f3bab

File tree

5 files changed

+5
-37
lines changed

5 files changed

+5
-37
lines changed

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

+5-20
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import kotlin.jvm.*
1414
import kotlin.time.*
1515
import kotlin.time.Duration.Companion.milliseconds
1616
import kotlin.time.Duration.Companion.seconds
17+
import kotlinx.coroutines.internal.*
1718

1819
/**
1920
* A test result.
@@ -309,19 +310,13 @@ public fun TestScope.runTest(
309310
it.start(CoroutineStart.UNDISPATCHED, it) {
310311
testBody()
311312
}
312-
/**
313-
* The thread in which the task was last seen executing.
314-
*/
315-
val lastKnownPosition = MutableStateFlow<Any?>(null)
316-
317313
/**
318314
* We run the tasks in the test coroutine using [Dispatchers.Default]. On JS, this does nothing particularly,
319315
* but on the JVM and Native, this means that the timeout can be processed even while the test runner is busy
320316
* doing some synchronous work.
321317
*/
322318
val workRunner = launch(Dispatchers.Default + CoroutineName("kotlinx.coroutines.test runner")) {
323319
while (true) {
324-
lastKnownPosition.value = getLastKnownPosition()
325320
val executedSomething = testScheduler.tryRunNextTaskUnless { !isActive }
326321
if (executedSomething) {
327322
/** yield to check for cancellation. On JS, we can't use [ensureActive] here, as the cancellation
@@ -355,19 +350,13 @@ public fun TestScope.runTest(
355350
}
356351
timeoutError = UncompletedCoroutinesError(message)
357352
dumpCoroutines()
358-
/**
359-
* There's a race that may lead to the misleading results here, but it's better than nothing.
360-
* The race: `lastKnownPosition` is read, then the task executed in `workRunner` completes,
361-
* then `updateStacktrace` does its thing, but the thread is already busy doing something else.
362-
*/
363-
updateStacktrace(timeoutError, lastKnownPosition.value)
364-
val cancellationException = CancellationException("The test timed out", timeoutError)
353+
val cancellationException = CancellationException("The test timed out")
365354
(it as Job).cancel(cancellationException)
366355
// we can't abandon the work we're doing, so if it hanged, we'll still hang, despite the timeout.
367356
it.join()
368-
it.getCompletionExceptionOrNull()?.let { exception ->
369-
if (exception !== cancellationException)
370-
timeoutError.addSuppressed(exception)
357+
val completion = it.getCompletionExceptionOrNull()
358+
if (completion != null && completion !== cancellationException) {
359+
timeoutError.addSuppressed(completion)
371360
}
372361
workRunner.cancelAndJoin()
373362
} finally {
@@ -578,8 +567,4 @@ internal fun throwAll(head: Throwable?, other: List<Throwable>) {
578567
}
579568
}
580569

581-
internal expect fun getLastKnownPosition(): Any?
582-
583570
internal expect fun dumpCoroutines()
584-
585-
internal expect fun updateStacktrace(exception: Throwable, lastKnownPosition: Any?): Throwable

kotlinx-coroutines-test/common/test/RunTestTest.kt

-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ class RunTestTest {
9999
fn()
100100
fail("shouldn't be reached")
101101
} catch (e: Throwable) {
102-
e.printStackTrace()
103102
assertIs<UncompletedCoroutinesError>(e)
104103
}
105104
}) {

kotlinx-coroutines-test/js/src/TestBuilders.kt

-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,4 @@ internal actual fun createTestResult(testProcedure: suspend CoroutineScope.() ->
1414
testProcedure()
1515
}
1616

17-
internal actual fun getLastKnownPosition(): Any? = null
18-
1917
internal actual fun dumpCoroutines() { }
20-
21-
internal actual fun updateStacktrace(exception: Throwable, lastKnownPosition: Any?): Throwable = exception

kotlinx-coroutines-test/jvm/src/TestBuildersJvm.kt

-8
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ internal actual fun createTestResult(testProcedure: suspend CoroutineScope.() ->
1515
}
1616
}
1717

18-
internal actual fun getLastKnownPosition(): Any? = Thread.currentThread()
19-
2018
internal actual fun dumpCoroutines() {
2119
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
2220
if (DebugProbesImpl.isInstalled) {
@@ -29,9 +27,3 @@ internal actual fun dumpCoroutines() {
2927
}
3028
}
3129
}
32-
33-
internal actual fun updateStacktrace(exception: Throwable, lastKnownPosition: Any?): Throwable {
34-
val thread = lastKnownPosition as? Thread
35-
thread?.stackTrace?.let { exception.stackTrace = it }
36-
return exception
37-
}

kotlinx-coroutines-test/native/src/TestBuilders.kt

-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,4 @@ internal actual fun createTestResult(testProcedure: suspend CoroutineScope.() ->
1515
}
1616
}
1717

18-
internal actual fun getLastKnownPosition(): Any? = null
19-
2018
internal actual fun dumpCoroutines() { }
21-
22-
internal actual fun updateStacktrace(exception: Throwable, lastKnownPosition: Any?): Throwable = exception

0 commit comments

Comments
 (0)