• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3  */
4 
5 package kotlinx.coroutines.test
6 
7 import kotlinx.coroutines.*
8 import kotlinx.coroutines.debug.*
9 import org.junit.Test
10 import java.io.*
11 import kotlin.test.*
12 import kotlin.time.Duration.Companion.milliseconds
13 
14 class DumpOnTimeoutTest {
15     /**
16      * Tests that the dump on timeout contains the correct stacktrace.
17      */
18     @Test
testDumpOnTimeoutnull19     fun testDumpOnTimeout() {
20         val oldErr = System.err
21         val baos = ByteArrayOutputStream()
22         try {
23             System.setErr(PrintStream(baos, true))
24             DebugProbes.withDebugProbes {
25                 try {
26                     runTest(timeout = 100.milliseconds) {
27                         uniquelyNamedFunction()
28                     }
29                     throw IllegalStateException("unreachable")
30                 } catch (e: UncompletedCoroutinesError) {
31                     // do nothing
32                 }
33             }
34             baos.toString().let {
35                 assertTrue(it.contains("uniquelyNamedFunction"), "Actual trace:\n$it")
36             }
37         } finally {
38             System.setErr(oldErr)
39         }
40     }
41 
uniquelyNamedFunctionnull42     fun CoroutineScope.uniquelyNamedFunction() {
43         while (true) {
44             ensureActive()
45             Thread.sleep(10)
46         }
47     }
48 }
49