Skip to content

Commit

Permalink
Convert cache tests to JUnit 5
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Chelombitko committed Dec 17, 2024
1 parent bfdfcb0 commit a8e0080
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 268 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.malinskiy.marathon.cache.gradle

import com.malinskiy.marathon.cache.SimpleCacheKey
import com.malinskiy.marathon.cache.SimpleEntryReader
import com.malinskiy.marathon.cache.SimpleEntryWriter
import com.malinskiy.marathon.cache.config.RemoteCacheConfiguration
import kotlinx.coroutines.test.runTest
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.AutoClose
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

class GradleHttpCacheServiceTest {
@AutoClose
private val container = GradleCacheContainer()

@AutoClose
private lateinit var cacheService: GradleHttpCacheService

@BeforeEach
fun setUp() {
container.start()
cacheService = GradleHttpCacheService(RemoteCacheConfiguration.Enabled(container.cacheUrl))
}

@Test
fun `GIVEN empty cache WHEN loading an entry from cache THEN returns false`() = runTest {
val cacheKey = SimpleCacheKey("this_key_does_not_exists")

val reader = SimpleEntryReader()
val result = cacheService.load(cacheKey, reader)

assertFalse(result)
assertFalse(reader.readInvoked)
}

@Test
fun `GIVEN cache with an entry WHEN loading the entry from cache THEN returns the original data`() = runTest {
val cacheKey = SimpleCacheKey("test")
cacheService.store(cacheKey, SimpleEntryWriter("qwerty"))

val reader = SimpleEntryReader()
val result = cacheService.load(cacheKey, reader)

assertTrue(result)
assertTrue(reader.readInvoked)
assertEquals("qwerty", reader.data)
}
}
Original file line number Diff line number Diff line change
@@ -1,99 +1,102 @@
package com.malinskiy.marathon.scenario

import com.google.gson.JsonObject
import com.google.gson.JsonParser
import com.malinskiy.marathon.cache.config.RemoteCacheConfiguration
import com.malinskiy.marathon.cache.gradle.GradleCacheContainer
import com.malinskiy.marathon.device.DeviceProvider
import com.malinskiy.marathon.execution.CacheConfiguration
import com.malinskiy.marathon.execution.TestStatus
import com.malinskiy.marathon.test.StubDevice
import com.malinskiy.marathon.test.Test
import com.malinskiy.marathon.test.TestComponentInfo
import com.malinskiy.marathon.test.runAsync
import com.malinskiy.marathon.test.setupMarathon
import com.malinskiy.marathon.test.toTestName
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runTest
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.TestBody
import org.jetbrains.spek.api.dsl.given
import org.jetbrains.spek.api.dsl.it
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.AutoClose
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import org.koin.core.context.stopKoin
import java.io.File
import com.malinskiy.marathon.test.Test as MarathonTest

class CacheScenarios : Spek({
val container = GradleCacheContainer()
class CacheScenarios {
@AutoClose
private val container = GradleCacheContainer()

beforeGroup {
@TempDir
private lateinit var tempDir: File

@BeforeEach
fun setUp() {
container.start()
}

afterGroup {
container.stop()
}
@Test
fun `GIVEN cache is enabled and empty WHEN running tests first time THEN tests gets executed`() = runTest {
val test = createTest()
val outputDir = tempDir.resolve("build-1")

given("cache is enabled") {
group("the first execution of the test") {
it("should execute the test") {
val outputDir = runMarathonWithOneTest(
test = Test("test", "ExampleTest", "test", emptySet(), TestComponentInfo()),
cacheConfig = CacheConfiguration(remote = RemoteCacheConfiguration.Enabled(url = container.cacheUrl))
)

val isFromCache = File(outputDir.absolutePath + "/test_result/omni/serial-1", "test.ExampleTest#test.json")
.jsonObject
.get("isFromCache")
.asBoolean
assertFalse(isFromCache)
}
}
runMarathonWithOneTest(
cacheConfig = CacheConfiguration(remote = RemoteCacheConfiguration.Enabled(url = container.cacheUrl)),
outputDir = outputDir,
test = test
)

group("the second execution of the test") {
it("should restored the test from cache") {
runMarathonWithOneTest(
test = Test("test", "SimpleTest", "test", emptySet(), TestComponentInfo()),
cacheConfig = CacheConfiguration(remote = RemoteCacheConfiguration.Enabled(url = container.cacheUrl))
)
val secondRunDir = runMarathonWithOneTest(
test = Test("test", "SimpleTest", "test", emptySet(), TestComponentInfo()),
cacheConfig = CacheConfiguration(remote = RemoteCacheConfiguration.Enabled(url = container.cacheUrl))
)

val isFromCache = File(secondRunDir.absolutePath + "/test_result/omni/serial-1", "test.SimpleTest#test.json")
.jsonObject
.get("isFromCache")
.asBoolean
assertTrue(isFromCache)
}
}
val isFromCache = isFromCache(outputDir, test)
assertFalse(isFromCache)
}
})

private val File.jsonObject: JsonObject
get() = JsonParser.parseReader(reader()).asJsonObject
@Test
fun `GIVEN cache is enabled WHEN running tests second time THEN test results get taken from cache`() = runTest {
val test = createTest()
val cacheConfiguration = CacheConfiguration(remote = RemoteCacheConfiguration.Enabled(url = container.cacheUrl))

val build1OutputDir = tempDir.resolve("build-1")
runMarathonWithOneTest(cacheConfiguration, build1OutputDir, test)

private fun TestBody.runMarathonWithOneTest(
test: Test,
cacheConfig: CacheConfiguration
): File {
lateinit var output: File
val build2OutputDir = tempDir.resolve("build-2")
runMarathonWithOneTest(cacheConfiguration, build2OutputDir, test)

runTest {
val isFromCache = isFromCache(build2OutputDir, test)
assertTrue(isFromCache)
}

private fun createTest(): MarathonTest =
MarathonTest(
pkg = "test",
clazz = "SimpleTest",
method = "test",
metaProperties = emptySet(),
componentInfo = TestComponentInfo()
)

private fun isFromCache(outputDir: File, test: MarathonTest): Boolean {
val testResultJson = outputDir.resolve("test_result/omni/serial-1/${test.toTestName()}.json")
return testResultJson.reader().use { JsonParser.parseReader(it).asJsonObject.get("isFromCache").asBoolean }
}

private suspend fun TestScope.runMarathonWithOneTest(
cacheConfig: CacheConfiguration,
outputDir: File,
test: MarathonTest
) {
val marathon = setupMarathon {
val device = StubDevice()

configuration {
output = outputDir
this.outputDir = outputDir

tests {
listOf(test)
}

cache = cacheConfig

vendorConfiguration.deviceProvider.coroutineScope = this@runTest
vendorConfiguration.deviceProvider.coroutineScope = this@runMarathonWithOneTest

devices {
delay(1000)
Expand All @@ -107,9 +110,6 @@ private fun TestBody.runMarathonWithOneTest(
}

marathon.runAsync()
stopKoin()
}

stopKoin()

return output
}
Loading

0 comments on commit a8e0080

Please sign in to comment.