Test background threads are stopped

This commit is contained in:
Nikolay Krasko
2015-04-11 00:38:26 +03:00
parent e304ee0a29
commit 1c4335ec25
4 changed files with 51 additions and 3 deletions
@@ -25,6 +25,8 @@ import java.util.concurrent.ScheduledExecutorService
class FinishBuildListener(var pluginClassLoader: ParentLastURLClassLoader?) : BuildAdapter() {
val log = Logging.getLogger(this.javaClass)
private var threadTracker: ThreadTracker? = ThreadTracker()
override fun buildFinished(result: BuildResult?) {
log.debug("Build finished listener")
@@ -38,6 +40,9 @@ class FinishBuildListener(var pluginClassLoader: ParentLastURLClassLoader?) : Bu
pluginClassLoader = null
result?.getGradle()?.removeListener(this)
threadTracker?.checkThreadLeak(result?.getGradle())
threadTracker = null
}
public fun removeThreadLocals() {
@@ -0,0 +1,42 @@
package org.jetbrains.kotlin.gradle.plugin
import org.gradle.api.Project
import org.gradle.api.invocation.Gradle
import org.gradle.api.logging.Logging
import org.junit.Assert
import java.util.HashSet
public class ThreadTracker {
val log = Logging.getLogger(this.javaClass)
private var before: Collection<Thread>? = getThreads()
private fun getThreads(): Collection<Thread> = Thread.getAllStackTraces().keySet()
public fun checkThreadLeak(gradle: Gradle?) {
try {
val testThreads = gradle != null &&
gradle.getRootProject().hasProperty("kotlin.gradle.test") &&
!gradle.getRootProject().hasProperty("kotlin.gradle.noThreadTest")
Thread.sleep(if (testThreads) 200L else 50L)
val after = HashSet(getThreads())
after.removeAll(before!!)
for (thread in after) {
if (thread == Thread.currentThread()) continue
val name = thread.getName()
if (testThreads) {
throw RuntimeException("Thread leaked: $thread: $name\n ${thread.getStackTrace().joinToString(separator = "\n", prefix = " at ")}")
}
else {
log.info("Thread leaked: $thread: $name")
}
}
} finally {
before = null
}
}
}
@@ -37,7 +37,7 @@ abstract class BaseGradleIT {
copyDirRecursively(File(resourcesRootFile, "GradleWrapper-$wrapperVersion"), projectDir)
val cmd = createCommand(tasks)
println("<=== Test build: ${this.projectName} $cmd ====>")
println("<=== Test build: ${this.projectName} $cmd ===>")
val process = createProcess(cmd, projectDir)
@@ -96,7 +96,8 @@ abstract class BaseGradleIT {
private fun Project.createCommand(params: Array<out String>): List<String> {
val pathToKotlinPlugin = "-PpathToKotlinPlugin=" + File("local-repo").getAbsolutePath()
val tailParameters = params + listOf(pathToKotlinPlugin, "--no-daemon", "--${minLogLevel.name().toLowerCase()}")
val tailParameters = params +
listOf(pathToKotlinPlugin, "--no-daemon", "--${minLogLevel.name().toLowerCase()}", "-Pkotlin.gradle.test=true")
return if (isWindows())
listOf("cmd", "/C", "gradlew.bat") + tailParameters
@@ -8,7 +8,7 @@ class Kotlin2JsGradlePluginIT : BaseGradleIT() {
Test fun testBuildAndClean() {
val project = Project("kotlin2JsProject", "1.6")
project.build("build") {
project.build("build", "-Pkotlin.gradle.noThreadTest=true") {
assertSuccessful()
assertReportExists()