mirror of
https://github.com/InsanusMokrassar/krontab.git
synced 2025-12-03 03:26:02 +00:00
asFlow extension
This commit is contained in:
@@ -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)
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user