What would be the ideal way to convert a `Coroutin...
# coroutines
z
What would be the ideal way to convert a
CoroutineScope
or
CoroutineContext
to an
ExecutorService
? context is that I'm writing a test (so the
CoroutineScope
comes from the
runTest { }
function), and I need to inject an
okhttp3.Dispatcher
(which requires an
ExecutorService
to construct, docs) into an
OkHttpClient.Builder
s
It's possible, but not trivial (you can get an Executor easily, but not a full ExecutorService) so I'd think carefully about whether you need it. Why not just use a separate thread?
z
I thought it might be good if all work is done under the thread/dispatcher owned by
runTest { .. }
, but maybe it doesn’t matter. also thinking maybe switching to ktor bc I’m sure the integration with coroutines here is better
s
If you have something that's working okay, I wouldn't switch. My experiences with Ktor HTTP client haven't been the smoothest. As far as the dispatcher choice for the tests, the main things to consider are: • Making sure errors in the HTTP client are captured by the test. • Making sure the test waits for the HTTP client to finish all its work. • Making sure the HTTP client and its resources are shut down when the test is finished. The first two are probably already taken care of for you by whatever wrapper you're using to add suspending function support to OkHttp. If you can set up the third one properly too, it shouldn't matter too much which thread you're actually using.