Fixup end label of local variable if it is before start label
In that case, put end label to next label after start label.
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
// WITH_RUNTIME
|
||||
// FULL_JDK
|
||||
|
||||
import kotlin.coroutines.*
|
||||
|
||||
fun ticker(
|
||||
delayMillis: Long,
|
||||
initialDelayMillis: Long = delayMillis
|
||||
): ReceiveChannel<Unit> = object : ReceiveChannel<Unit> {
|
||||
override fun cancel() {}
|
||||
override fun receive() {}
|
||||
}
|
||||
|
||||
suspend fun <T> withTimeoutOrNull(timeMillis: Long, block: suspend CoroutineScope.() -> T): T? = null
|
||||
|
||||
interface ReceiveChannel<out E> {
|
||||
fun receive(): E
|
||||
fun cancel()
|
||||
}
|
||||
|
||||
interface CoroutineScope
|
||||
|
||||
suspend fun delay(i: Int) {}
|
||||
|
||||
suspend fun test() {
|
||||
val tickerChannel = ticker(delayMillis = 100, initialDelayMillis = 0) // create ticker channel
|
||||
var nextElement = withTimeoutOrNull(1) { tickerChannel.receive() }
|
||||
println("Initial element is available immediately: $nextElement") // no initial delay
|
||||
|
||||
nextElement = withTimeoutOrNull(50) { tickerChannel.receive() } // all subsequent elements have 100ms delay
|
||||
println("Next element is not ready in 50 ms: $nextElement")
|
||||
|
||||
nextElement = withTimeoutOrNull(60) { tickerChannel.receive() }
|
||||
println("Next element is ready in 100 ms: $nextElement")
|
||||
|
||||
// Emulate large consumption delays
|
||||
println("Consumer pauses for 150ms")
|
||||
delay(150)
|
||||
// Next element is available immediately
|
||||
nextElement = withTimeoutOrNull(1) { tickerChannel.receive() }
|
||||
println("Next element is available immediately after large consumer delay: $nextElement")
|
||||
// Note that the pause between `receive` calls is taken into account and next element arrives faster
|
||||
nextElement = withTimeoutOrNull(60) { tickerChannel.receive() }
|
||||
println("Next element is ready in 50ms after consumer pause in 150ms: $nextElement")
|
||||
|
||||
tickerChannel.cancel() // indicate that no more elements are needed
|
||||
}
|
||||
|
||||
fun builder(c: suspend () -> Unit) {
|
||||
c.startCoroutine(Continuation(EmptyCoroutineContext) {
|
||||
it.getOrThrow()
|
||||
})
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
builder {
|
||||
test()
|
||||
}
|
||||
return "OK"
|
||||
}
|
||||
Reference in New Issue
Block a user