asFlow extension

This commit is contained in:
2020-01-13 10:15:01 +06:00
parent 5e4065ee53
commit 1d202a7311
7 changed files with 84 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
package com.insanusmokrassar.krontab.utils
import kotlinx.coroutines.CoroutineScope
/**
* Workaround to use suspending functions in unit tests
*/
expect fun runTest(block: suspend (scope : CoroutineScope) -> Unit)

View File

@@ -0,0 +1,34 @@
package com.insanusmokrassar.krontab.utils
import com.insanusmokrassar.krontab.builder.buildSchedule
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.takeWhile
import kotlin.test.Test
import kotlin.test.assertEquals
@ExperimentalCoroutinesApi
@FlowPreview
class SchedulerFlowTests {
@Test
fun testThatFlowIsCorrectlyWorkEverySecond() {
val kronScheduler = buildSchedule {
seconds {
0 every 1
}
}
val flow = kronScheduler.asFlow()
runTest {
val mustBeCollected = 10
var collected = 0
flow.takeWhile {
collected < mustBeCollected
}.collect {
collected++
}
assertEquals(mustBeCollected, collected)
}
}
}