https://kotlinlang.org logo
Title
m

Michal Klimczak

03/16/2022, 10:59 AM
I'm trying to synchronize some calls in an okhttp authenticator and it works fine, but I'm having troubles testing it. To simplify things I reduced it to such test (in 🧵)- the important part is that
anImportantBlockingCall
resembles
okhttp.Authenticator.uthenticate
- it's a blocking call, which in my case will run a runBlocking coroutine which will synchronise a few things and wait for them. So it really is important that in this test case this remains blocking. 🧵
The test works fine if I wrap
anImportantBlockingCall
into
thread
instead of
launch
and do some
Thread.sleeps
instead of advancing virtual coroutine time. but this is what I'm trying to avoid. concurrentTestScope was me trying to hack it, but it doesn't seem to do it concurrently to the testScope this way 😉 Any hints?
@Test
    fun test() {

        val testScope = TestCoroutineScope()
        val concurrentTestScope = TestCoroutineScope()

        fun anImportantBlockingCall() {
            runBlocking(concurrentTestScope.coroutineContext) {
                delay(100)
                println("C")
            }
        }
        
        testScope.runBlockingTest {
            println("A")
            launch {
                anImportantBlockingCall()
            }
            println("B")
        }

    }