Update coroutines helpers with StateMachineChecker (#2829)
This commit is contained in:
committed by
Nikolay Igotti
parent
19310d84c6
commit
8b48e1fa86
@@ -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<Unit> { 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<Unit>() {
|
||||
| override val context: CoroutineContext
|
||||
| get() = EmptyCoroutineContext
|
||||
|
|
||||
| override fun resume(value: Unit) {
|
||||
| StateMachineChecker.proceed = {
|
||||
| StateMachineChecker.finished = true
|
||||
| }
|
||||
| }
|
||||
|
|
||||
| override fun resumeWithException(exception: Throwable) {
|
||||
| throw exception
|
||||
| }
|
||||
|}
|
||||
""".stripMargin()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user