Tgo1014
11/26/2021, 9:18 AMdelay()
s inside a runTest{}
? In my mind the delays would wait until I call advanceTimeBy()
but in practice the test just run and ignore all the delays. What’s the point of the advanceTimeBy()
then?谢朋刚
11/26/2021, 9:44 AMTgo1014
11/26/2021, 10:03 AMlaunch
block, it still skip the delays and print the numbers instantly. That’s why I’m confused with the use of advanceTimeBy()
.
I see that when you advance the time it changes the currentTime
variable, but what uses this? I would imagine delays would depend on it but doesn’t seems like the case.Nick Allen
11/27/2021, 1:11 AMrunTest
does some extra stuff after it runs your lambda. It calls advanceUntilIdle
and checks that all your coroutines actually finish.
delay
does wait, but then it is run during the cleanup phase of runTest
.Nick Allen
11/27/2021, 1:18 AMclass UnderTest(val scope: CoroutineScope) {
var x: Int = 0
fun doSomething() {
scope.launch {
delay(100)
x = 1
delay(100)
x = 2
}
}
}
@Test
fun testFoo() = runTest {
val underTest = UnderTest(this)
underTest.doSomething()
advanceTimeBy(150)
assertEquals(1, underTest.x)
}
ursus
11/27/2021, 1:45 AMNick Allen
11/27/2021, 1:48 AMNick Allen
11/27/2021, 1:53 AMinvokeOnCompletion
listeners.ursus
11/27/2021, 1:54 AMNick Allen
11/27/2021, 1:54 AMNick Allen
11/27/2021, 1:55 AMursus
11/27/2021, 1:56 AMNick Allen
11/27/2021, 1:56 AMNick Allen
11/27/2021, 1:57 AMursus
11/27/2021, 1:59 AMclass Foo(dispatcher: Dispatcher) : Scoped {
private val scope = SupervisorScope(dispatcher)
override fun clear() {
scope.cancel()
}
}
class Foo(private val scope: CoroutineScope)
I'm mostly talking about this. I could see your point but, Scoped interface predates coroutines in my code, and also has a rxjava implementation (CompositeDisposable)
But more over what I fear is that since now scope is injected, its DI responsibility, and there fore I need a singleton CoroutineScope per semantic DI scope, and since scopes are nested I'd need a qualifierursus
11/27/2021, 2:00 AMursus
11/27/2021, 2:01 AMinvokeOnCancelation
or somethingNick Allen
11/27/2021, 2:08 AMursus
11/27/2021, 2:40 AMNick Allen
11/27/2021, 2:44 AMNick Allen
11/27/2021, 2:47 AMursus
11/27/2021, 2:55 AMTgo1014
11/29/2021, 8:47 AMadvanceTimeBy()
correct?Nick Allen
11/29/2021, 8:54 PMJob
allows runTest
to verify that you aren't leaking coroutines using that scope.ursus
11/30/2021, 1:28 AMNick Allen
11/30/2021, 1:54 AMrunTest
installs will not do it job and report uncaught exceptions (JVM on desktop just swallows them iirc)Nick Allen
11/30/2021, 1:57 AMrunBlockingTest
had coroutine leak checking behavior that would be missed if you didn't pass in the Job
, I'm not seeing that in the runTest
source so maybe the ditched it (though can't be sure, not like I scoured the code).ursus
11/30/2021, 2:16 AMNick Allen
11/30/2021, 2:31 AM@JvmInline value class AppScope(private val scope: CoroutineScope) : CoroutineScope by scope
@JvmInline value class UserScope(private val scope: CoroutineScope) : CoroutineScope by scope
Different types is the easiest workaround I can think of to make mix ups less likely.ursus
11/30/2021, 3:05 AM