https://kotlinlang.org logo
#coroutines
Title
# coroutines
d

Davide Giuseppe Farella

09/17/2020, 7:34 AM
Hello, I got a
Flow
like
Copy code
flow {
    while (true) {
        emit(something)
        delay(interval)
    }
}.flowOn(injectedDispatcher)
In my test, I inject the dispatcher and test like
Copy code
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? 😵
g

gildor

09/17/2020, 7:50 AM
try not exact number, 30 for flow, use a bit gap between, like advanceTimeBy(interval + 5.seconds)
d

Davide Giuseppe Farella

09/17/2020, 7:51 AM
Same 😞
g

gildor

09/17/2020, 7:52 AM
could you provide a complete example on Kotlin Playground?
d

Davide Giuseppe Farella

09/17/2020, 7:53 AM
Sure, let me try to reproduce without dependants to project
Can I use coroutines-test on Playground?
🤔
May that help?
message has been deleted
g

gildor

09/17/2020, 8:23 AM
Can I use coroutines-test on Playground? (edited)
Yes
d

Davide Giuseppe Farella

09/17/2020, 8:27 AM
How to? Deps are not resolved 😵
g

gildor

09/17/2020, 8:50 AM
ahh, TestCoroutineDispatcher wouldn’t be available, indeed, worth to request adding it to Junit run configuration
d

Davide Giuseppe Farella

09/17/2020, 8:54 AM
So is there something I can do? 🙂
g

gildor

09/17/2020, 8:56 AM
Yes
it’s because of closure of mutable property
Change
Copy code
emit(something)
With
Copy code
emit(something.toList())
What is happening, is that by time when flow lambda is invoking, repo alredy changed, essentially when you call
repo += 3
flow is not even dispatched, because it’s blocking code (mutating repo) but launch and flow are asyncronous
also, to make your test pass, swap emit and delay, otherwise flow will emit empty list
👍 1
d

Davide Giuseppe Farella

09/17/2020, 8:59 AM
Oh… This is tricky!! So that’s given by our mock! The signature of the method is
Map
, but our test implementation is a
MutableMap
under the hood!
Thank you!
also, to make your test pass, swap emit and delay, otherwise flow will emit empty list
Yup, noticed, May be better to delay the collecting
3 Views