How does one get a manually created `CoroutineScop...
# coroutines
r
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)
Copy code
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)
    }
j
@Ridwan Farouki Try this
val differentScope = CoroutineScope(Job() + coroutineContext[ContinuationInterceptor]!!)
I think the reason ^ works is that the test dispatcher has the
delay
implementation needed for virtual time.
🙌 1
r
Thank you @julian, it does indeed work!
😄 1
j
Also, the reason
CoroutineScope(Job() + coroutineContext)
cancels the
TestScope
job is that the rightmost context element wins when adding. Since
coroutineContext
is rightmost and contains a job, that job overwrites the one on the left of the
+
. This, though, would do what you want:
CoroutineScope(coroutineContext + Job())
, and then later,
differentScope.cancel()
.
r
I see, that makes sense, thanks for explaining. The important (and, IMO, rather esoteric) note that the
+
operator is not commutative caught me off guard.
👍 1
d
Another option is
CoroutineScope(StandardTestDispatcher(testScheduler) + Job())
(here, the order doesn't matter), or even just
CoroutineScope(StandardTestDispatcher(testScheduler))
because
CoroutineScope
adds a
Job
if none is specifie. This creates a new dispatcher that is linked to the test framework.
👍 2