Add facility to check number of suspensions in coroutine tests

This commit is contained in:
Ilmir Usmanov
2019-03-12 15:56:27 +03:00
parent ca35c9e2ff
commit df52c5217d
2 changed files with 50 additions and 41 deletions
@@ -21,53 +21,20 @@ import COROUTINES_PACKAGE.*
import COROUTINES_PACKAGE.intrinsics.*
import helpers.*
var result = "FAIL"
var i = 0
var finished = false
var proceed: () -> Unit = {}
suspend fun suspendHere() = suspendCoroutine<Unit> { c ->
i++
proceed = { c.resume(Unit) }
}
fun builder(c: suspend () -> Unit) {
val continuation = object: ContinuationAdapter<Unit>() {
override val context: CoroutineContext
get() = EmptyCoroutineContext
override fun resume(value: Unit) {
proceed = {
result = "OK"
finished = true
}
}
override fun resumeWithException(exception: Throwable) {
throw exception
}
}
c.startCoroutine(continuation)
c.startCoroutine(CheckStateMachineContinuation)
}
fun box(): String {
builder {
crossinlineMe2 {
suspendHere()
suspendHere()
suspendHere()
suspendHere()
suspendHere()
StateMachineChecker.suspendHere()
StateMachineChecker.suspendHere()
StateMachineChecker.suspendHere()
StateMachineChecker.suspendHere()
StateMachineChecker.suspendHere()
}
}
for (counter in 0 until 10) {
if (i != counter + 1) return "Expected ${counter + 1}, got $i"
proceed()
}
if (i != 10) return "FAIL $i"
if (finished) return "resume on root continuation is called"
proceed()
if (!finished) return "resume on root continuation is not called"
return result
StateMachineChecker.check(numberOfSuspensions = 10)
return "OK"
}