question about testing in ktor-client: So, runBlo...
# ktor
p
question about testing in ktor-client: So, runBlockingTest {} (from kotlin-test) creates a TestDispatcher and runs its block on said dispatcher, and then fails if any work is left running at the end of the test (say, on another dispatcher). I’d assume that the ktor MockEngine is meant to be used in tests as well (where best practice seems to be to use runBlockingTest {} instead of runBlocking {}). But MockEngine creates its own dispatcher (by calling Dispatchers.clientDispatcher()), and calls to client.request() end up scheduling work on that dispatcher, which seems to cause tests that rely on a TestCoroutineDispatcher to fail. Am I understanding this correctly, or am I missing something obvious?
a
Please see the related conversation and KTOR-2629.
p
thanks!
sorry to rehash something already known!
oh interesting --- @Aleksei Tirman [JB] so I see now that the bug was declined. The sense I’ve gotten from blog posts and from #coroutines here is that using runBlocking {} in tests is considered bad practice --- is there any hope that being able to pass in a custom CoroutineDispatcher might happen? Or is it unlikely that it’ll be possible to test ktor code with a TestCoroutineDispatcher at any point in the future?
(this came up because I was going through and removing instances of runBlocking {} from our tests because it looked like we might have tests that were unwittingly leaking work onto other dispatchers)
a
@e5l please give a comment.
p
it’d be nice if MockEngine() at least allowed passing in a dispatcher/context!
though I understand wanting to encapsulate that detail
e
Hey @Patrick Ramsey , thanks for the notice. Ktor code needs actual suspension. We can provide such extension point, but I’m not sure that it will 100% solve the problem.
btw let me investigate why runBlockingTest doesn’t work
p
for my use case at least, I don’t need much of ktor’s internal logic to work --- I’m just using MockEngine because it’s not easy to mock the actual request() method (since that stuff’s all inline)
just wanna test the code that uses it!
and thanks, @e5l for responding and for looking into it!