Fix dispatchResume.kt tests after suspendCoroutineOrReturn removal

To leave them COMMON_COROUTINES_TEST use suspendCoroutine instead
But, since it requires for continuation to be called externally
the tests are rewritten
This commit is contained in:
Denis Zharkov
2018-07-04 18:35:32 +03:00
parent 8c65e55c02
commit e753e0978f
2 changed files with 42 additions and 18 deletions
+20 -8
View File
@@ -3,6 +3,7 @@
// WITH_RUNTIME
// WITH_COROUTINES
// COMMON_COROUTINES_TEST
// FULL_JDK
import helpers.*
import COROUTINES_PACKAGE.*
import COROUTINES_PACKAGE.intrinsics.*
@@ -11,16 +12,20 @@ class Controller {
var log = ""
var resumeIndex = 0
suspend fun <T> suspendWithValue(value: T): T = suspendCoroutineOrReturn { continuation ->
var callback: (() -> Unit)? = null
suspend fun <T> suspendWithValue(value: T): T = suspendCoroutine { continuation ->
log += "suspend($value);"
continuation.resume(value)
COROUTINE_SUSPENDED
callback = {
continuation.resume(value)
}
}
suspend fun suspendWithException(value: String): Unit = suspendCoroutineOrReturn { continuation ->
suspend fun suspendWithException(value: String): Unit = suspendCoroutine { continuation ->
log += "error($value);"
continuation.resumeWithException(RuntimeException(value))
COROUTINE_SUSPENDED
callback = {
continuation.resumeWithException(RuntimeException(value))
}
}
}
@@ -71,6 +76,13 @@ fun test(c: suspend Controller.() -> Unit): String {
return true
}
}))
while (controller.callback != null) {
val c = controller.callback!!
controller.callback = null
c()
}
return controller.log
}
@@ -80,7 +92,7 @@ fun box(): String {
val k = suspendWithValue("K")
log += "$o$k;"
}
if (result != "before 0;suspend(O);before 1;suspend(K);before 2;OK;after 2;after 1;after 0;") return "fail1: $result"
if (result != "before 0;suspend(O);after 0;before 1;suspend(K);after 1;before 2;OK;after 2;") return "fail1: $result"
result = test {
try {
@@ -91,7 +103,7 @@ fun box(): String {
log += "${e.message};"
}
}
if (result != "before 0;error(OK);before 1;OK;after 1;after 0;") return "fail2: $result"
if (result != "before 0;error(OK);after 0;before 1;OK;after 1;") return "fail2: $result"
return "OK"
}
@@ -3,6 +3,7 @@
// WITH_RUNTIME
// WITH_COROUTINES
// COMMON_COROUTINES_TEST
// FULL_JDK
import helpers.*
import COROUTINES_PACKAGE.*
import COROUTINES_PACKAGE.intrinsics.*
@@ -11,16 +12,20 @@ class Controller {
var log = ""
var resumeIndex = 0
suspend fun <T> suspendWithValue(value: T): T = suspendCoroutineOrReturn { continuation ->
var callback: (() -> Unit)? = null
suspend fun <T> suspendWithValue(value: T): T = suspendCoroutine { continuation ->
log += "suspend($value);"
continuation.resume(value)
COROUTINE_SUSPENDED
callback = {
continuation.resume(value)
}
}
suspend fun suspendWithException(value: String): Unit = suspendCoroutineOrReturn { continuation ->
suspend fun suspendWithException(value: String): Unit = suspendCoroutine { continuation ->
log += "error($value);"
continuation.resumeWithException(RuntimeException(value))
COROUTINE_SUSPENDED
callback = {
continuation.resumeWithException(RuntimeException(value))
}
}
}
@@ -31,8 +36,8 @@ abstract class ContinuationDispatcher : AbstractCoroutineContextElement(Continua
}
private class DispatchedContinuation<T>(
val dispatcher: ContinuationDispatcher,
val continuation: Continuation<T>
val dispatcher: ContinuationDispatcher,
val continuation: Continuation<T>
): ContinuationAdapter<T>() {
override val context: CoroutineContext = continuation.context
@@ -71,6 +76,13 @@ fun test(c: suspend Controller.() -> Unit): String {
return true
}
}))
while (controller.callback != null) {
val c = controller.callback!!
controller.callback = null
c()
}
return controller.log
}
@@ -98,12 +110,12 @@ fun box(): String {
var result = test {
test1()
}
if (result != "before 0;suspend();before 1;suspend(O);before 2;suspend(K);before 3;OK;after 3;after 2;after 1;after 0;") return "fail1: $result"
if (result != "before 0;suspend();after 0;before 1;suspend(O);after 1;before 2;suspend(K);after 2;before 3;OK;after 3;") return "fail1: $result"
result = test {
test2()
}
if (result != "before 0;suspend();before 1;suspend(O);before 2;error(OK);before 3;OK;after 3;after 2;after 1;after 0;") return "fail2: $result"
if (result != "before 0;suspend();after 0;before 1;suspend(O);after 1;before 2;error(OK);after 2;before 3;OK;after 3;") return "fail2: $result"
return "OK"
}