https://kotlinlang.org logo
Title
e

eygraber

08/11/2021, 4:28 AM
I have a wrapper class for CoroutineDispatcher that I use so that for tests I can provide test dispatchers:
interface MyDispatchers {
  val main: CoroutineDispatcher
  val io: CoroutineDispatcher
  val computation: CoroutineDispatcher
  val unconfined: CoroutineDispatcher
}
Instead of using
Dispatchers.*
I use one of the properties from `MyDispatchers`(which are set to the corresponding
Dispatchers.*
) Is this necessary, or does the coroutines test artifact have a construct that allows you to use
Dispatchers.*
and control them during a test?
:yes: 2
e

ephemient

08/11/2021, 8:33 AM
the others can't - I'm not sure it ever makes sense to change Dispatchers.Unconfined, as it has some unique behavior
e

eygraber

08/11/2021, 5:57 PM
So the correct approach would be to keep what I have, and for tests, set the Dispatchers to a
TestCoroutineDispatcher
(aside from Unconfined because of its behavior)? Or am I better off using
Dispatchers.*
and rely on the behavior of
runBlockingTest
for time advancement, etc... The reason I ask is because it's pretty clunky passing
MyDispatchers
around, but I don't want to not use it, and paint myself into a corner later on if I need a test dispatcher.
e

ephemient

08/11/2021, 6:05 PM
my team has chosen to use our own Dispatchers singleton object, like the built in one but with IO settable
e

eygraber

08/11/2021, 6:08 PM
Can you share some examples of the things you do when overriding IO?
e

ephemient

08/11/2021, 6:16 PM
the only thing we override it to is the test dispatcher
(which is customized to log uncaught exceptions and report them at the end of the test, but anyway)
e

eygraber

08/11/2021, 6:41 PM
Ok thanks for the help