-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FIR IDE: introduce memory leak checking in symbols test
- Loading branch information
1 parent
11e94c1
commit 15277c0
Showing
9 changed files
with
175 additions
and
10 deletions.
There are no files selected for viewing
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
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
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
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
10 changes: 10 additions & 0 deletions
10
...rc/org/jetbrains/kotlin/idea/frontend/api/fir/utils/EntityWasGarbageCollectedException.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,10 @@ | ||
/* | ||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. | ||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. | ||
*/ | ||
|
||
package org.jetbrains.kotlin.idea.frontend.api.fir.utils | ||
|
||
internal class EntityWasGarbageCollectedException(entity: String) : IllegalStateException() { | ||
override val message: String = "$entity was garbage collected while KtAnalysisSession session is still valid" | ||
} |
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
28 changes: 28 additions & 0 deletions
28
idea/idea-frontend-fir/testData/symbolMemoryLeak/symbols.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,28 @@ | ||
fun <R> x(p: R): Int { | ||
|
||
} | ||
|
||
class Y<T> { | ||
fun a() = 1 | ||
} | ||
|
||
var z: Int | ||
get = 10 | ||
set(value) {} | ||
|
||
object Q | ||
|
||
val z: String = "" | ||
|
||
fun yyy() { | ||
// val q = 10 | ||
// fun aaa() {} | ||
// | ||
// class F {} | ||
} | ||
|
||
typealias Str = String | ||
|
||
interface I { | ||
fun str(): String | ||
} |
73 changes: 73 additions & 0 deletions
73
...r/tests/org/jetbrains/kotlin/idea/frontend/api/symbols/AbstractMemoryLeakInSymbolsTest.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,73 @@ | ||
/* | ||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. | ||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. | ||
*/ | ||
|
||
package org.jetbrains.kotlin.idea.frontend.api.symbols | ||
|
||
import com.intellij.openapi.components.service | ||
import com.intellij.openapi.util.io.FileUtil | ||
import junit.framework.Assert | ||
import org.jetbrains.kotlin.idea.executeOnPooledThreadInReadAction | ||
import org.jetbrains.kotlin.idea.fir.low.level.api.trackers.KotlinFirOutOfBlockModificationTrackerFactory | ||
import org.jetbrains.kotlin.idea.frontend.api.InvalidWayOfUsingAnalysisSession | ||
import org.jetbrains.kotlin.idea.frontend.api.KtAnalysisSessionProvider | ||
import org.jetbrains.kotlin.idea.frontend.api.analyze | ||
import org.jetbrains.kotlin.idea.frontend.api.fir.KtFirAnalysisSessionProvider | ||
import org.jetbrains.kotlin.idea.frontend.api.fir.symbols.KtFirSymbol | ||
import org.jetbrains.kotlin.idea.frontend.api.fir.utils.EntityWasGarbageCollectedException | ||
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase | ||
import org.jetbrains.kotlin.psi.KtDeclaration | ||
import org.jetbrains.kotlin.psi.KtFile | ||
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType | ||
import java.io.File | ||
|
||
abstract class AbstractMemoryLeakInSymbolsTest : KotlinLightCodeInsightFixtureTestCase() { | ||
override fun isFirPlugin() = true | ||
|
||
protected fun doTest(path: String) { | ||
val testDataFile = File(path) | ||
val ktFile = myFixture.configureByText(testDataFile.name, FileUtil.loadFile(testDataFile)) as KtFile | ||
val symbols = executeOnPooledThreadInReadAction { | ||
analyze(ktFile) { | ||
ktFile.collectDescendantsOfType<KtDeclaration>().map { it.getSymbol() } | ||
} | ||
} | ||
|
||
invalidateAllCaches(ktFile) | ||
System.gc() | ||
|
||
val leakedSymbols = executeOnPooledThreadInReadAction { | ||
symbols.map { it.hasNoFirElementLeak() }.filterIsInstance<LeakCheckResult.Leak>() | ||
} | ||
if (leakedSymbols.isNotEmpty()) { | ||
Assert.fail( | ||
"""The following symbols leaked (${leakedSymbols.size}/${symbols.size}) | ||
${leakedSymbols.joinToString(separator = "\n") { it.symbol }} | ||
""".trimIndent() | ||
) | ||
} | ||
} | ||
|
||
@OptIn(InvalidWayOfUsingAnalysisSession::class) | ||
private fun invalidateAllCaches(ktFile: KtFile) { | ||
project.service<KotlinFirOutOfBlockModificationTrackerFactory>().incrementModificationsCount() | ||
(project.service<KtAnalysisSessionProvider>() as KtFirAnalysisSessionProvider).clearCaches() | ||
executeOnPooledThreadInReadAction { analyze(ktFile) {} } | ||
} | ||
|
||
private fun KtSymbol.hasNoFirElementLeak(): LeakCheckResult { | ||
require(this is KtFirSymbol<*>) | ||
return try { | ||
firRef.withFir { LeakCheckResult.Leak(this::class.simpleName!!) } | ||
} catch (_: EntityWasGarbageCollectedException) { | ||
LeakCheckResult.NoLeak | ||
} | ||
} | ||
|
||
private sealed class LeakCheckResult { | ||
object NoLeak : LeakCheckResult() | ||
data class Leak(val symbol: String) : LeakCheckResult() | ||
} | ||
} | ||
|
35 changes: 35 additions & 0 deletions
35
...ests/org/jetbrains/kotlin/idea/frontend/api/symbols/MemoryLeakInSymbolsTestGenerated.java
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.