Davide Giuseppe Farella
09/17/2020, 7:34 AMFlow
like
flow {
while (true) {
emit(something)
delay(interval)
}
}.flowOn(injectedDispatcher)
In my test, I inject the dispatcher and test like
val result = mutableListOf<Int>()
val interval = 30.seconds
val flow = getSavedCredentials(interval)
val job = launch {
flow.toList(result)
}
repo.add(1)
advanceTimeBy(interval)
repo.add(2)
advanceTimeBy(interval)
repo.add(3)
advanceTimeBy(interval)
So I would expect result
to be [ 1 ], [ 1, 2 ], [ 1, 2, 3 ]
But instead it is [ 1, 2, 3 ], [ 1, 2, 3 ], [ 1, 2, 3 ]
What’s wrong with it? 😵gildor
09/17/2020, 7:50 AMDavide Giuseppe Farella
09/17/2020, 7:51 AMgildor
09/17/2020, 7:52 AMDavide Giuseppe Farella
09/17/2020, 7:53 AMgildor
09/17/2020, 8:23 AMCan I use coroutines-test on Playground? (edited)Yes
Davide Giuseppe Farella
09/17/2020, 8:27 AMgildor
09/17/2020, 8:50 AMDavide Giuseppe Farella
09/17/2020, 8:54 AMgildor
09/17/2020, 8:56 AMemit(something)
With
emit(something.toList())
repo += 3
flow is not even dispatched, because it’s blocking code (mutating repo) but launch and flow are asyncronousDavide Giuseppe Farella
09/17/2020, 8:59 AMMap
, but our test implementation is a MutableMap
under the hood!also, to make your test pass, swap emit and delay, otherwise flow will emit empty listYup, noticed, May be better to delay the collecting