-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't use Dispatchers.DEFAULT for the test runner
- Loading branch information
1 parent
38f3bab
commit 4fe30ec
Showing
4 changed files
with
101 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2016-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines.test | ||
|
||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.debug.* | ||
import org.junit.Test | ||
import java.io.* | ||
import kotlin.test.* | ||
import kotlin.time.Duration.Companion.milliseconds | ||
|
||
class DumpOnTimeoutTest { | ||
/** | ||
* Tests that the dump on timeout contains the correct stacktrace. | ||
*/ | ||
@Test | ||
fun testDumpOnTimeout() { | ||
val oldErr = System.err | ||
val baos = ByteArrayOutputStream() | ||
try { | ||
System.setErr(PrintStream(baos, true)) | ||
DebugProbes.withDebugProbes { | ||
try { | ||
runTest(timeout = 100.milliseconds) { | ||
uniquelyNamedFunction() | ||
} | ||
throw IllegalStateException("unreachable") | ||
} catch (e: UncompletedCoroutinesError) { | ||
// do nothing | ||
} | ||
} | ||
baos.toString().let { | ||
assertTrue(it.contains("uniquelyNamedFunction"), "Actual trace:\n$it") | ||
} | ||
} finally { | ||
System.setErr(oldErr) | ||
} | ||
} | ||
|
||
fun CoroutineScope.uniquelyNamedFunction() { | ||
while (true) { | ||
ensureActive() | ||
Thread.sleep(10) | ||
} | ||
} | ||
} |