From 44c925f17a563a282bf6c63681f145ce3062e4df Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Tue, 1 Dec 2020 16:08:08 +0300 Subject: [PATCH] Fix Daemon compiler tests on Windows In 202 platform tearDown tries to remove temporary directory, but this fails on Windows, because while Daemon is active directory can't be deleted. Original commit: 2ffedd273112830d8863d4d60c125b8aa45bc539 --- .../build/KotlinJpsBuildTestIncremental.kt | 25 +++----------- .../jps/build/SimpleKotlinJpsBuildTest.kt | 16 ++++----- .../kotlin/jps/build/testingUtils.kt | 34 +++++++++++++++++++ .../compilerRunner/JpsKotlinCompilerRunner.kt | 10 ++++++ 4 files changed, 55 insertions(+), 30 deletions(-) diff --git a/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/KotlinJpsBuildTestIncremental.kt b/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/KotlinJpsBuildTestIncremental.kt index dba6e9a38d1..7ce1a6b3bd8 100644 --- a/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/KotlinJpsBuildTestIncremental.kt +++ b/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/KotlinJpsBuildTestIncremental.kt @@ -16,23 +16,16 @@ package org.jetbrains.kotlin.jps.build -import com.intellij.openapi.util.io.FileUtil -import org.jetbrains.jps.builders.CompileScopeTestBuilder -import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments import org.jetbrains.jps.builders.JpsBuildTestCase -import org.jetbrains.jps.builders.logging.BuildLoggingManager +import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments import org.jetbrains.kotlin.compilerRunner.JpsKotlinCompilerRunner import org.jetbrains.kotlin.config.LanguageVersion -import kotlin.reflect.KMutableProperty1 -import org.jetbrains.kotlin.daemon.common.COMPILE_DAEMON_CUSTOM_RUN_FILES_PATH_FOR_TESTS -import org.jetbrains.kotlin.daemon.common.COMPILE_DAEMON_ENABLED_PROPERTY import org.jetbrains.kotlin.daemon.common.isDaemonEnabled -import org.jetbrains.kotlin.incremental.LookupSymbol import org.jetbrains.kotlin.jps.build.fixtures.EnableICFixture import org.jetbrains.kotlin.jps.model.kotlinCommonCompilerArguments import org.jetbrains.kotlin.jps.model.kotlinCompilerArguments -import org.junit.Assert import java.io.File +import kotlin.reflect.KMutableProperty1 class KotlinJpsBuildTestIncremental : KotlinJpsBuildTest() { private val enableICFixture = EnableICFixture() @@ -77,19 +70,11 @@ class KotlinJpsBuildTestIncremental : KotlinJpsBuildTest() { assertCompiled(KotlinBuilder.KOTLIN_BUILDER_NAME, "src/main.kt", "src/Foo.kt") } - val daemonHome = FileUtil.createTempDirectory("daemon-home", "testJpsDaemonIC") - try { - withSystemProperty(COMPILE_DAEMON_CUSTOM_RUN_FILES_PATH_FOR_TESTS, daemonHome.absolutePath) { - withSystemProperty(COMPILE_DAEMON_ENABLED_PROPERTY, "true") { - withSystemProperty(JpsKotlinCompilerRunner.FAIL_ON_FALLBACK_PROPERTY, "true") { - testImpl() - } - } + withDaemon { + withSystemProperty(JpsKotlinCompilerRunner.FAIL_ON_FALLBACK_PROPERTY, "true") { + testImpl() } } - finally { - daemonHome.deleteRecursively() - } } fun testManyFiles() { diff --git a/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/SimpleKotlinJpsBuildTest.kt b/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/SimpleKotlinJpsBuildTest.kt index 8c915775fa6..7e98717ba8b 100644 --- a/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/SimpleKotlinJpsBuildTest.kt +++ b/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/SimpleKotlinJpsBuildTest.kt @@ -16,11 +16,11 @@ package org.jetbrains.kotlin.jps.build -import com.intellij.openapi.util.io.FileUtil import com.intellij.util.PathUtil import org.jetbrains.jps.model.java.JpsJavaExtensionService import org.jetbrains.kotlin.compilerRunner.JpsKotlinCompilerRunner -import org.jetbrains.kotlin.daemon.common.* +import org.jetbrains.kotlin.daemon.common.COMPILE_DAEMON_VERBOSE_REPORT_PROPERTY +import org.jetbrains.kotlin.daemon.common.OSKind import org.jetbrains.kotlin.test.KotlinTestUtils import java.io.File @@ -68,14 +68,10 @@ class SimpleKotlinJpsBuildTest : AbstractKotlinJpsBuildTestCase() { // TODO: add JS tests fun testDaemon() { - val daemonHome = FileUtil.createTempDirectory("daemon-home", "testJpsDaemonIC") - - withSystemProperty(COMPILE_DAEMON_CUSTOM_RUN_FILES_PATH_FOR_TESTS, daemonHome.absolutePath) { - withSystemProperty(COMPILE_DAEMON_ENABLED_PROPERTY, "true") { - withSystemProperty(COMPILE_DAEMON_VERBOSE_REPORT_PROPERTY, "true") { - withSystemProperty(JpsKotlinCompilerRunner.FAIL_ON_FALLBACK_PROPERTY, "true") { - testLoadingKotlinFromDifferentModules() - } + withDaemon { + withSystemProperty(COMPILE_DAEMON_VERBOSE_REPORT_PROPERTY, "true") { + withSystemProperty(JpsKotlinCompilerRunner.FAIL_ON_FALLBACK_PROPERTY, "true") { + testLoadingKotlinFromDifferentModules() } } } diff --git a/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/testingUtils.kt b/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/testingUtils.kt index a5b77e06594..6b2feb62b5d 100644 --- a/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/testingUtils.kt +++ b/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/testingUtils.kt @@ -16,6 +16,11 @@ package org.jetbrains.kotlin.jps.build +import com.intellij.openapi.util.io.FileUtil +import org.jetbrains.kotlin.compilerRunner.JpsKotlinCompilerRunner +import org.jetbrains.kotlin.daemon.common.COMPILE_DAEMON_CUSTOM_RUN_FILES_PATH_FOR_TESTS +import org.jetbrains.kotlin.daemon.common.COMPILE_DAEMON_ENABLED_PROPERTY + inline fun withSystemProperty(property: String, newValue: String?, fn: ()->Unit) { val backup = System.getProperty(property) setOrClearSysProperty(property, newValue) @@ -37,4 +42,33 @@ inline fun setOrClearSysProperty(property: String, newValue: String?) { else { System.clearProperty(property) } +} + +fun withDaemon(fn: () -> Unit) { + val daemonHome = FileUtil.createTempDirectory("daemon-home", "testJpsDaemonIC") + + withSystemProperty(COMPILE_DAEMON_CUSTOM_RUN_FILES_PATH_FOR_TESTS, daemonHome.absolutePath) { + withSystemProperty(COMPILE_DAEMON_ENABLED_PROPERTY, "true") { + try { + fn() + } finally { + JpsKotlinCompilerRunner.shutdownDaemon() + + // Try to force directory deletion to prevent test failure later in tearDown(). + // Working Daemon can prevent folder deletion on Windows, because Daemon shutdown + // is asynchronous. + var attempts = 0 + daemonHome.deleteRecursively() + while (daemonHome.exists() && attempts < 100) { + daemonHome.deleteRecursively() + attempts++ + Thread.sleep(50) + } + + if (daemonHome.exists()) { + error("Couldn't delete Daemon home directory") + } + } + } + } } \ No newline at end of file diff --git a/jps/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/JpsKotlinCompilerRunner.kt b/jps/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/JpsKotlinCompilerRunner.kt index 5088cfe0fc3..e2d6d835f94 100644 --- a/jps/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/JpsKotlinCompilerRunner.kt +++ b/jps/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/JpsKotlinCompilerRunner.kt @@ -54,6 +54,16 @@ class JpsKotlinCompilerRunner { private var _jpsCompileServiceSession: CompileServiceSession? = null @TestOnly + fun shutdownDaemon() { + _jpsCompileServiceSession?.let { + try { + it.compileService.shutdown() + } catch (_: Throwable) { + } + } + _jpsCompileServiceSession = null + } + fun releaseCompileServiceSession() { _jpsCompileServiceSession?.let { try {