Skip to content

Commit d7c1edd

Browse files
authored
Use Dispatchers.Default on native when no dispatcher is provided (#4075)
Fixes #4074
1 parent cb50ebe commit d7c1edd

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

kotlinx-coroutines-core/common/test/CoroutineScopeTest.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,31 @@ class CoroutineScopeTest : TestBase() {
247247
finish(7)
248248
}
249249

250+
@Test
251+
fun testLaunchContainsDefaultDispatcher() = runTest {
252+
val scopeWithoutDispatcher = CoroutineScope(coroutineContext.minusKey(ContinuationInterceptor))
253+
scopeWithoutDispatcher.launch(Dispatchers.Default) {
254+
assertSame(Dispatchers.Default, coroutineContext[ContinuationInterceptor])
255+
}.join()
256+
scopeWithoutDispatcher.launch {
257+
assertSame(Dispatchers.Default, coroutineContext[ContinuationInterceptor])
258+
}.join()
259+
}
260+
261+
@Test
262+
fun testNewCoroutineContextDispatcher() {
263+
fun newContextDispatcher(c1: CoroutineContext, c2: CoroutineContext) =
264+
ContextScope(c1).newCoroutineContext(c2)[ContinuationInterceptor]
265+
266+
assertSame(Dispatchers.Default, newContextDispatcher(EmptyCoroutineContext, EmptyCoroutineContext))
267+
assertSame(Dispatchers.Default, newContextDispatcher(EmptyCoroutineContext, Dispatchers.Default))
268+
assertSame(Dispatchers.Default, newContextDispatcher(Dispatchers.Default, EmptyCoroutineContext))
269+
assertSame(Dispatchers.Default, newContextDispatcher(Dispatchers.Default, Dispatchers.Default))
270+
assertSame(Dispatchers.Default, newContextDispatcher(Dispatchers.Unconfined, Dispatchers.Default))
271+
assertSame(Dispatchers.Unconfined, newContextDispatcher(Dispatchers.Default, Dispatchers.Unconfined))
272+
assertSame(Dispatchers.Unconfined, newContextDispatcher(Dispatchers.Unconfined, Dispatchers.Unconfined))
273+
}
274+
250275
@Test
251276
fun testScopePlusContext() {
252277
assertSame(EmptyCoroutineContext, scopePlusContext(EmptyCoroutineContext, EmptyCoroutineContext))

kotlinx-coroutines-core/native/src/CoroutineContext.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ internal actual val DefaultDelay: Delay = DefaultExecutor
3131

3232
public actual fun CoroutineScope.newCoroutineContext(context: CoroutineContext): CoroutineContext {
3333
val combined = coroutineContext + context
34-
return if (combined !== DefaultDelay && combined[ContinuationInterceptor] == null)
35-
combined + (DefaultDelay as CoroutineContext.Element) else combined
34+
return if (combined !== Dispatchers.Default && combined[ContinuationInterceptor] == null)
35+
combined + Dispatchers.Default else combined
3636
}
3737

3838
public actual fun CoroutineContext.newCoroutineContext(addedContext: CoroutineContext): CoroutineContext {

0 commit comments

Comments
 (0)