fillup readme with asFlow info and add one more test

This commit is contained in:
2020-01-13 10:34:35 +06:00
parent 1d202a7311
commit 1678b637a6
3 changed files with 69 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
package com.insanusmokrassar.krontab.utils
import kotlinx.coroutines.*
fun CoroutineScope.createFailJob(forTimeMillis: Long) = launch {
delay(forTimeMillis)
throw IllegalStateException("This job must be completed at before this exception happen")
}

View File

@@ -1,6 +1,7 @@
package com.insanusmokrassar.krontab.utils
import com.insanusmokrassar.krontab.builder.buildSchedule
import com.soywiz.klock.DateTime
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.takeWhile
@@ -31,4 +32,38 @@ class SchedulerFlowTests {
assertEquals(mustBeCollected, collected)
}
}
@Test
fun testThatFlowIsCorrectlyWorkEverySecondWithMuchOfEmitters() {
val kronScheduler = buildSchedule {
seconds {
0 every 1
}
}
val flow = kronScheduler.asFlow()
runTest {
val testsCount = 10
val failJob = it.createFailJob((testsCount * 2) * 1000L)
val mustBeCollected = 10
val answers = (0 until testsCount).map { _ ->
it.async {
var collected = 0
flow.takeWhile {
collected < mustBeCollected
}.collect {
collected++
}
collected
}
}.awaitAll()
failJob.cancel()
answers.forEach {
assertEquals(mustBeCollected, it)
}
}
}
}