Changing default dispatchers used during suspend calls of Retrofit with test dispatchers.
shubhamgarg1 opened this issue · 2 comments
What kind of issue is this?
- Feature Request.
Changing default dispatchers used during suspend call with test dispatchers.
In our codebase, we launch a bunch of coroutines using scopes and dispatchers which we change in our tests to TestScope and StandardTestDispatchers. This helps us control the execution of those coroutines.
With Retrofit suspend calls, we are currently not able to replace the dispatchers used internally by it. So, we can't ensure that after calling testScope.testScheduler.advanceUntilIIdle, the coroutines that have been started have completed their work.
For example.
class ClassUnderTest(scope: CoroutineScope)
{
fun callRetofitAPI()
{
scope.launch {
// Get Retrofit Service
val response = service.makeApiCall()
// do some more work.
}
}
}
interface RetrofitService
{
suspend fun makeApiCall(): Response<SomeClass>
}
@Test
fun test() = testScope.runTest {
val classUnderTest = ClassUnderTest(testScope)
classUnderTest.callRetofitAPI()
testScope.testScheduler().advanceUntillIdle()
// Doesn't ensure that response would have been computed as Retrofit's dispatchers aren't replaced.
}
Is there some way or plan to allow replacing the dispatchers used internally by Retrofit?
Room which provides similar suspend functions provides setTransactionExecutor and setQueryExecutor methods that allows one to set the Executor. One can use asExecutor method on a dispatcher whose scheduler has been changed during tests.
Retrofit does not use dispatchers. Our suspend funs support delegates to OkHttp's Call.enqueue and threading is entirely encapsulated within that library. You can change the executor that its Dispatcher uses, but there's nothing to be done within Retrofit itself.