Exercise: Continuation storage
Implement the continuationSteal
function, which should print "Before" and "After" in the console; in between them, it should suspend the coroutine this function is executed on and store the continuation in the continuation
property. After resuming, it should print the string this continuation was resumed with.
var continuation: Continuation<String>? = null
suspend fun continuationSteal(console: Console) {
console.println("Before")
// TODO
console.println("After")
}
interface Console {
fun println(text: Any?)
}
fun main(): Unit = runBlocking<Unit> {
launch {
continuationSteal(object : Console {
override fun println(text: Any?) {
kotlin.io.println(text)
}
})
}
delay(1000)
continuation?.resume("This is some text")
}
// Before
// (1 sec)
// This is some text
// After
This problem can either be solved in the below playground or you can clone kotlin-exercises project and solve it locally. In the project, you can find code template for this exercise in coroutines/suspension/ContinuationStorage.kt. You can find there starting code, example usage and unit tests.
Hint: Use suspendCancellableCoroutine
instead of suspendCoroutine
.
Once you are done with the exercise, you can check your solution here.
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.junit.Test
import kotlin.coroutines.Continuation
import kotlin.coroutines.resume
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
var continuation: Continuation<String>? = null
suspend fun continuationSteal(console: Console) {
console.println("Before")
// TODO
console.println("After")
}
interface Console {
fun println(text: Any?)
}
fun main(): Unit = runBlocking<Unit> {
launch {
continuationSteal(object : Console {
override fun println(text: Any?) {
kotlin.io.println(text)
}
})
}
delay(1000)
continuation?.resume("This is some text")
}
// Before
// (1 sec)
// This is some text
// After
@Suppress("FunctionName")
class ContinuationStealTests {
private val fakeText = "This is some text"
class FakeConsole : Console {
val printed = mutableListOf<Any?>()
override fun println(text: Any?) {
printed += text
}
}
@Test
fun `At the beginning function says Before`() = runTest(UnconfinedTestDispatcher()) {
val fakeConsole = FakeConsole()
val job = launch {
continuationSteal(fakeConsole)
}
delay(100)
assertEquals("Before", fakeConsole.printed.first())
job.cancel()
}
@Test
fun `At the end function says After`() = runTest(UnconfinedTestDispatcher()) {
val fakeConsole = FakeConsole()
val job = launch {
continuationSteal(fakeConsole)
}
continuation?.resume(fakeText)
assertEquals("After", fakeConsole.printed.last())
}
@Test
fun `In the middle, we suspend function`() = runTest(UnconfinedTestDispatcher()) {
val fakeConsole = FakeConsole()
val job = launch {
continuationSteal(fakeConsole)
}
assertEquals(mutableListOf<Any?>("Before"), fakeConsole.printed)
job.cancel()
}
@Test
fun `Function should return continuation`() = runTest(UnconfinedTestDispatcher()) {
val fakeConsole = FakeConsole()
launch {
continuationSteal(fakeConsole)
}
assertNotNull(continuation).resume(fakeText)
assertEquals("After", fakeConsole.printed.last())
}
@Test
fun `Only Before is printed before resume`() = runTest(UnconfinedTestDispatcher()) {
val fakeConsole = FakeConsole()
val job = launch {
continuationSteal(fakeConsole)
}
assertEquals("Before", fakeConsole.printed.first())
job.cancel()
}
@Test
fun `After resume function should print text to resume`() = runTest(UnconfinedTestDispatcher()) {
val fakeConsole = FakeConsole()
launch {
continuationSteal(fakeConsole)
}
continuation?.resume(fakeText)
assertEquals(3, fakeConsole.printed.size)
assertEquals(fakeText, fakeConsole.printed[1])
}
}
Marcin Moskala is a highly experienced developer and Kotlin instructor as the founder of Kt. Academy, an official JetBrains partner specializing in Kotlin training, Google Developers Expert, known for his significant contributions to the Kotlin community. Moskala is the author of several widely recognized books, including "Effective Kotlin," "Kotlin Coroutines," "Functional Kotlin," "Advanced Kotlin," "Kotlin Essentials," and "Android Development with Kotlin."
Beyond his literary achievements, Moskala is the author of the largest Medium publication dedicated to Kotlin. As a respected speaker, he has been invited to share his insights at numerous programming conferences, including events such as Droidcon and the prestigious Kotlin Conf, the premier conference dedicated to the Kotlin programming language.