|
| 1 | +package kotlinx.coroutines |
| 2 | + |
| 3 | +import kotlinx.coroutines.testing.TestBase |
| 4 | +import java.lang.ref.WeakReference |
| 5 | +import kotlin.coroutines.AbstractCoroutineContextElement |
| 6 | +import kotlin.coroutines.Continuation |
| 7 | +import kotlin.coroutines.ContinuationInterceptor |
| 8 | +import kotlin.coroutines.CoroutineContext |
| 9 | +import kotlin.test.Test |
| 10 | +import kotlin.test.assertNull |
| 11 | + |
| 12 | +/* |
| 13 | + * This is an adapted verion of test from #4296. |
| 14 | + * |
| 15 | + * qwwdfsad: the test relies on System.gc() actually collecting the garbage. |
| 16 | + * If these tests flake on CI, first check that JDK/GC setup in not an issue. |
| 17 | + */ |
| 18 | +class ThreadLocalCustomContinuationInterceptorTest : TestBase() { |
| 19 | + |
| 20 | + private class CustomContinuationInterceptor(private val delegate: ContinuationInterceptor) : |
| 21 | + AbstractCoroutineContextElement(ContinuationInterceptor), ContinuationInterceptor { |
| 22 | + |
| 23 | + override fun <T> interceptContinuation(continuation: Continuation<T>): Continuation<T> { |
| 24 | + return delegate.interceptContinuation(continuation) |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + private class CustomNeverEqualContinuationInterceptor(private val delegate: ContinuationInterceptor) : |
| 29 | + AbstractCoroutineContextElement(ContinuationInterceptor), ContinuationInterceptor { |
| 30 | + |
| 31 | + override fun <T> interceptContinuation(continuation: Continuation<T>): Continuation<T> { |
| 32 | + return delegate.interceptContinuation(continuation) |
| 33 | + } |
| 34 | + |
| 35 | + override fun equals(other: Any?) = false |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + fun testDefaultDispatcherNoSuspension() = ensureCoroutineContextGCed(Dispatchers.Default, suspend = false) |
| 40 | + |
| 41 | + @Test |
| 42 | + fun testDefaultDispatcher() = ensureCoroutineContextGCed(Dispatchers.Default, suspend = true) |
| 43 | + |
| 44 | + |
| 45 | + @Test |
| 46 | + fun testNonCoroutineDispatcher() = ensureCoroutineContextGCed( |
| 47 | + CustomContinuationInterceptor(Dispatchers.Default), |
| 48 | + suspend = true |
| 49 | + ) |
| 50 | + |
| 51 | + @Test |
| 52 | + fun testNonCoroutineDispatcherSuspension() = ensureCoroutineContextGCed( |
| 53 | + CustomContinuationInterceptor(Dispatchers.Default), |
| 54 | + suspend = false |
| 55 | + ) |
| 56 | + |
| 57 | + // Note asymmetric equals codepath never goes through the undispatched withContext, thus the separate test case |
| 58 | + |
| 59 | + @Test |
| 60 | + fun testNonCoroutineDispatcherAsymmetricEquals() = |
| 61 | + ensureCoroutineContextGCed( |
| 62 | + CustomNeverEqualContinuationInterceptor(Dispatchers.Default), |
| 63 | + suspend = true |
| 64 | + ) |
| 65 | + |
| 66 | + @Test |
| 67 | + fun testNonCoroutineDispatcherAsymmetricEqualsSuspension() = |
| 68 | + ensureCoroutineContextGCed( |
| 69 | + CustomNeverEqualContinuationInterceptor(Dispatchers.Default), |
| 70 | + suspend = false |
| 71 | + ) |
| 72 | + |
| 73 | + |
| 74 | + private fun ensureCoroutineContextGCed(coroutineContext: CoroutineContext, suspend: Boolean) { |
| 75 | + runTest { |
| 76 | + lateinit var ref: WeakReference<CoroutineName> |
| 77 | + val job = GlobalScope.launch(coroutineContext) { |
| 78 | + val coroutineName = CoroutineName("Yo") |
| 79 | + ref = WeakReference(coroutineName) |
| 80 | + withContext(coroutineName) { |
| 81 | + if (suspend) { |
| 82 | + delay(1) |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + job.join() |
| 87 | + |
| 88 | + // Twice is enough to ensure |
| 89 | + System.gc() |
| 90 | + System.gc() |
| 91 | + assertNull(ref.get()) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments