Skip to content

Commit

Permalink
Fixed CoroutineScope.plus operator
Browse files Browse the repository at this point in the history
Fixes #559
  • Loading branch information
r4zzz4k authored and elizarov committed Sep 14, 2018
1 parent d1be1c9 commit 76bfdc8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Expand Up @@ -83,7 +83,7 @@ public interface CoroutineScope {
* This is a shorthand for `CoroutineScope(thisScope + context)`.
*/
public operator fun CoroutineScope.plus(context: CoroutineContext): CoroutineScope =
CoroutineScope(context + context)
CoroutineScope(coroutineContext + context)

/**
* Returns `true` when current [Job] is still active (has not completed and was not cancelled yet).
Expand Down
15 changes: 15 additions & 0 deletions common/kotlinx-coroutines-core-common/test/CoroutineScopeTest.kt
Expand Up @@ -4,6 +4,7 @@

package kotlinx.coroutines.experimental

import kotlin.coroutines.experimental.*
import kotlin.test.*

class CoroutineScopeTest : TestBase() {
Expand Down Expand Up @@ -192,4 +193,18 @@ class CoroutineScopeTest : TestBase() {
finish(4)
}
}

@Test
fun testScopePlusContext() {
assertSame(EmptyCoroutineContext, scopePlusContext(EmptyCoroutineContext, EmptyCoroutineContext))
assertSame(Dispatchers.Default, scopePlusContext(EmptyCoroutineContext, Dispatchers.Default))
assertSame(Dispatchers.Default, scopePlusContext(Dispatchers.Default, EmptyCoroutineContext))
assertSame(Dispatchers.Default, scopePlusContext(Dispatchers.Default, Dispatchers.Default))
assertSame(Dispatchers.Default, scopePlusContext(Dispatchers.Unconfined, Dispatchers.Default))
assertSame(Dispatchers.Unconfined, scopePlusContext(Dispatchers.Default, Dispatchers.Unconfined))
assertSame(Dispatchers.Unconfined, scopePlusContext(Dispatchers.Unconfined, Dispatchers.Unconfined))
}

private fun scopePlusContext(c1: CoroutineContext, c2: CoroutineContext) =
(CoroutineScope(c1) + c2).coroutineContext
}

0 comments on commit 76bfdc8

Please sign in to comment.