Skip to content

Commit

Permalink
Update debug module documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
qwwdfsad committed Feb 18, 2019
1 parent 7f55627 commit 596a421
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ GlobalScope.launch {
* `Dispatchers.setMain` to override `Dispatchers.Main` in tests.
* [debug](kotlinx-coroutines-debug/README.md) — debug utilities for coroutines.
* `DebugProbes` API to probe, keep track of, print and dump active coroutines.
* `CoroutinesTimeout` test rule to automatically dump coroutines on test timeout.
* [reactive](reactive/README.md) — modules that provide builders and iteration support for various reactive streams libraries:
* Reactive Streams, RxJava 2.x, and Project Reactor.
* [ui](ui/README.md) — modules that provide coroutine dispatchers for various single-threaded UI libraries:
Expand Down
6 changes: 3 additions & 3 deletions kotlinx-coroutines-core/jvm/test/guide/example-compose-05.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import kotlinx.coroutines.*
import kotlin.system.*

fun main() = runBlocking<Unit> {
//sampleStart
//sampleStart
val time = measureTimeMillis {
println("The answer is ${concurrentSum()}")
}
println("Completed in $time ms")
//sampleEnd
//sampleEnd
}

suspend fun concurrentSum(): Int = coroutineScope {
val one = async { doSomethingUsefulOne() }
val two = async { doSomethingUsefulTwo() }
one.await() + two.await()
one.await() + two.await()
}

suspend fun doSomethingUsefulOne(): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ suspend fun failedConcurrentSum(): Int = coroutineScope {
println("Second child throws an exception")
throw ArithmeticException()
}
one.await() + two.await()
one.await() + two.await()
}
39 changes: 39 additions & 0 deletions kotlinx-coroutines-debug/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,43 @@ dependencies {
}
```

### Using in unit tests

For JUnit4 debug module provides special test rule, [CoroutinesTimeout], for installing debug probes
and dump coroutines on timeout to simplify tests debugging.

Its usage is better to demonstrate by the example (runnable code is [here](test/TestRuleExample.kt)):

```kotlin
class TestRuleExample {
@Rule
@JvmField
public val timeout = CoroutinesTimeout.seconds(1)

private suspend fun someFunctionDeepInTheStack() {
withContext(Dispatchers.IO) {
delay(Long.MAX_VALUE)
println("This line is never executed")
}

println("This line is never executed as well")
}

@Test
fun hangingTest() = runBlocking {
val job = launch {
someFunctionDeepInTheStack()
}

println("Doing some work...")
job.join()
}
}
```

After 1 second, test will fail with `TestTimeoutException` and all coroutines (`runBlocking` and `launch`) and their
stacktraces will be dumped to the console.

### Using as JVM agent

It is possible to use this module as a standalone JVM agent to enable debug probes on the application startup.
Expand Down Expand Up @@ -112,4 +149,6 @@ Do not use this module in production environment and do not rely on the format o
[DebugProbes.dumpCoroutinesState]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-debug/kotlinx.coroutines.debug/-debug-probes/dump-coroutines-state.html
[DebugProbes.printJob]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-debug/kotlinx.coroutines.debug/-debug-probes/print-job.html
[DebugProbes.printScope]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-debug/kotlinx.coroutines.debug/-debug-probes/print-scope.html
<!--- INDEX kotlinx.coroutines.debug.junit4 -->
[CoroutinesTimeout]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-debug/kotlinx.coroutines.debug.junit4/-coroutines-timeout/index.html
<!--- END -->
2 changes: 1 addition & 1 deletion kotlinx-coroutines-debug/src/junit4/CoroutinesTimeout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import java.util.concurrent.*
* ```
* class HangingTest {
*
* @Rule
* @Rule
* @JvmField
* val timeout = CoroutinesTimeout.seconds(5)
*
Expand Down
42 changes: 42 additions & 0 deletions kotlinx-coroutines-debug/test/TestRuleExample.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

import kotlinx.coroutines.*
import kotlinx.coroutines.debug.junit4.*
import org.junit.*

@Ignore // do not run it on CI
class TestRuleExample {

@JvmField
@Rule
public val timeout = CoroutinesTimeout.seconds(1)

private suspend fun someFunctionDeepInTheStack() {
withContext(Dispatchers.IO) {
delay(Long.MAX_VALUE)
println("This line is never executed")
}

println("This line is never executed as well")
}

@Test
fun hangingTest() = runBlocking {
val job = launch {
someFunctionDeepInTheStack()
}

println("Doing some work...")
job.join()
}

@Test
fun successfulTest() = runBlocking {
launch {
delay(10)
}.join()
}

}

0 comments on commit 596a421

Please sign in to comment.