Last active
July 6, 2021 14:04
-
-
Save timrijckaert/81ea82b4f16426af92c4d59cc8679871 to your computer and use it in GitHub Desktop.
Use cases with Flow
This file contains hidden or 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
interface FlowProvider { | |
fun returnsAColdFlow(): Flow<Int> = flow { | |
repeat(5) { | |
emit(it) | |
delay(100) | |
} | |
} | |
} | |
object DefaultFlowProvider : FlowProvider | |
class FlowPlayground(private val flowProvider: FlowProvider = DefaultFlowProvider) { | |
val globalScope: CoroutineScope = CoroutineScope(EmptyCoroutineContext) | |
val flowOf: Flow<Int> = flowOf(1, 2, 3, 4, 5) | |
fun sharedFlow(coroutineScope: CoroutineScope): SharedFlow<Int> = flowOf.shareIn(coroutineScope, SharingStarted.Eagerly, 0) | |
val sharedFlowEagerly: SharedFlow<Int> = flowOf.shareIn(globalScope, SharingStarted.Eagerly, 1) | |
val sharedFlowLazily: SharedFlow<Int> = flowOf.shareIn(globalScope, SharingStarted.Lazily, 1) | |
val sharedFlowWhileSubscribed: SharedFlow<Int> = flowOf.shareIn(globalScope, SharingStarted.WhileSubscribed(), 1) | |
suspend fun stateFlow(coroutineScope: CoroutineScope): StateFlow<Int> = flowOf.stateIn(coroutineScope) | |
fun stateFlow2(coroutineScope: CoroutineScope): StateFlow<Int> = flowOf.stateIn(coroutineScope, SharingStarted.Lazily, 0) | |
val stateFlowLazily: StateFlow<Int> = flowOf.stateIn(globalScope, SharingStarted.Lazily, 0) | |
val stateFlowEagerly: StateFlow<Int> = flowOf.stateIn(globalScope, SharingStarted.Eagerly, 0) | |
val stateFlowWhileSubscribed: StateFlow<Int> = flowOf.stateIn(globalScope, SharingStarted.WhileSubscribed(), 0) | |
val transformColdFlowToHotFlow: StateFlow<Int> = | |
flowProvider.returnsAColdFlow() | |
.drop(1) | |
.map { it * 2 } | |
.stateIn(globalScope, SharingStarted.Lazily, 0) | |
} | |
class FlowPlaygroundTest : StringSpec({ | |
"cold flow to hot flow" { | |
val sut = FlowPlayground() | |
sut.transformColdFlowToHotFlow.test { | |
expectItem() shouldBe 0 | |
expectItem() shouldBe 2 | |
expectItem() shouldBe 4 | |
expectItem() shouldBe 6 | |
} | |
} | |
"cold flow to hot flow - with mockk" { | |
val sut = FlowPlayground(mockk { | |
every { returnsAColdFlow() } returns flowOf(7, 8, 9) | |
}) | |
sut.transformColdFlowToHotFlow.test { | |
expectItem() shouldBe 0 | |
expectItem() shouldBe 16 | |
expectItem() shouldBe 18 | |
cancelAndConsumeRemainingEvents() | |
} | |
} | |
"flowOf" { | |
FlowPlayground().flowOf.test { | |
expectItem() shouldBe 1 | |
expectItem() shouldBe 2 | |
expectItem() shouldBe 3 | |
expectItem() shouldBe 4 | |
expectItem() shouldBe 5 | |
expectComplete() | |
} | |
} | |
"shared flow - function" { | |
coroutineScope { | |
FlowPlayground().sharedFlow(this).test { | |
expectItem() shouldBe 1 | |
expectItem() shouldBe 2 | |
expectItem() shouldBe 3 | |
expectItem() shouldBe 4 | |
expectItem() shouldBe 5 | |
cancelAndConsumeRemainingEvents() | |
} | |
} | |
} | |
"shared flow eagerly" { | |
val sut = FlowPlayground() | |
sut.sharedFlowEagerly.test { | |
expectItem() shouldBe 5 | |
cancelAndConsumeRemainingEvents() | |
} | |
sut.sharedFlowEagerly.test { | |
expectItem() shouldBe 5 | |
cancelAndConsumeRemainingEvents() | |
} | |
} | |
"shared flow lazily" { | |
val sut = FlowPlayground() | |
sut.sharedFlowLazily.test { | |
expectItem() shouldBe 1 | |
expectItem() shouldBe 2 | |
expectItem() shouldBe 3 | |
expectItem() shouldBe 4 | |
expectItem() shouldBe 5 | |
cancelAndConsumeRemainingEvents() | |
} | |
sut.sharedFlowLazily.test { | |
expectItem() shouldBe 5 | |
cancelAndConsumeRemainingEvents() | |
} | |
} | |
"shared flow while subscribed" { | |
val sut = FlowPlayground() | |
sut.sharedFlowWhileSubscribed.test { | |
expectItem() shouldBe 1 | |
expectItem() shouldBe 2 | |
expectItem() shouldBe 3 | |
expectItem() shouldBe 4 | |
expectItem() shouldBe 5 | |
cancelAndConsumeRemainingEvents() | |
} | |
sut.sharedFlowWhileSubscribed.test { | |
expectItem() shouldBe 5 | |
cancelAndConsumeRemainingEvents() | |
} | |
} | |
"state flow - function" { | |
coroutineScope { | |
val sut = FlowPlayground() | |
sut.stateFlow(this).test { | |
expectItem() shouldBe 5 | |
cancelAndConsumeRemainingEvents() | |
} | |
} | |
} | |
"state flow 2 - function" { | |
coroutineScope { | |
val sut = FlowPlayground() | |
sut.stateFlow2(this).test { | |
expectItem() shouldBe 0 | |
expectItem() shouldBe 1 | |
expectItem() shouldBe 2 | |
expectItem() shouldBe 3 | |
expectItem() shouldBe 4 | |
expectItem() shouldBe 5 | |
cancelAndConsumeRemainingEvents() | |
} | |
sut.stateFlow2(this).test { | |
expectItem() shouldBe 0 | |
expectItem() shouldBe 1 | |
expectItem() shouldBe 2 | |
expectItem() shouldBe 3 | |
expectItem() shouldBe 4 | |
expectItem() shouldBe 5 | |
cancelAndConsumeRemainingEvents() | |
} | |
} | |
} | |
"state flow - lazily" { | |
val sut = FlowPlayground() | |
sut.stateFlowLazily.test { | |
expectItem() shouldBe 0 | |
expectItem() shouldBe 1 | |
expectItem() shouldBe 2 | |
expectItem() shouldBe 3 | |
expectItem() shouldBe 4 | |
expectItem() shouldBe 5 | |
cancelAndConsumeRemainingEvents() | |
} | |
sut.stateFlowLazily.test { | |
expectItem() shouldBe 5 | |
cancelAndConsumeRemainingEvents() | |
} | |
} | |
"state flow - eagerly" { | |
val sut = FlowPlayground() | |
sut.stateFlowEagerly.test { | |
expectItem() shouldBe 5 | |
cancelAndConsumeRemainingEvents() | |
} | |
sut.stateFlowEagerly.test { | |
expectItem() shouldBe 5 | |
cancelAndConsumeRemainingEvents() | |
} | |
} | |
"state flow - while subscribed" { | |
val sut = FlowPlayground() | |
sut.stateFlowWhileSubscribed.test { | |
expectItem() shouldBe 0 | |
expectItem() shouldBe 1 | |
expectItem() shouldBe 2 | |
expectItem() shouldBe 3 | |
expectItem() shouldBe 4 | |
expectItem() shouldBe 5 | |
cancelAndConsumeRemainingEvents() | |
} | |
sut.stateFlowWhileSubscribed.test { | |
expectItem() shouldBe 5 | |
cancelAndConsumeRemainingEvents() | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment