Jelle Fresen [G]
08/09/2024, 4:00 PMJelle Fresen [G]
08/09/2024, 4:00 PMval ctx1: CoroutineContext = StandardTestDispatcher()
val ctx2: CoroutineContext = MyContinuationInterceptor()
val ctx3 = ctx1 + ctx2
assert(ctx3[ContinuationInterceptor] == ctx2)
Jelle Fresen [G]
08/09/2024, 4:01 PMContinuationInterceptor
to a context that has a TestDispatcher
would replace that dispatcher, because a dispatcher is a ContinuationInterceptorross_a
08/09/2024, 4:24 PMross_a
08/09/2024, 4:24 PM@Test
fun thing() {
val ctx1: CoroutineContext = StandardTestDispatcher()
val ctx2: CoroutineContext = object : ContinuationInterceptor {
override fun <T> interceptContinuation(continuation: Continuation<T>): Continuation<T> {
TODO("Not yet implemented")
}
override val key: CoroutineContext.Key<*>
get() = ContinuationInterceptor
}
val ctx3 = ctx1 + ctx2
assert(ctx3[ContinuationInterceptor] == ctx2)
}
ross_a
08/09/2024, 4:26 PMContinuationInterceptor.Key
is different to CoroutineDispatcher.Key
Jelle Fresen [G]
08/09/2024, 4:29 PMJelle Fresen [G]
08/09/2024, 4:33 PMJelle Fresen [G]
08/09/2024, 4:34 PMAbstractCoroutineContextElement
, but that might be the problem?ross_a
08/09/2024, 7:02 PMoverride val key: CoroutineContext.Key<*>
get() = ContinuationInterceptor.Key`
or, since it's a delegate anyway I think you could do
class DelegatingContinuationInterceptor(private val delegate: ContinuationInterceptor) :
AbstractCoroutineContextElement(ContinuationInterceptor), ContinuationInterceptor by delegate
and don't override the key propertyross_a
08/09/2024, 7:03 PMross_a
08/09/2024, 7:36 PMfun main() {
val ctx1: CoroutineContext = kotlinx.coroutines.Dispatchers.Default
val ctx2 = DelegatingContinuationInterceptor(ctx1[ContinuationInterceptor]!!)
val ctx3 = ctx1 + ctx2
assert(ctx3.minusKey(kotlinx.coroutines.CoroutineDispatcher)[ContinuationInterceptor] == ctx2)
}
ross_a
08/09/2024, 7:38 PMval ctx2 = DelegatingContinuationInterceptor(ctx1[ContinuationInterceptor]!!)
val ctx3 = ctx1.minusKey(ContinuationInterceptor) + ctx2
which I'm sure you could wrap in a nice extension method