https://kotlinlang.org logo
Title
j

John O'Reilly

07/27/2021, 4:49 PM
Is anyone by chance aware of any threading related changes that were made in
MockEngine
in 1.5.1.....we're updating a client here from a 1.4.x version and have isolated issue we're seeing to update from 1.5.0 to 1.5.1.....we have unit tests that are using
runBlockingTest
and getting
IllegalStateException: This job has not completed yet
after that update....digging deeper to try and narrow in on what's changed but just in case anyone encountered this before....
Alternatively, are there any particular recommended best practices for setting up ktor related unit tests like this (that perhaps we weren't previously following)?
j

Javier

07/27/2021, 4:59 PM
if you change runBlockingTest to runBlocking, it works?
j

John O'Reilly

07/27/2021, 5:02 PM
So, that seems to affect things all right but other part of test fails then.....haven't had chance yet to dig in to that
I went through changelog for 1.5.1 but nothing jumped out
a

Aleksei Tirman [JB]

07/28/2021, 10:04 AM
You can find an explanation here https://youtrack.jetbrains.com/issue/KTOR-2629
j

John O'Reilly

07/28/2021, 10:05 AM
so, we can't use
runBlockingTest
for unit tests that use
MockEngine
?
a

Aleksei Tirman [JB]

07/28/2021, 10:07 AM
I did encounter the same issue and didn't find a way to solve it. @e5l is there a way to use
runBlockingTest
?
j

John O'Reilly

07/28/2021, 10:25 AM
perhaps pattern like following could be used to allow test to inject/replace dispatcher used? https://developer.android.com/kotlin/coroutines/coroutines-best-practices#inject-dispatchers
e

e5l

07/28/2021, 10:48 AM
Hey @John O'Reilly. The problem is caused by context change in MockEngine. Most likely testing library you’re using is not assuming that there are no suspension points in test. It should be some
await
method there. Could you provide the whole code snippet to figure it out?
j

John O'Reilly

07/28/2021, 10:51 AM
Hi @e5l, I'm not using any other testing library....just standard unit test with
runBlockingTest
and then testing one of our classes that in turn is using another class that uses
MockEngine
based
HttpClient
. Do you know of any examples/samples etc that are able to use
runBlockingTest
that I can maybe compare to?
e

e5l

07/28/2021, 10:52 AM
Could you check if the code is working with a regular
runBlocking
?
j

John O'Reilly

07/28/2021, 10:53 AM
Partly, yes....but running in to other issues then which I'm still trying to get to bottom of
a

Aleksei Tirman [JB]

07/28/2021, 10:53 AM
The one problem is that you have to wait for all delays.
j

John O'Reilly

07/28/2021, 11:02 AM
fwiw we're injecting
TestCoroutineDispatcher()
when constructing the class under test
e

e5l

07/28/2021, 11:03 AM
Please note:
When switched to lazy execution using pauseDispatcher any coroutines started via launch or async will not execute until a call to DelayController.runCurrent or the virtual clock-time has been advanced via one of the methods on DelayController.
j

John O'Reilly

07/28/2021, 11:12 AM
we are using those in some other tests....let me see if/how they can be applied here too
Just to clarify though....we're not using
pauseDispatcher
in these particular tests right now