Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing default dispatchers used during suspend calls of Retrofit with test dispatchers. #4206

Closed
shubhamgarg1 opened this issue Aug 12, 2024 · 2 comments

Comments

@shubhamgarg1
Copy link

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?

@shubhamgarg1
Copy link
Author

shubhamgarg1 commented Aug 12, 2024

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.

@JakeWharton
Copy link
Member

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.

@JakeWharton JakeWharton closed this as not planned Won't fix, can't repro, duplicate, stale Aug 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants