涉及 MutableSharedFlow 的测试 - java.lang.IllegalStateException:此作业尚未完成

Testing involving MutableSharedFlow - java.lang.IllegalStateException: This job has not completed yet

抱歉,这可能是一个非常业余的问题。 我正在掌握流程,并且在 MutableSharedFlow 相关的测试方面遇到问题。

以下是我可以构造的重现问题的最简单示例:

@ExperimentalCoroutinesApi
@ExperimentalTime
class MyExampleTest {

    val testDispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher()

    @Test
    fun test() = testDispatcher.runBlockingTest  {
        val sharedFlow = MutableSharedFlow<String>()

        sharedFlow.take(2).collect {
            println(it)
        }

        sharedFlow.tryEmit("Hello")
        sharedFlow.tryEmit("World")
    }
}

这会导致以下错误:

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

    at kotlinx.coroutines.JobSupport.getCompletionExceptionOrNull(JobSupport.kt:1187)
    at kotlinx.coroutines.test.TestBuildersKt.runBlockingTest(TestBuilders.kt:53)
    at kotlinx.coroutines.test.TestBuildersKt.runBlockingTest(TestBuilders.kt:80)
    at com.example.MyExampleTest.test(MyExampleTest.kt:22)

根据我有限的理解,我认为这与 SharedFlow 从未完成这一事实有关。但我认为 take(2) 可以缓解这种情况。如有任何建议,我们将不胜感激!

From my limited understanding I think it's something to do with the fact that SharedFlow never completes.

你是对的,这或多或少是个问题。 Flow是基于Coroutines的,协程还没有完成。

在我看来单元测试 Flows 的最佳方式是 Turbine:

https://github.com/cashapp/turbine

// Cold Flow
flowOf("one", "two").test {
  assertEquals("one", expectItem())
  assertEquals("two", expectItem())
  expectComplete()
}

// Hot Flow
MutableStateFlow("test").test {
    assertThat(expectItem()).isEqualTo("test")
    cancelAndConsumeRemainingEvents()
}

关于这个确切的“问题”还有一个悬而未决的问题:

https://github.com/Kotlin/kotlinx.coroutines/issues/1204

内部测试使用 runBlocking 而不是 runBlockingTest

对于 flow 测试,将其与从范围启动结合起来。例如:

val testDispatcher = TestCoroutineDispatcher()
val testScope = TestCoroutineScope(testDispatcher)

   runBlocking {
        val job1 = testScope.launch {
            doJobThatNeedsCollect(Unit).collect {
                when (it) {
                    is Success -> {
                        isSuccess = true
                        cancel()
                    }
                    is Failure -> {
                        isSuccess = false
                        cancel()
                    }
                }
            }
        }
        while (!job1.isCancelled) {
        }
        assertTrue(isSuccess)
    }