Hi! I'm rather new to working with coroutines and ...
# coroutines
m
Hi! I'm rather new to working with coroutines and I have a question regarding testing them. I have a service similar to this
Copy code
fun handleDelete(extIds: List<String>) {
        val ids = someClient1.getInternalIds(extIds) // get deleteable IDs

        try {
            runBlocking(<http://Dispatchers.IO|Dispatchers.IO>) { // use IO dispatcher as someClient2 makes a call to some remote service. 
                ids.forEach{ launch { someClient2.deleteId(it) } }
            }
        } catch (e: Exception) {
            handleException(e)
        }
    }
and I want to test it. My team uses Mockito and not Mockk, if that makes any difference. Maybe the test does not make sense, but as I am not very familiar with coroutines I want to test that these are run correctly in paralell and that they exit the runBlocking block after they're all done. So I was thinking of mocking the responses from
someClient2
so that say i have
extIds = listof("123", "456")
and the calls take respectively 500ms and 750ms, I want to check that in total this function does not use much more time than ~750ms and not (500+750)=1250ms. Is this possible using Mockito? And is it actually a useful test? 🤔
m
Yeah, I saw this, but not sure how to use it in this scenario. All examples I see use a suspending function where they call delay, but how can I relate that to my function being a remote call? Also Mockito has a
ResponseWithDelay
or smth class, but that delay does not play nice with the timings in
runBlockingTest
it seems like
So thats why I am wondering if the test is actually a stupid one, as I am struggling to find a solution for this. Am I basically just testing that the coroutines work as Jetbrains say they do..? Or is what I am trying to achieve not possible using Mockito, and testing coroutines should be done using Mockk?
o
well, if you want to time it, this isn't really a suspend function, so it's actually out of coroutines land
just use your test executors
assertTimeout
or similar
realistically, your code isn't properly structured to time well
well, actually, it's runBlocking, so that should actually work out fine
but there's no way to do anything with the coroutines inside the method, they're not exposed in any way
m
Hmm, I could do as Jetbrains call it
extract function refactoring
and extract whats inside the launch into a suspending fcn, but yeah, then it is still only one function, and the paralell execution I want to test needs also the
forEach
to work, so that might not be a good idea anyway. Cool, I'll check that out now!