From 8b48e1fa86f374290d2c922e64168b0a48d8f00b Mon Sep 17 00:00:00 2001 From: Pavel Punegov Date: Thu, 28 Mar 2019 22:34:57 +0300 Subject: [PATCH] Update coroutines helpers with StateMachineChecker (#2829) --- .../org/jetbrains/kotlin/KonanTest.groovy | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/buildSrc/plugins/src/main/groovy/org/jetbrains/kotlin/KonanTest.groovy b/buildSrc/plugins/src/main/groovy/org/jetbrains/kotlin/KonanTest.groovy index a1956d736f9..c5b79c1ab22 100644 --- a/buildSrc/plugins/src/main/groovy/org/jetbrains/kotlin/KonanTest.groovy +++ b/buildSrc/plugins/src/main/groovy/org/jetbrains/kotlin/KonanTest.groovy @@ -211,6 +211,44 @@ abstract class KonanTest extends JavaExec { | abstract fun resumeWithException(exception: Throwable) | abstract fun resume(value: T) |} + |class StateMachineCheckerClass { + | private var counter = 0 + | var finished = false + | + | var proceed: () -> Unit = {} + | + | suspend fun suspendHere() = suspendCoroutine { c -> + | counter++ + | proceed = { c.resume(Unit) } + | } + | + | fun check(numberOfSuspensions: Int) { + | for (i in 1..numberOfSuspensions) { + | if (counter != i) error("Wrong state-machine generated: suspendHere called should be called exactly once in one state. Expected " + i + ", got " + counter) + | proceed() + | } + | if (counter != numberOfSuspensions) + | error("Wrong state-machine generated: suspendHere called should be called exactly once in one state. Expected " + numberOfSuspensions + ", got " + counter) + | if (finished) error("Wrong state-machine generated: it is finished early") + | proceed() + | if (!finished) error("Wrong state-machine generated: it is not finished yet") + | } + |} + |val StateMachineChecker = StateMachineCheckerClass() + |object CheckStateMachineContinuation: ContinuationAdapter() { + | override val context: CoroutineContext + | get() = EmptyCoroutineContext + | + | override fun resume(value: Unit) { + | StateMachineChecker.proceed = { + | StateMachineChecker.finished = true + | } + | } + | + | override fun resumeWithException(exception: Throwable) { + | throw exception + | } + |} """.stripMargin() }