Exercise: DiscNewsRepository
DiscNewsRepository
is a simple class that uses DiscReader
to read stored news from disk. The problem is that reading data from disk is a blocking operation, therefore the current implementation of DiscNewsRepository
blocks threads in suspending functions, which leads to performance problems in your application. Fix this problem! Assume that getNews
is used intensively and you want to support 200 parallel reads. Starting code:
class DiscNewsRepository(
private val discReader: DiscReader
) : NewsRepository {
override suspend fun getNews(newsId: String): News {
val (title, content) = discReader.read("user/$newsId")
return News(title, content)
}
}
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/dispatcher/DiscNewsRepository.kt. You can find there starting code and unit tests.
Once you are done with the exercise, you can check your solution here.
import kotlinx.coroutines.*
import org.junit.Test
import kotlin.system.measureTimeMillis
import kotlin.test.assertEquals
class DiscNewsRepository(
private val discReader: DiscReader
) : NewsRepository {
override suspend fun getNews(newsId: String): News {
val (title, content) = discReader.read("user/$newsId")
return News(title, content)
}
}
interface DiscReader {
fun read(key: String): List<String>
}
interface NewsRepository {
suspend fun getNews(newsId: String): News
}
data class News(val title: String, val content: String)
@Suppress("FunctionName")
class DiscNewsRepositoryTests {
@Test
fun `should read data from disc using DiscReader`() = runBlocking {
val repo = DiscNewsRepository(OneSecDiscReader(listOf("Some title", "Some content")))
val res = repo.getNews("SomeUserId")
assertEquals("Some title", res.title)
assertEquals("Some content", res.content)
}
class ImmediateDiscReader(val map: Map<String, List<String>>) : DiscReader {
override fun read(key: String): List<String> = map[key] ?: error("Element not found")
}
@Test
fun `should be prepared for many reads at the same time`() = runBlocking<Unit> {
val repo = DiscNewsRepository(OneSecDiscReader(listOf("Some title", "Some content")))
val time = measureTimeMillis {
coroutineScope {
repeat(10) { id ->
launch {
repo.getNews("SomeUserId$id")
}
}
}
}
assert(time < 2000) { "Should take less than 2000, took $time" }
}
@Test(timeout = 2000)
fun `should be prepared for 200 parallel reads`() = runBlocking<Unit> {
val repo = DiscNewsRepository(OneSecDiscReader(listOf("Some title", "Some content")))
val time = measureTimeMillis {
coroutineScope {
repeat(200) { id ->
launch {
repo.getNews("SomeUserId$id")
}
}
}
}
assert(time < 1900) { "Should take less than 2000, took $time" }
}
@Test(timeout = 3000)
fun `should not allow more than 200 parallel reads`() = runBlocking<Unit> {
val repo = DiscNewsRepository(OneSecDiscReader(listOf("Some title", "Some content")))
val time = measureTimeMillis {
coroutineScope {
repeat(201) { id ->
launch {
repo.getNews("SomeUserId$id")
}
}
}
}
assert(time > 2000) { "Should take longer than 2000, took $time" }
}
class OneSecDiscReader(private val response: List<String>) : DiscReader {
override fun read(key: String): List<String> {
Thread.sleep(1000)
return response
}
}
}
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.