Skip to content

Commit

Permalink
Use TestCoroutineScope, pass also TestCoroutineDispatcher to control …
Browse files Browse the repository at this point in the history
…execution and delay, do not skip delays with runBlockingTest{}

Kotlin/kotlinx.coroutines#1266
  • Loading branch information
kewiany committed Aug 21, 2020
1 parent 591c375 commit f185345
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import kotlinx.coroutines.Dispatchers


interface DispatcherProvider {

fun main(): CoroutineDispatcher = Dispatchers.Main
fun default(): CoroutineDispatcher = Dispatchers.Default
fun io(): CoroutineDispatcher = Dispatchers.IO
fun unconfined(): CoroutineDispatcher = Dispatchers.Unconfined

}

class DefaultDispatcherProvider : DispatcherProvider
class DefaultDispatcherProvider : DispatcherProvider
17 changes: 10 additions & 7 deletions logic/src/main/java/xyz/kewiany/contactus/logic/MainLogic.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package xyz.kewiany.contactus.logic

import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext
import kotlinx.coroutines.*
import xyz.kewiany.contactus.core.DefaultDispatcherProvider
import xyz.kewiany.contactus.core.DispatcherProvider
import xyz.kewiany.contactus.logic.common.MainViewState
import kotlin.coroutines.CoroutineContext

suspend fun MainViewState.MainLogic(dispatchers: DispatcherProvider = DefaultDispatcherProvider()): Boolean {
suspend fun MainViewState.MainLogic(
dispatchers: DispatcherProvider = DefaultDispatcherProvider()
): Boolean {
commonViewState.isLoading = true
withContext(dispatchers.io()) {
delay(5000)
commonViewState.isLoading = false
}
withContext(dispatchers.io()) {
delay(1000)
commonViewState.isLoading = false
}

return true
}
4 changes: 3 additions & 1 deletion logic/src/test/java/xyz/kewiany/contactus/CustomFreeSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.TestCoroutineScope
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import org.junit.Rule
Expand All @@ -18,6 +19,7 @@ abstract class CustomFreeSpec constructor(
) : FreeSpec(body as FreeSpec.() -> Unit) {

val testDispatcher = TestCoroutineDispatcher()
val testScope = TestCoroutineScope(testDispatcher)
val testDispatcherProvider = object : DispatcherProvider {
override fun default(): CoroutineDispatcher = testDispatcher
override fun io(): CoroutineDispatcher = testDispatcher
Expand All @@ -35,4 +37,4 @@ abstract class CustomFreeSpec constructor(
Dispatchers.resetMain()
testDispatcher.cleanupTestCoroutines()
}
}
}
19 changes: 11 additions & 8 deletions logic/src/test/java/xyz/kewiany/contactus/MainLogicTest.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package xyz.kewiany.contactus

import io.kotest.matchers.shouldBe
import kotlinx.coroutines.test.TestCoroutineScope
import kotlinx.coroutines.test.runBlockingTest
import kotlinx.coroutines.launch
import xyz.kewiany.contactus.logic.MainLogic
import xyz.kewiany.contactus.logic.common.MainViewState

internal class MainLogicTest : CustomFreeSpec({
val state = MainViewState()
val stateT = MainStateT(state)

beforeTest {
testDispatcher.runBlockingTest {
"test loading" - {
testScope.launch {
state.MainLogic(testDispatcherProvider)
}
"load" {
stateT.state.commonViewState.isLoading shouldBe true
}
testScope.advanceTimeBy(1000)
"do not load" {
stateT.state.commonViewState.isLoading shouldBe false
}
}
})

"test" - {
stateT.state.commonViewState.isLoading shouldBe false
}
})

0 comments on commit f185345

Please sign in to comment.