1 package kotlinx.coroutines.test 2 3 import kotlinx.coroutines.* 4 import kotlinx.coroutines.debug.* 5 import org.junit.Test 6 import java.io.* 7 import kotlin.test.* 8 import kotlin.time.Duration.Companion.milliseconds 9 10 class DumpOnTimeoutTest { 11 /** 12 * Tests that the dump on timeout contains the correct stacktrace. 13 */ 14 @Test testDumpOnTimeoutnull15 fun testDumpOnTimeout() { 16 val oldErr = System.err 17 val baos = ByteArrayOutputStream() 18 try { 19 System.setErr(PrintStream(baos, true)) 20 DebugProbes.withDebugProbes { 21 try { 22 runTest(timeout = 100.milliseconds) { 23 uniquelyNamedFunction() 24 } 25 throw IllegalStateException("unreachable") 26 } catch (e: UncompletedCoroutinesError) { 27 // do nothing 28 } 29 } 30 baos.toString().let { 31 assertTrue(it.contains("uniquelyNamedFunction"), "Actual trace:\n$it") 32 } 33 } finally { 34 System.setErr(oldErr) 35 } 36 } 37 uniquelyNamedFunctionnull38 fun CoroutineScope.uniquelyNamedFunction() { 39 while (true) { 40 ensureActive() 41 Thread.sleep(10) 42 } 43 } 44 } 45