Migrate Gradle daemon memory leak tests to JUnit5.

^KT-45745 In Progress
This commit is contained in:
Yahor Berdnikau
2021-07-26 15:13:11 +02:00
committed by teamcityserver
parent be0a0a7784
commit a55eacd8db
@@ -5,48 +5,57 @@
package org.jetbrains.kotlin.gradle package org.jetbrains.kotlin.gradle
import org.junit.Test import org.gradle.api.logging.LogLevel
import org.gradle.tooling.internal.consumer.ConnectorServices
import org.gradle.util.GradleVersion
import org.jetbrains.kotlin.gradle.testbase.*
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.DisplayName
import kotlin.test.assertTrue import kotlin.test.assertTrue
class GradleDaemonMemoryIT : BaseGradleIT() { @DisplayName("Gradle daemon memory leak")
@DaemonsGradlePluginTests
class GradleDaemonMemoryIT : KGPBaseTest() {
override val defaultBuildOptions: BuildOptions
get() = super.defaultBuildOptions.copy(logLevel = LogLevel.DEBUG)
@AfterEach
fun tearDown() {
// Stops Gradle and Kotlin daemons, so new run will start them again
ConnectorServices.reset()
}
// For corresponding documentation, see https://docs.gradle.org/current/userguide/gradle_daemon.html // For corresponding documentation, see https://docs.gradle.org/current/userguide/gradle_daemon.html
// Setting user.variant to different value implies a new daemon process will be created. // Setting user.variant to different value implies a new daemon process will be created.
// In order to stop daemon process, special exit task is used ( System.exit(0) ). // In order to stop daemon process, special exit task is used ( System.exit(0) ).
@Test @DisplayName("No small memory leak in plugin")
fun testGradleDaemonMemory() { @GradleTest
val project = Project("gradleDaemonMemory") fun testGradleDaemonMemory(gradleVersion: GradleVersion) {
val userVariantArg = "-Duser.variant=ForTest" project("gradleDaemonMemory", gradleVersion) {
val memoryMaxGrowthLimitKB = 5000 val userVariantArg = "-Duser.variant=ForTest"
val buildCount = 10 val memoryMaxGrowthLimitKB = 5000
val reportMemoryUsage = "-Dkotlin.gradle.test.report.memory.usage=true" val buildCount = 10
val options = BuildOptions(withDaemon = true) val reportMemoryUsage = "-Dkotlin.gradle.test.report.memory.usage=true"
val reportRegex = "\\[KOTLIN]\\[PERF] Used memory after build: (\\d+) kb \\(difference since build start: ([+-]?\\d+) kb\\)"
.toRegex()
fun exitTestDaemon() { val usedMemory: List<Int> = (1..buildCount).map {
project.build(userVariantArg, reportMemoryUsage, "exit", options = options) { var reportedMemory = 0
assertFailed() build(userVariantArg, reportMemoryUsage, "clean", "assemble") {
assertContains("The daemon has exited normally or was terminated in response to a user interrupt.") val matches = output
.lineSequence()
.filter { it.contains("[KOTLIN][PERF]") }
.joinToString(separator = "\n")
.run { reportRegex.find(this) }
assertTasksExecuted(":compileKotlin")
assert(matches != null && matches.groups.size == 3) {
printBuildOutput()
"Used memory after build is not reported by plugin"
}
reportedMemory = matches!!.groupValues[1].toInt()
}
reportedMemory
} }
}
fun buildAndGetMemoryAfterBuild(): Int {
var reportedMemory: Int? = null
project.build(userVariantArg, reportMemoryUsage, "clean", "assemble", options = options) {
assertSuccessful()
val matches = "\\[KOTLIN]\\[PERF] Used memory after build: (\\d+) kb \\(difference since build start: ([+-]?\\d+) kb\\)"
.toRegex().find(output)
assertTasksExecuted(":compileKotlin")
assert(matches != null && matches.groups.size == 3) { "Used memory after build is not reported by plugin" }
reportedMemory = matches!!.groupValues[1].toInt()
}
return reportedMemory!!
}
exitTestDaemon()
try {
val usedMemory: List<Int> = (1..buildCount).map { buildAndGetMemoryAfterBuild() }
// ensure that the maximum of the used memory established after several first builds doesn't raise significantly in the subsequent builds // ensure that the maximum of the used memory established after several first builds doesn't raise significantly in the subsequent builds
val establishedMaximum = usedMemory.take(buildCount / 2).maxOrNull()!! val establishedMaximum = usedMemory.take(buildCount / 2).maxOrNull()!!
@@ -61,11 +70,7 @@ class GradleDaemonMemoryIT : BaseGradleIT() {
) )
// testing that nothing remains locked by daemon, see KT-9440 // testing that nothing remains locked by daemon, see KT-9440
project.build(userVariantArg, "clean", options = BuildOptions(withDaemon = true)) { build(userVariantArg, "clean")
assertSuccessful()
}
} finally {
exitTestDaemon()
} }
} }
} }