183: CoroutineNonBlockingContextChecker for warning on blocking calls in coroutines (KT-15525)
This commit is contained in:
committed by
Nicolay Mitropolsky
parent
341f7c348a
commit
d2536f207c
@@ -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);
|
||||
}
|
||||
}
|
||||
+20
@@ -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
|
||||
Reference in New Issue
Block a user