Skip to content

Commit f185345

Browse files
committed
Use TestCoroutineScope, pass also TestCoroutineDispatcher to control execution and delay, do not skip delays with runBlockingTest{}
Kotlin/kotlinx.coroutines#1266
1 parent 591c375 commit f185345

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

core/src/main/java/xyz/kewiany/contactus/core/DispatcherProvider.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ import kotlinx.coroutines.Dispatchers
55

66

77
interface DispatcherProvider {
8-
98
fun main(): CoroutineDispatcher = Dispatchers.Main
109
fun default(): CoroutineDispatcher = Dispatchers.Default
1110
fun io(): CoroutineDispatcher = Dispatchers.IO
1211
fun unconfined(): CoroutineDispatcher = Dispatchers.Unconfined
13-
1412
}
1513

16-
class DefaultDispatcherProvider : DispatcherProvider
14+
class DefaultDispatcherProvider : DispatcherProvider
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package xyz.kewiany.contactus.logic
22

3-
import kotlinx.coroutines.delay
4-
import kotlinx.coroutines.withContext
3+
import kotlinx.coroutines.*
54
import xyz.kewiany.contactus.core.DefaultDispatcherProvider
65
import xyz.kewiany.contactus.core.DispatcherProvider
76
import xyz.kewiany.contactus.logic.common.MainViewState
7+
import kotlin.coroutines.CoroutineContext
88

9-
suspend fun MainViewState.MainLogic(dispatchers: DispatcherProvider = DefaultDispatcherProvider()): Boolean {
9+
suspend fun MainViewState.MainLogic(
10+
dispatchers: DispatcherProvider = DefaultDispatcherProvider()
11+
): Boolean {
1012
commonViewState.isLoading = true
11-
withContext(dispatchers.io()) {
12-
delay(5000)
13-
commonViewState.isLoading = false
14-
}
13+
withContext(dispatchers.io()) {
14+
delay(1000)
15+
commonViewState.isLoading = false
16+
}
17+
1518
return true
1619
}

logic/src/test/java/xyz/kewiany/contactus/CustomFreeSpec.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import kotlinx.coroutines.CoroutineDispatcher
88
import kotlinx.coroutines.Dispatchers
99
import kotlinx.coroutines.ExperimentalCoroutinesApi
1010
import kotlinx.coroutines.test.TestCoroutineDispatcher
11+
import kotlinx.coroutines.test.TestCoroutineScope
1112
import kotlinx.coroutines.test.resetMain
1213
import kotlinx.coroutines.test.setMain
1314
import org.junit.Rule
@@ -18,6 +19,7 @@ abstract class CustomFreeSpec constructor(
1819
) : FreeSpec(body as FreeSpec.() -> Unit) {
1920

2021
val testDispatcher = TestCoroutineDispatcher()
22+
val testScope = TestCoroutineScope(testDispatcher)
2123
val testDispatcherProvider = object : DispatcherProvider {
2224
override fun default(): CoroutineDispatcher = testDispatcher
2325
override fun io(): CoroutineDispatcher = testDispatcher
@@ -35,4 +37,4 @@ abstract class CustomFreeSpec constructor(
3537
Dispatchers.resetMain()
3638
testDispatcher.cleanupTestCoroutines()
3739
}
38-
}
40+
}

logic/src/test/java/xyz/kewiany/contactus/MainLogicTest.kt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
package xyz.kewiany.contactus
22

33
import io.kotest.matchers.shouldBe
4-
import kotlinx.coroutines.test.TestCoroutineScope
5-
import kotlinx.coroutines.test.runBlockingTest
4+
import kotlinx.coroutines.launch
65
import xyz.kewiany.contactus.logic.MainLogic
76
import xyz.kewiany.contactus.logic.common.MainViewState
87

98
internal class MainLogicTest : CustomFreeSpec({
109
val state = MainViewState()
1110
val stateT = MainStateT(state)
1211

13-
beforeTest {
14-
testDispatcher.runBlockingTest {
12+
"test loading" - {
13+
testScope.launch {
1514
state.MainLogic(testDispatcherProvider)
1615
}
16+
"load" {
17+
stateT.state.commonViewState.isLoading shouldBe true
18+
}
19+
testScope.advanceTimeBy(1000)
20+
"do not load" {
21+
stateT.state.commonViewState.isLoading shouldBe false
22+
}
1723
}
24+
})
1825

19-
"test" - {
20-
stateT.state.commonViewState.isLoading shouldBe false
21-
}
22-
})

0 commit comments

Comments
 (0)