If `turbine` gurus could chime in ```private val...
# squarelibraries
u
If
turbine
gurus could chime in
Copy code
private val _state = MutableStateFlow<ChatState>(ChatState.Idle)
override val state: Flow<ChatState> get() = _state

scope.launch {
    combine(
        conversationDao.conversation.doOnNext { println("-- CONVERSATION=$it") },
        connector.connectionState.doOnNext { println("-- CONN_STATE=$it") },
        ::stateMapper
    )
        .collect {
            println("--- PRODUCED=$it")
            _state.value = it
        }
}
I have
combine
which produces as a result and pipes it into stateflow
_state
. That
state
flow is then asserted at test time with turbine (ommited) Issue is that for some reason this is not deterministic. Default value of
connectionState
is
Disconnected
and then obviously
Connecting etc
What I'm seeing is that sometimes I see
Connecting
getting emitted before the product of combining with
Disconnected
gets set to the
_state
Othertimes it's not and I see 2 `--- PRODUCED`as expected. Is this a
StateFlow
issue? A
combine
issue? A Turbine issue? A
TestDispatcher
issue? Should I maybe not assert all the intermediary emits and just the last one using?
I suspect it the
_state.value = it
part maybe?
c
I suspect the problem is because it is a StateFlow, you might be seeing conflation happening. A StateFlow is not guaranteed to emit every intermediate state.
u
yes it does seem to be the case (if I just expose the
combine
directly
override val state = combine ...
) but what to do in turbine tests? At runtime I don' really case if such transient product exists or not Can I maybe assert just the latest value in turbine? I notice
turbine.expectMostRecentItem()
but doesnt really behave how I'd expect it to -- is it not alias for draining the queue?
c
I will need to check my code later but if I remember you would probably need to call
advanceUntilIdle()
before that
expectMostRecentItem()
u
doesn't seem to help 😕
would you advice against something like
awaitItem { ... predicate }
to skip the uninteresting intermediate emits?