|
4 | 4 |
|
5 | 5 | package kotlinx.coroutines
|
6 | 6 |
|
| 7 | +import kotlinx.coroutines.sync.* |
| 8 | +import java.util.concurrent.* |
| 9 | +import kotlin.coroutines.* |
| 10 | +import kotlin.coroutines.intrinsics.* |
7 | 11 | import kotlin.test.*
|
8 | 12 |
|
9 | 13 |
|
@@ -63,10 +67,99 @@ class ThreadLocalStressTest : TestBase() {
|
63 | 67 | withContext(threadLocal.asContextElement("foo")) {
|
64 | 68 | yield()
|
65 | 69 | cancel()
|
66 |
| - suspendCancellableCoroutineReusable<Unit> { } |
| 70 | + suspendCancellableCoroutineReusable<Unit> { } |
67 | 71 | }
|
68 | 72 | } finally {
|
69 | 73 | assertEquals(expectedValue, threadLocal.get())
|
70 | 74 | }
|
71 | 75 | }
|
| 76 | + |
| 77 | + /* |
| 78 | + * Another set of tests for undispatcheable continuations that do not require stress test multiplier. |
| 79 | + * Also note that `uncaughtExceptionHandler` is used as the only available mechanism to propagate error from |
| 80 | + * `resumeWith` |
| 81 | + */ |
| 82 | + |
| 83 | + @Test |
| 84 | + fun testNonDispatcheableLeak() { |
| 85 | + repeat(100) { |
| 86 | + doTestWithPreparation( |
| 87 | + ::doTest, |
| 88 | + { threadLocal.set(null) }) { threadLocal.get() != null } |
| 89 | + assertNull(threadLocal.get()) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + fun testNonDispatcheableLeakWithInitial() { |
| 95 | + repeat(100) { |
| 96 | + doTestWithPreparation(::doTest, { threadLocal.set("initial") }) { threadLocal.get() != "initial" } |
| 97 | + assertEquals("initial", threadLocal.get()) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + fun testNonDispatcheableLeakWithContextSwitch() { |
| 103 | + repeat(100) { |
| 104 | + doTestWithPreparation( |
| 105 | + ::doTestWithContextSwitch, |
| 106 | + { threadLocal.set(null) }) { threadLocal.get() != null } |
| 107 | + assertNull(threadLocal.get()) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + fun testNonDispatcheableLeakWithInitialWithContextSwitch() { |
| 113 | + repeat(100) { |
| 114 | + doTestWithPreparation( |
| 115 | + ::doTestWithContextSwitch, |
| 116 | + { threadLocal.set("initial") }) { false /* can randomly wake up on the non-main thread */ } |
| 117 | + // Here we are always on the main thread |
| 118 | + assertEquals("initial", threadLocal.get()) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private fun doTestWithPreparation(testBody: suspend () -> Unit, setup: () -> Unit, isInvalid: () -> Boolean) { |
| 123 | + setup() |
| 124 | + val latch = CountDownLatch(1) |
| 125 | + testBody.startCoroutineUninterceptedOrReturn(Continuation(EmptyCoroutineContext) { |
| 126 | + if (isInvalid()) { |
| 127 | + Thread.currentThread().uncaughtExceptionHandler.uncaughtException( |
| 128 | + Thread.currentThread(), |
| 129 | + IllegalStateException("Unexpected error: thread local was not cleaned") |
| 130 | + ) |
| 131 | + } |
| 132 | + latch.countDown() |
| 133 | + }) |
| 134 | + latch.await() |
| 135 | + } |
| 136 | + |
| 137 | + private suspend fun doTest() { |
| 138 | + withContext(threadLocal.asContextElement("foo")) { |
| 139 | + try { |
| 140 | + coroutineScope { |
| 141 | + val semaphore = Semaphore(1, 1) |
| 142 | + cancel() |
| 143 | + semaphore.acquire() |
| 144 | + } |
| 145 | + } catch (e: CancellationException) { |
| 146 | + // Ignore cancellation |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + private suspend fun doTestWithContextSwitch() { |
| 152 | + withContext(threadLocal.asContextElement("foo")) { |
| 153 | + try { |
| 154 | + coroutineScope { |
| 155 | + val semaphore = Semaphore(1, 1) |
| 156 | + GlobalScope.launch { }.join() |
| 157 | + cancel() |
| 158 | + semaphore.acquire() |
| 159 | + } |
| 160 | + } catch (e: CancellationException) { |
| 161 | + // Ignore cancellation |
| 162 | + } |
| 163 | + } |
| 164 | + } |
72 | 165 | }
|
0 commit comments