-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
dachlatten-flow/src/main/java/de/sipgate/dachlatten/flow/FlowCycler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package de.sipgate.dachlatten.flow | ||
|
||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.combine | ||
import kotlinx.coroutines.flow.flow | ||
import kotlin.time.Duration | ||
|
||
fun tickEvery(time: Duration) = flow { | ||
while (true) { | ||
emit(Unit) | ||
delay(time) | ||
} | ||
} | ||
|
||
fun <T> cycleBetween( | ||
tickerInterval: Duration, | ||
flow1: Flow<T>, | ||
flow2: Flow<T>, | ||
): Flow<T> = cycleBetween(tickEvery(tickerInterval), flow1, flow2) | ||
|
||
fun <I,T> cycleBetween( | ||
ticker: Flow<I>, | ||
flow1: Flow<T>, | ||
flow2: Flow<T>, | ||
): Flow<T> { | ||
var first = false | ||
return combine(ticker, flow1, flow2) { _, a, b -> | ||
first = !first | ||
return@combine when { | ||
first -> a | ||
else -> b | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
dachlatten-flow/src/test/java/de/sipgate/dachlatten/flow/FlowCyclerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package de.sipgate.dachlatten.flow | ||
|
||
import app.cash.turbine.test | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.flowOf | ||
import kotlinx.coroutines.test.advanceTimeBy | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
class FlowCyclerTest { | ||
|
||
@Test | ||
fun cycleBetweenSwitchesTheFlowBeingEmitted() = runTest { | ||
val flow1 = flowOf("flow1") | ||
val flow2 = flowOf("flow2") | ||
|
||
val trigger = MutableStateFlow(1) | ||
|
||
cycleBetween(trigger, flow1, flow2).test { | ||
assertEquals("flow1", awaitItem()) | ||
trigger.value++ | ||
assertEquals("flow2", awaitItem()) | ||
trigger.value++ | ||
assertEquals("flow1", awaitItem()) | ||
} | ||
} | ||
|
||
@Test | ||
fun cycleBetweenWorksWithNullValues() = runTest { | ||
val flow1 = flowOf("flow1") | ||
val flow2 = flowOf<String?>(null) | ||
|
||
val trigger = MutableStateFlow(1) | ||
|
||
cycleBetween(trigger, flow1, flow2).test { | ||
assertEquals("flow1", awaitItem()) | ||
trigger.value++ | ||
assertEquals(null, awaitItem()) | ||
trigger.value++ | ||
assertEquals("flow1", awaitItem()) | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
@Test | ||
fun cycleBetweenWithDurationWorks() = runTest { | ||
val flow1 = flowOf("flow1") | ||
val flow2 = flowOf<String?>("flow2") | ||
|
||
val trigger = MutableStateFlow(1) | ||
|
||
cycleBetween(5.seconds, flow1, flow2).test { | ||
assertEquals("flow1", awaitItem()) | ||
expectNoEvents() | ||
advanceTimeBy(5.seconds) | ||
assertEquals("flow2", awaitItem()) | ||
expectNoEvents() | ||
advanceTimeBy(5.seconds) | ||
assertEquals("flow1", awaitItem()) | ||
} | ||
} | ||
} |