183: CoroutineNonBlockingContextChecker for warning on blocking calls in coroutines (KT-15525)

This commit is contained in:
NikitaKatkov
2018-09-12 19:05:16 +03:00
committed by Nicolay Mitropolsky
parent 341f7c348a
commit d2536f207c
6 changed files with 182 additions and 0 deletions
@@ -0,0 +1,32 @@
@file:Suppress("UNUSED_PARAMETER")
import kotlin.coroutines.*
import org.jetbrains.annotations.BlockingContext
import kotlin.coroutines.experimental.buildSequence
import java.lang.Thread.sleep
suspend fun testFunction() {
@BlockingContext
val ctx = getContext()
// no warnings with @BlockingContext annotation on ctx object
withContext(ctx) {Thread.sleep (2)}
// no warnings with @BlockingContext annotation on getContext() method
withContext(getContext()) {Thread.sleep(3)}
withContext(getNonBlockingContext()) {
Thread.<warning descr="Inappropriate blocking method call">sleep</warning>(3);
}
}
@BlockingContext
fun getContext(): CoroutineContext = TODO()
fun getNonBlockingContext(): CoroutineContext = TODO()
suspend fun <T> withContext(
context: CoroutineContext,
f: suspend () -> T
) {
TODO()
}
@@ -0,0 +1,10 @@
@file:Suppress("UNUSED_PARAMETER")
import kotlin.coroutines.*
import java.lang.Thread.sleep
class InsideCoroutine {
suspend fun example1() {
Thread.<warning descr="Inappropriate blocking method call">sleep</warning>(1);
}
}
@@ -0,0 +1,20 @@
@file:Suppress("UNUSED_PARAMETER")
import java.lang.Thread.sleep
import kotlin.coroutines.RestrictsSuspension
suspend fun testFunction() {
// @RestrictsSuspension annotation allows blocking calls
withRestrictedReceiver({Thread.sleep(0)}, {Thread.sleep(1)})
withSimpleReceiver({Thread.<warning descr="Inappropriate blocking method call">sleep</warning>(2)})
}
fun withRestrictedReceiver(firstParam: suspend Test1.() -> Unit, secondParam: () -> Unit) {}
fun withSimpleReceiver(firstParam: suspend Test2.() -> Unit) {}
@RestrictsSuspension
class Test1
class Test2