@@ -22,6 +22,43 @@ dependencies {
22
22
}
23
23
```
24
24
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
+
25
62
### Using as JVM agent
26
63
27
64
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
112
149
[ DebugProbes.dumpCoroutinesState ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-debug/kotlinx.coroutines.debug/-debug-probes/dump-coroutines-state.html
113
150
[ DebugProbes.printJob ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-debug/kotlinx.coroutines.debug/-debug-probes/print-job.html
114
151
[ 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
115
154
<!-- - END -->
0 commit comments