[Tests] Ensure the temporary directories are cleared

This change may prevent OOMs. Context:
https://jetbrains.slack.com/archives/C4U955N6B/p1685000899030279
This commit is contained in:
Nikolay Lunyak
2023-06-19 13:02:05 +03:00
committed by Space Team
parent 039b5fca7a
commit 46ea908daf
3 changed files with 37 additions and 9 deletions
@@ -30,8 +30,8 @@ class TestRunner(private val testConfiguration: TestConfiguration) {
} finally {
try {
testConfiguration.testServices.temporaryDirectoryManager.cleanupTemporaryDirectories()
} catch (_: IOException) {
// ignored
} catch (e: IOException) {
println("Failed to clean temporary directories: ${e.message}\n${e.stackTrace}")
}
beforeDispose(testConfiguration)
Disposer.dispose(testConfiguration.rootDisposable)
@@ -5,22 +5,23 @@
package org.jetbrains.kotlin.test.services.impl
import com.intellij.openapi.util.io.FileUtil
import com.intellij.openapi.util.io.NioFiles
import org.jetbrains.kotlin.test.services.TemporaryDirectoryManager
import org.jetbrains.kotlin.test.services.TestServices
import org.jetbrains.kotlin.test.services.testInfo
import org.jetbrains.kotlin.test.util.KtTestUtil
import java.io.File
import java.nio.file.Paths
import java.util.*
class TemporaryDirectoryManagerImpl(testServices: TestServices) : TemporaryDirectoryManager(testServices) {
private val cache = mutableMapOf<String, File>()
private val rootTempDir: File = run {
private val rootTempDir = lazy {
val testInfo = testServices.testInfo
val className = testInfo.className
val methodName = testInfo.methodName
if (!onWindows && className.length + methodName.length < 255) {
return@run KtTestUtil.tmpDirForTest(className, methodName)
return@lazy KtTestUtil.tmpDirForTest(className, methodName)
}
// This code will simplify directory name for windows. This is needed because there can occur errors due to long name
@@ -32,15 +33,25 @@ class TemporaryDirectoryManagerImpl(testServices: TestServices) : TemporaryDirec
}
override val rootDir: File
get() = rootTempDir
get() = rootTempDir.value
override fun getOrCreateTempDirectory(name: String): File {
return cache.getOrPut(name) { KtTestUtil.tmpDir(rootTempDir, name) }
return cache.getOrPut(name) { KtTestUtil.tmpDir(rootDir, name) }
}
override fun cleanupTemporaryDirectories() {
cache.clear()
FileUtil.delete(rootTempDir)
if (rootTempDir.isInitialized()) {
NioFiles.deleteRecursively(Paths.get(rootDir.path))
}
}
@Suppress("removal")
fun finalize() {
if (rootTempDir.isInitialized() && rootDir.exists()) {
error("The temporary directory $rootDir has not been deleted by the time the corresponding ${this::class.simpleName} is finalized.")
}
}
companion object {