想象以下独立的测试用例
@Test
fun `stateFlow in GlobalScope`() = runBlockingTest {
suspend fun makeHeavyRequest(): String {
return "heavy result"
}
val flow1 = flowOf(Unit)
.map { makeHeavyRequest() }
.onEach { logThread("1: before flowOn") }
.flowOn(testDispatcher)
.stateIn(GlobalScope, SharingStarted.Lazily, "init state")
val flow2 = flowOf(Unit)
.map { makeHeavyRequest() }
.onEach { logThread("2: before flowOn") }
.flowOn(testDispatcher)
.stateIn(GlobalScope, SharingStarted.Lazily, "init state")
.onEach { logThread("2: after stateIn") }
val flow3 = flowOf(Unit)
.map { makeHeavyRequest() }
.onEach { logThread("3: before flowOn") }
.flowOn(testDispatcher)
.onEach { logThread("3: after flowOn") }
.stateIn(GlobalScope, SharingStarted.Lazily, "init state")
flow1.test {
assertEquals("heavy result", expectItem())
cancelAndIgnoreRemainingEvents()
}
flow2.test {
assertEquals("heavy result", expectItem())
cancelAndIgnoreRemainingEvents()
}
flow3.test {
assertEquals("heavy result", expectItem())
cancelAndIgnoreRemainingEvents()
}
}
运行它的效果将是:
Thread (1: before flowOn): Thread[main @coroutine#2,5,main]
Thread (2: before flowOn): Thread[main @coroutine#3,5,main]
Thread (2: after stateIn): Thread[main @coroutine#6,5,main]
Thread (3: before flowOn): Thread[DefaultDispatcher-worker-1 @coroutine#8,5,main]
Thread (3: after flowOn): Thread[DefaultDispatcher-worker-1 @coroutine#4,5,main]
org.opentest4j.AssertionFailedError:
Expected :heavy result
Actual :init state
在flow3
放置onEach
之间flowOn
并stateIn
完全更改调度程序并弄乱结果。这是为什么?
发生这种情况的原因是因为stateIn
操作员根据上游流量是否为a进行了一些优化ChannelFlow
。
.flowOn(...)
返回ChannelFlow
时.onEach(...)
没有。
通常这并不重要。在您的情况下为什么如此重要,是因为您希望返回的流stateIn
永远不会发出初始值。但是有一个理由说明此参数是强制性的,您应该期望收到初始值。是否实际执行操作主要取决于上游流是否能够发出一个值而不会暂停。
现在看来,stateIn
操作员中的一项优化是,它可能消耗ChannelFlow而不会暂停。这就是为什么您使用时会得到预期的行为
.flowOn(testDispatcher) /*returns ChannelFlow*/
.stateIn(GlobalScope, SharingStarted.Lazily, "init state")
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句