File tree 2 files changed +36
-1
lines changed
2 files changed +36
-1
lines changed Original file line number Diff line number Diff line change @@ -163,7 +163,7 @@ public suspend fun <T> withContext(
163
163
if (newContext[ContinuationInterceptor ] == oldContext[ContinuationInterceptor ]) {
164
164
val coroutine = UndispatchedCoroutine (newContext, uCont)
165
165
// There are changes in the context, so this thread needs to be updated
166
- withCoroutineContext(newContext , null ) {
166
+ withCoroutineContext(coroutine.context , null ) {
167
167
return @sc coroutine.startUndispatchedOrReturn(coroutine, block)
168
168
}
169
169
}
Original file line number Diff line number Diff line change @@ -156,6 +156,41 @@ class ThreadContextElementTest : TestBase() {
156
156
}
157
157
}
158
158
}
159
+
160
+ class JobCaptor (val capturees : ArrayList <Job > = ArrayList ()) : ThreadContextElement<Unit> {
161
+
162
+ companion object Key : CoroutineContext.Key<MyElement>
163
+
164
+ override val key: CoroutineContext .Key <* > get() = Key
165
+
166
+ override fun updateThreadContext (context : CoroutineContext ) {
167
+ capturees.add(context.job)
168
+ }
169
+
170
+ override fun restoreThreadContext (context : CoroutineContext , oldState : Unit ) {
171
+ }
172
+ }
173
+
174
+ @Test
175
+ fun testWithContextJobAccess () = runTest {
176
+ val captor = JobCaptor ()
177
+ val manuallyCaptured = ArrayList <Job >()
178
+ runBlocking(captor) {
179
+ manuallyCaptured + = coroutineContext.job
180
+ withContext(CoroutineName (" undispatched" )) {
181
+ manuallyCaptured + = coroutineContext.job
182
+ withContext(Dispatchers .IO ) {
183
+ manuallyCaptured + = coroutineContext.job
184
+ }
185
+ // Context restored, captured again
186
+ manuallyCaptured + = coroutineContext.job
187
+ }
188
+ // Context restored, captured again
189
+ manuallyCaptured + = coroutineContext.job
190
+ }
191
+
192
+ assertEquals(manuallyCaptured, captor.capturees)
193
+ }
159
194
}
160
195
161
196
class MyData
You can’t perform that action at this time.
0 commit comments