forked from Kotlin/kotlinx.coroutines
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test invariant that IDEA evaluator depends on (Kotlin#2525)
Fixes Kotlin#2522
- Loading branch information
1 parent
76f157f
commit d66076a
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
kotlinx-coroutines-core/jvm/test/IntellijIdeaDebuggerEvaluatorCompatibilityTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines | ||
|
||
import org.junit.Test | ||
import kotlin.coroutines.* | ||
import kotlin.test.* | ||
|
||
class IntellijIdeaDebuggerEvaluatorCompatibilityTest { | ||
|
||
/* | ||
* This test verifies that our CoroutineScope is accessible to IDEA debugger. | ||
* | ||
* Consider the following scenario: | ||
* ``` | ||
* runBlocking<Unit> { // this: CoroutineScope | ||
* println("runBlocking") | ||
* } | ||
* ``` | ||
* user puts breakpoint to `println` line, opens "Evaluate" window | ||
* and executes `launch { println("launch") }`. They (obviously) expect it to work, but | ||
* it won't: `{}` in `runBlocking` is `SuspendLambda` and `this` is an unused implicit receiver | ||
* that is removed by the compiler (because it's unused). | ||
* | ||
* But we still want to provide consistent user experience for functions with `CoroutineScope` receiver, | ||
* for that IDEA debugger tries to retrieve the scope via `kotlin.coroutines.coroutineContext[Job] as? CoroutineScope` | ||
* and with this test we're fixing this behaviour. | ||
* | ||
* Note that this behaviour is not carved in stone: IDEA fallbacks to `kotlin.coroutines.coroutineContext` for the context if necessary. | ||
*/ | ||
|
||
@Test | ||
fun testScopeIsAccessible() = runBlocking<Unit> { | ||
verify() | ||
|
||
withContext(Job()) { | ||
verify() | ||
} | ||
|
||
coroutineScope { | ||
verify() | ||
} | ||
|
||
supervisorScope { | ||
verify() | ||
} | ||
|
||
} | ||
|
||
private suspend fun verify() { | ||
val ctx = coroutineContext | ||
assertTrue { ctx.job is CoroutineScope } | ||
} | ||
} |