Introduce startCoroutineUninterceptedOrReturn coroutine intrinsic

#KT-15716 Fixed
This commit is contained in:
Denis Zharkov
2017-01-30 19:24:53 +03:00
parent 29b0b30031
commit 258ee0db75
11 changed files with 452 additions and 0 deletions
@@ -0,0 +1,44 @@
// WITH_RUNTIME
// WITH_COROUTINES
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
import kotlin.test.assertEquals
suspend fun suspendHere(): String = suspendCoroutineOrReturn { x ->
x.resume("OK")
COROUTINE_SUSPENDED
}
suspend fun suspendWithException(): String = suspendCoroutineOrReturn { x ->
x.resumeWithException(RuntimeException("OK"))
COROUTINE_SUSPENDED
}
fun builder(c: suspend () -> String): String {
var fromSuspension: String? = null
c.startCoroutine(object: Continuation<String> {
override val context: CoroutineContext
get() = EmptyCoroutineContext
override fun resumeWithException(exception: Throwable) {
fromSuspension = "Exception: " + exception.message!!
}
override fun resume(value: String) {
fromSuspension = value
}
})
return fromSuspension as String
}
fun box(): String {
if (builder { "OK" } != "OK") return "fail 4"
if (builder { suspendHere() } != "OK") return "fail 5"
if (builder { throw RuntimeException("OK") } != "Exception: OK") return "fail 6"
if (builder { suspendWithException() } != "Exception: OK") return "fail 7"
return "OK"
}
@@ -0,0 +1,55 @@
// WITH_RUNTIME
// WITH_COROUTINES
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
import kotlin.test.assertEquals
suspend fun suspendHere(): String = suspendCoroutineOrReturn { x ->
x.resume("OK")
COROUTINE_SUSPENDED
}
suspend fun suspendWithException(): String = suspendCoroutineOrReturn { x ->
x.resumeWithException(RuntimeException("OK"))
COROUTINE_SUSPENDED
}
fun builder(shouldSuspend: Boolean, c: suspend () -> String): String {
var fromSuspension: String? = null
val result = try {
c.startCoroutineUninterceptedOrReturn(object: Continuation<String> {
override val context: CoroutineContext
get() = EmptyCoroutineContext
override fun resumeWithException(exception: Throwable) {
fromSuspension = "Exception: " + exception.message!!
}
override fun resume(value: String) {
fromSuspension = value
}
})
} catch (e: Exception) {
"Exception: ${e.message}"
}
if (shouldSuspend) {
if (result !== COROUTINE_SUSPENDED) throw RuntimeException("fail 1")
if (fromSuspension == null) throw RuntimeException("fail 2")
return fromSuspension!!
}
if (result === COROUTINE_SUSPENDED) throw RuntimeException("fail 3")
return result as String
}
fun box(): String {
if (builder(false) { "OK" } != "OK") return "fail 4"
if (builder(true) { suspendHere() } != "OK") return "fail 5"
if (builder(false) { throw RuntimeException("OK") } != "Exception: OK") return "fail 6"
if (builder(true) { suspendWithException() } != "Exception: OK") return "fail 7"
return "OK"
}
@@ -0,0 +1,80 @@
// WITH_RUNTIME
// WITH_COROUTINES
// IGNORE_BACKEND: JS
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
import kotlin.test.assertEquals
suspend fun suspendHere(): String = suspendCoroutineOrReturn { x ->
x.resume("OK")
COROUTINE_SUSPENDED
}
suspend fun suspendWithException(): String = suspendCoroutineOrReturn { x ->
x.resumeWithException(RuntimeException("OK"))
COROUTINE_SUSPENDED
}
fun builder(shouldSuspend: Boolean, expectedCount: Int, c: suspend () -> String): String {
var fromSuspension: String? = null
var counter = 0
val result = try {
c.startCoroutineUninterceptedOrReturn(object: Continuation<String> {
override val context: CoroutineContext
get() = ContinuationDispatcher { counter++ }
override fun resumeWithException(exception: Throwable) {
fromSuspension = "Exception: " + exception.message!!
}
override fun resume(value: String) {
fromSuspension = value
}
})
} catch (e: Exception) {
"Exception: ${e.message}"
}
if (counter != expectedCount) throw RuntimeException("fail 0")
if (shouldSuspend) {
if (result !== COROUTINE_SUSPENDED) throw RuntimeException("fail 1")
if (fromSuspension == null) throw RuntimeException("fail 2")
return fromSuspension!!
}
if (result === COROUTINE_SUSPENDED) throw RuntimeException("fail 3")
return result as String
}
class ContinuationDispatcher(val dispatcher: () -> Unit) : AbstractCoroutineContextElement(ContinuationInterceptor), ContinuationInterceptor {
override fun <T> interceptContinuation(continuation: Continuation<T>): Continuation<T> = DispatchedContinuation(dispatcher, continuation)
}
private class DispatchedContinuation<T>(
val dispatcher: () -> Unit,
val continuation: Continuation<T>
): Continuation<T> {
override val context: CoroutineContext = continuation.context
override fun resume(value: T) {
dispatcher()
continuation.resume(value)
}
override fun resumeWithException(exception: Throwable) {
dispatcher()
continuation.resumeWithException(exception)
}
}
fun box(): String {
if (builder(false, 0) { "OK" } != "OK") return "fail 4"
if (builder(true, 1) { suspendHere() } != "OK") return "fail 5"
if (builder(false, 0) { throw RuntimeException("OK") } != "Exception: OK") return "fail 6"
if (builder(true, 1) { suspendWithException() } != "Exception: OK") return "fail 7"
return "OK"
}