mirror of
https://github.com/InsanusMokrassar/krontab.git
synced 2024-11-22 16:23:55 +00:00
fillup readme with asFlow info and add one more test
This commit is contained in:
parent
1d202a7311
commit
1678b637a6
26
README.md
26
README.md
@ -11,6 +11,7 @@ runtime of applications.
|
|||||||
| [ How to use: Including in project ](#including-in-project) |
|
| [ How to use: Including in project ](#including-in-project) |
|
||||||
| [ How to use: Config from string ](#config-from-string) |
|
| [ How to use: Config from string ](#config-from-string) |
|
||||||
| [ How to use: Config via builder (DSL preview) ](#config-via-builder) |
|
| [ How to use: Config via builder (DSL preview) ](#config-via-builder) |
|
||||||
|
| [ How to use: KronScheduler as a Flow ](#KronScheduler-as-a-Flow) |
|
||||||
|
|
||||||
## How to use
|
## How to use
|
||||||
|
|
||||||
@ -124,3 +125,28 @@ kronScheduler.doInfinity {
|
|||||||
```
|
```
|
||||||
|
|
||||||
All of these examples will do the same things: print `Called` message every five seconds.
|
All of these examples will do the same things: print `Called` message every five seconds.
|
||||||
|
|
||||||
|
### KronScheduler as a Flow
|
||||||
|
|
||||||
|
Any `KronScheduler`can e converted to a `Flow<DateTime` using extension `asFlow`:
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
val kronScheduler = buildSchedule {
|
||||||
|
seconds {
|
||||||
|
0 every 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val flow = kronScheduler.asFlow()
|
||||||
|
```
|
||||||
|
|
||||||
|
So, in this case any operations related to flow are available and it is expected tt th will work correctly. For example,
|
||||||
|
it is possible to use this flow with `takeWhile`:
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
flow.takeWhile {
|
||||||
|
condition()
|
||||||
|
}.collect {
|
||||||
|
action()
|
||||||
|
}
|
||||||
|
```
|
||||||
|
@ -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")
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
package com.insanusmokrassar.krontab.utils
|
package com.insanusmokrassar.krontab.utils
|
||||||
|
|
||||||
import com.insanusmokrassar.krontab.builder.buildSchedule
|
import com.insanusmokrassar.krontab.builder.buildSchedule
|
||||||
|
import com.soywiz.klock.DateTime
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.coroutines.flow.collect
|
import kotlinx.coroutines.flow.collect
|
||||||
import kotlinx.coroutines.flow.takeWhile
|
import kotlinx.coroutines.flow.takeWhile
|
||||||
@ -31,4 +32,38 @@ class SchedulerFlowTests {
|
|||||||
assertEquals(mustBeCollected, collected)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user