Coroutine testing fails with "This job has not completed yet"

Robert

I'm following Craig Russell's blog post about testing coroutines: https://craigrussell.io/2019/11/unit-testing-coroutine-suspend-functions-using-testcoroutinedispatcher/ but I can't get this test to pass:

@Test
fun multipleLaunch() = runBlockingTest {
    var result = 0
    val jobs = mutableListOf<Job>()
    for (j in 0 until 10) {
        val job = launch(testDispatcherProvider.io()) {
            delay(1000)
            result++
        }
        jobs.add(job)
    }
    jobs.forEach { job ->
        job.join()
    }
    assertEquals(10, result)
}

Basically I'm launching a bunch of parallel jobs and want to get the result once all of them are finished. I'm getting this, by now classical exception:

java.lang.IllegalStateException: This job has not completed yet

Please advise how to get this to work as intended.

My complete code:

class LaunchTest {
    @get:Rule
    var coroutinesTestRule = CoroutineTestRule()

    val testDispatcherProvider = object : DispatcherProvider {
        override fun io(): CoroutineDispatcher = coroutinesTestRule.testDispatcher
    }

    @Test
    fun multipleLaunch() = runBlockingTest {
        var result = 0
        val jobs = mutableListOf<Job>()
        for (j in 0 until 10) {
            val job = launch(testDispatcherProvider.io()) {
                delay(1000)
                result++
            }
            jobs.add(job)
        }
        jobs.forEach { job ->
            job.join()
        }
        assertEquals(10, result)
    }
}

class CoroutineTestRule constructor(val testDispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher()) : TestWatcher() {
    override fun starting(description: Description?) {
        super.starting(description)
        Dispatchers.setMain(testDispatcher)
    }

    override fun finished(description: Description?) {
        super.finished(description)
        Dispatchers.resetMain()
        testDispatcher.cleanupTestCoroutines()
    }
}
Robert

Solved.

I totally blame Android Studio's auto-completion for this. :)

I simply ran the wrong "runBlockingTest()".

Replace this line:

fun multipleLaunch() = runBlockingTest {

with this line:

fun multipleLaunch() = coroutinesTestRule.testDispatcher.runBlockingTest {

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Testing components with Jest fails

分類Dev

Running mesos-local for testing a framework fails with Permission denied

分類Dev

Tornado coroutine

分類Dev

Expect jest mock to have been called with an argument fails in react testing library

分類Dev

Coroutine is a class in its nature?

分類Dev

How to properly exec() a coroutine

分類Dev

PlayMode Tests with Coroutine SetUp

分類Dev

Recyclerview not updating in coroutine

分類Dev

try/except/finally in Coroutine

分類Dev

Lua segfault on coroutine resume

分類Dev

How to rate limit a coroutine and re-call the coroutine after the limit?

分類Dev

Coroutine vs Fiber difference clarification

分類Dev

Can't stop a Coroutine in Unity

分類Dev

Set a class variable in IEnumerator coroutine

分類Dev

Testing to Jessie

分類Dev

@ asyncio.coroutineとasync def

分類Dev

How to call Kotlin coroutine in composable function callbacks?

分類Dev

How do I kill a task / coroutine in Julia?

分類Dev

Why do I need to await for a coroutine?

分類Dev

Kotlin alternative to Python's coroutine yield and send

分類Dev

how to add a coroutine to running event loop?

分類Dev

boost :: coroutine2 vs CoroutineTS

分類Dev

Lua C API - attaching data to a coroutine

分類Dev

Kotlin coroutine list returning null value

分類Dev

Returning from inner nested coroutine by label Kotlin

分類Dev

Android: Coroutine doesn't always work on call

分類Dev

How would you make a monad instance of this coroutine?

分類Dev

why the coroutine exception handler double the original exception?

分類Dev

Execute coroutine from `call_soon` callback function

Related 関連記事

ホットタグ

アーカイブ