Skip to content

Commit c7cf2e8

Browse files
committed
Update debug module documentation
1 parent 7f55627 commit c7cf2e8

File tree

5 files changed

+86
-4
lines changed

5 files changed

+86
-4
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ GlobalScope.launch {
3939
* `Dispatchers.setMain` to override `Dispatchers.Main` in tests.
4040
* [debug](kotlinx-coroutines-debug/README.md) — debug utilities for coroutines.
4141
* `DebugProbes` API to probe, keep track of, print and dump active coroutines.
42+
* `CoroutinesTimeout` test rule to automatically dump coroutines on test timeout.
4243
* [reactive](reactive/README.md) — modules that provide builders and iteration support for various reactive streams libraries:
4344
* Reactive Streams, RxJava 2.x, and Project Reactor.
4445
* [ui](ui/README.md) — modules that provide coroutine dispatchers for various single-threaded UI libraries:

kotlinx-coroutines-core/jvm/test/guide/example-compose-05.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ import kotlinx.coroutines.*
99
import kotlin.system.*
1010

1111
fun main() = runBlocking<Unit> {
12-
//sampleStart
12+
//sampleStart
1313
val time = measureTimeMillis {
1414
println("The answer is ${concurrentSum()}")
1515
}
1616
println("Completed in $time ms")
17-
//sampleEnd
17+
//sampleEnd
1818
}
1919

2020
suspend fun concurrentSum(): Int = coroutineScope {
2121
val one = async { doSomethingUsefulOne() }
2222
val two = async { doSomethingUsefulTwo() }
23-
one.await() + two.await()
23+
one.await() + two.await()
2424
}
2525

2626
suspend fun doSomethingUsefulOne(): Int {

kotlinx-coroutines-core/jvm/test/guide/example-compose-06.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ suspend fun failedConcurrentSum(): Int = coroutineScope {
2828
println("Second child throws an exception")
2929
throw ArithmeticException()
3030
}
31-
one.await() + two.await()
31+
one.await() + two.await()
3232
}

kotlinx-coroutines-debug/README.md

+39
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,43 @@ dependencies {
2222
}
2323
```
2424

25+
### Using in unit tests
26+
27+
For JUnit4 debug module provides special test rule, [CoroutinesTimeout], for installing debug probes
28+
and dump coroutines on timeout to simplify tests debugging.
29+
30+
Its usage is better to demonstrate by the example (runnable code is [here](test/TestRuleExample.kt)):
31+
32+
```kotlin
33+
class TestRuleExample {
34+
@Rule
35+
@JvmField
36+
public val timeout = CoroutinesTimeout.seconds(1)
37+
38+
private suspend fun someFunctionDeepInTheStack() {
39+
withContext(Dispatchers.IO) {
40+
delay(Long.MAX_VALUE)
41+
println("This line is never executed")
42+
}
43+
44+
println("This line is never executed as well")
45+
}
46+
47+
@Test
48+
fun hangingTest() = runBlocking {
49+
val job = launch {
50+
someFunctionDeepInTheStack()
51+
}
52+
53+
println("Doing some work...")
54+
job.join()
55+
}
56+
}
57+
```
58+
59+
After 1 second, test will fail with `TestTimeoutException` and all coroutines (`runBlocking` and `launch`) and their
60+
stacktraces will be dumped to the console.
61+
2562
### Using as JVM agent
2663

2764
It is possible to use this module as a standalone JVM agent to enable debug probes on the application startup.
@@ -112,4 +149,6 @@ Do not use this module in production environment and do not rely on the format o
112149
[DebugProbes.dumpCoroutinesState]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-debug/kotlinx.coroutines.debug/-debug-probes/dump-coroutines-state.html
113150
[DebugProbes.printJob]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-debug/kotlinx.coroutines.debug/-debug-probes/print-job.html
114151
[DebugProbes.printScope]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-debug/kotlinx.coroutines.debug/-debug-probes/print-scope.html
152+
<!--- INDEX kotlinx.coroutines.debug.junit4 -->
153+
[CoroutinesTimeout]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-debug/kotlinx.coroutines.debug.junit4/-coroutines-timeout/index.html
115154
<!--- END -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
import kotlinx.coroutines.*
6+
import kotlinx.coroutines.debug.junit4.*
7+
import org.junit.*
8+
9+
@Ignore // do not run it on CI
10+
class TestRuleExample {
11+
12+
@JvmField
13+
@Rule
14+
public val timeout = CoroutinesTimeout.seconds(1)
15+
16+
private suspend fun someFunctionDeepInTheStack() {
17+
withContext(Dispatchers.IO) {
18+
delay(Long.MAX_VALUE)
19+
println("This line is never executed")
20+
}
21+
22+
println("This line is never executed as well")
23+
}
24+
25+
@Test
26+
fun hangingTest() = runBlocking {
27+
val job = launch {
28+
someFunctionDeepInTheStack()
29+
}
30+
31+
println("Doing some work...")
32+
job.join()
33+
}
34+
35+
@Test
36+
fun successfulTest() = runBlocking {
37+
launch {
38+
delay(10)
39+
}.join()
40+
}
41+
42+
}

0 commit comments

Comments
 (0)