How does one get a manually created
CoroutineScope
to use a `TestScope`'s scheduler in
kotlinx-coroutines-test:1.6.1
for the virtual time behaviour? I would have expected
CoroutineScope(Job() + testScheduler)
to work but it doesn't. (Note that
CoroutineScope(Job() + coroutineContext)
does work but then when I try to cancel
differentScope
it fails the test because it cancels the parent
TestScope
, making it hard to test code that uses structured concurrency)
fun tickTest() = runTest {
val differentScope = CoroutineScope(Job() + testScheduler)
val ticks = mutableListOf<Int>()
val tickJob = differentScope.launch {
var time = 0
while (true) {
delay(1_000)
ticks.add(++time)
}
}
advanceTimeBy(5_001)
tickJob.cancel()
println(ticks)
assert(ticks.size == 5)
}