From 04a5c7761c46406bcfdb6269543cdc75be39c052 Mon Sep 17 00:00:00 2001 From: "Alexander.Likhachev" Date: Mon, 30 Jan 2023 21:36:49 +0100 Subject: [PATCH] [IC] Add unit tests for CachesManager closing in transaction #KT-56052 In Progress --- .../incremental/CompilationTransactionTest.kt | 95 ++++++++++++++++++- 1 file changed, 93 insertions(+), 2 deletions(-) diff --git a/build-common/test/org/jetbrains/kotlin/incremental/CompilationTransactionTest.kt b/build-common/test/org/jetbrains/kotlin/incremental/CompilationTransactionTest.kt index 89948cc7c96..cd14695a93d 100644 --- a/build-common/test/org/jetbrains/kotlin/incremental/CompilationTransactionTest.kt +++ b/build-common/test/org/jetbrains/kotlin/incremental/CompilationTransactionTest.kt @@ -10,10 +10,22 @@ import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertFalse import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows import org.junit.jupiter.api.io.TempDir +import java.io.Closeable import java.nio.file.Files import java.nio.file.Path +private class CacheMock(private val throwsException: Boolean = false) : Closeable { + var closed = false + override fun close() { + if (throwsException) { + throw Exception() + } + closed = true + } +} + abstract class BaseCompilationTransactionTest { @TempDir protected lateinit var stashDir: Path @@ -23,14 +35,55 @@ abstract class BaseCompilationTransactionTest { abstract fun createTransaction(): CompilationTransaction - protected fun useTransaction(block: CompilationTransaction.() -> Unit) = createTransaction().also { it.use(block) } + protected fun useTransaction(block: CompilationTransaction.() -> Unit) = createTransaction().also { it.runWithin(body = block) } @Test fun testNoOp() { - useTransaction() { + useTransaction { // do nothing } } + + @Test + fun testCachesClosedOnSuccessfulTransaction() { + val cacheMock = CacheMock() + useTransaction { + cachesManager = cacheMock + markAsSuccessful() + } + assertTrue(cacheMock.closed) + } + + @Test + fun testCachesClosedOnNonSuccessfulTransaction() { + val cacheMock = CacheMock() + useTransaction { + cachesManager = cacheMock + } + assertTrue(cacheMock.closed) + } + + @Test + fun testCachesClosedOnExceptionInsideTransaction() { + val cacheMock = CacheMock() + assertThrows { + useTransaction { + cachesManager = cacheMock + throw Exception() + } + } + assertTrue(cacheMock.closed) + } + + @Test + fun testCachesCloseExceptionIsWrapped() { + val cacheMock = CacheMock(true) + assertThrows { + useTransaction { + cachesManager = cacheMock + } + } + } } class NonRecoverableCompilationTransactionTest : BaseCompilationTransactionTest() { @@ -207,4 +260,42 @@ class RecoverableCompilationTransactionTest : BaseCompilationTransactionTest() { } assertFalse(Files.exists(file)) } + + @Test + fun testChangesAreRevertedOnExecutionException() { + val file1 = workingDir.resolve("1.txt") + val file2 = workingDir.resolve("2.txt") + Files.write(file1, "something".toByteArray()) + assertThrows { + useTransaction { + registerAddedOrChangedFile(file1) + Files.write(file1, "other".toByteArray()) + registerAddedOrChangedFile(file2) + Files.write(file2, "other".toByteArray()) + markAsSuccessful() + throw Exception() + } + } + assertEquals("something", String(Files.readAllBytes(file1))) + assertFalse(Files.exists(file2)) + } + + @Test + fun testChangesAreRevertedOnCachesCloseException() { + val file1 = workingDir.resolve("1.txt") + val file2 = workingDir.resolve("2.txt") + Files.write(file1, "something".toByteArray()) + assertThrows { + useTransaction { + cachesManager = CacheMock(true) + registerAddedOrChangedFile(file1) + Files.write(file1, "other".toByteArray()) + registerAddedOrChangedFile(file2) + Files.write(file2, "other".toByteArray()) + markAsSuccessful() + } + } + assertEquals("something", String(Files.readAllBytes(file1))) + assertFalse(Files.exists(file2)) + } } \ No newline at end of file