Skip to content

Commit

Permalink
Replace runBlocking with runTest in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Chelombitko committed Dec 10, 2024
1 parent 61d4ad5 commit e3ea0e6
Show file tree
Hide file tree
Showing 11 changed files with 267 additions and 319 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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.runBlocking
import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBeEqualTo
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.describe
Expand Down Expand Up @@ -33,7 +33,7 @@ class GradleHttpCacheServiceSpek : Spek({
describe("GradleHttpCacheService") {
group("basics") {
it("load with empty cache - should return false") {
runBlocking {
runTest {
val reader = SimpleEntryReader()
val result = cacheService().load(SimpleCacheKey("this_key_does_not_exists"), reader)

Expand All @@ -43,7 +43,7 @@ class GradleHttpCacheServiceSpek : Spek({
}

it("save to cache and load - should return the same data") {
runBlocking {
runTest {
cacheService().store(SimpleCacheKey("test"), SimpleEntryWriter("qwerty"))
val reader = SimpleEntryReader()
val result = cacheService().load(SimpleCacheKey("test"), reader)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.malinskiy.marathon.test.TestComponentInfo
import com.malinskiy.marathon.test.runAsync
import com.malinskiy.marathon.test.setupMarathon
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBeEqualTo
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.TestBody
Expand Down Expand Up @@ -76,37 +76,39 @@ private val File.jsonObject: JsonObject
private fun TestBody.runMarathonWithOneTest(
test: Test,
cacheConfig: CacheConfiguration
): File = runBlocking {
var output: File? = null
): File {
lateinit var output: File

val marathon = setupMarathon {
val device = StubDevice()
runTest {
val marathon = setupMarathon {
val device = StubDevice()

configuration {
output = outputDir
configuration {
output = outputDir

tests {
listOf(test)
}
tests {
listOf(test)
}

cache = cacheConfig
cache = cacheConfig

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

devices {
delay(1000)
it.send(DeviceProvider.DeviceEvent.DeviceConnected(device))
devices {
delay(1000)
it.send(DeviceProvider.DeviceEvent.DeviceConnected(device))
}
}

device.executionResults = mapOf(
test to arrayOf(TestStatus.PASSED)
)
}

device.executionResults = mapOf(
test to arrayOf(TestStatus.PASSED)
)
marathon.runAsync()
}

marathon.runAsync()

stopKoin()

output!!
return output
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.malinskiy.marathon.io.AttachmentManager
import com.malinskiy.marathon.io.FileType
import com.malinskiy.marathon.test.Test
import com.malinskiy.marathon.test.TestComponentInfo
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.amshove.kluent.mock
import org.amshove.kluent.shouldBeEqualTo
import org.amshove.kluent.shouldNotBeEqualTo
Expand All @@ -34,15 +34,15 @@ class TestResultsCacheSpek : Spek({

describe("TestResultsCache") {
it("should return null when load test with empty cache") {
runBlocking {
runTest {
val result = cache.load(SimpleCacheKey("test"), createTest())

result shouldBeEqualTo null
}
}

it("should return saved test result when load after saving") {
runBlocking {
runTest {
val test = Test(
pkg = "com.test",
clazz = "Test",
Expand Down Expand Up @@ -83,7 +83,7 @@ class TestResultsCacheSpek : Spek({
deleteOnExit()
}

runBlocking {
runTest {
val test = createTest()
val testResult = createTestResult(
attachments = listOf(Attachment(tempFile, AttachmentType.LOG, FileType.LOG))
Expand All @@ -101,12 +101,7 @@ class TestResultsCacheSpek : Spek({
}

it("should return null when exception occurred during reading") {
val tempFile = File.createTempFile("test", "123").apply {
writeText("abc")
deleteOnExit()
}

runBlocking {
runTest {
val testResult = createTestResult()
cache.store(SimpleCacheKey("test"), testResult)
cacheService.throwExceptions()
Expand All @@ -118,7 +113,7 @@ class TestResultsCacheSpek : Spek({
}

it("should not fail when error occurred during writing") {
runBlocking {
runTest {
cacheService.throwExceptions()
val testResult = createTestResult()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.malinskiy.marathon.cache.CacheKey
import com.malinskiy.marathon.device.DevicePoolId
import com.malinskiy.marathon.execution.ComponentInfo
import com.malinskiy.marathon.test.TestComponentInfo
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBeEqualTo
import org.amshove.kluent.shouldNotBeEqualTo
import org.junit.jupiter.api.Test
Expand All @@ -14,140 +14,116 @@ import com.malinskiy.marathon.test.Test as MarathonTest
class TestCacheKeyFactoryTest {

@Test
fun differentCacheKeysForDifferentMarathonVersion() {
runBlocking {
val firstKey = createCacheKey(marathonVersion = "1.0")
val secondKey = createCacheKey(marathonVersion = "1.1")
fun differentCacheKeysForDifferentMarathonVersion() = runTest {
val firstKey = createCacheKey(marathonVersion = "1.0")
val secondKey = createCacheKey(marathonVersion = "1.1")

firstKey shouldNotBeEqualTo secondKey
}
firstKey shouldNotBeEqualTo secondKey
}

@Test
fun sameCacheKeysForSameMarathonVersion() {
runBlocking {
val firstKey = createCacheKey(marathonVersion = "1.0")
val secondKey = createCacheKey(marathonVersion = "1.0")
fun sameCacheKeysForSameMarathonVersion() = runTest {
val firstKey = createCacheKey(marathonVersion = "1.0")
val secondKey = createCacheKey(marathonVersion = "1.0")

firstKey shouldBeEqualTo secondKey
}
firstKey shouldBeEqualTo secondKey
}

@Test
fun differCacheKeysForDifferentComponentCacheKeys() {
runBlocking {
val firstKey = createCacheKey(componentCacheKey = "abc")
val secondKey = createCacheKey(componentCacheKey = "def")
fun differCacheKeysForDifferentComponentCacheKeys() = runTest {
val firstKey = createCacheKey(componentCacheKey = "abc")
val secondKey = createCacheKey(componentCacheKey = "def")

firstKey shouldNotBeEqualTo secondKey
}
firstKey shouldNotBeEqualTo secondKey
}

@Test
fun sameCacheKeysForSameComponentCacheKeys() {
runBlocking {
val firstKey = createCacheKey(componentCacheKey = "abc")
val secondKey = createCacheKey(componentCacheKey = "abc")
fun sameCacheKeysForSameComponentCacheKeys() = runTest {
val firstKey = createCacheKey(componentCacheKey = "abc")
val secondKey = createCacheKey(componentCacheKey = "abc")

firstKey shouldBeEqualTo secondKey
}
firstKey shouldBeEqualTo secondKey
}

@Test
fun differentCacheKeysForDifferentDevicePools() {
runBlocking {
val firstKey = createCacheKey(devicePoolId = DevicePoolId("abc"))
val secondKey = createCacheKey(devicePoolId = DevicePoolId("def"))
fun differentCacheKeysForDifferentDevicePools() = runTest {
val firstKey = createCacheKey(devicePoolId = DevicePoolId("abc"))
val secondKey = createCacheKey(devicePoolId = DevicePoolId("def"))

firstKey shouldNotBeEqualTo secondKey
}
firstKey shouldNotBeEqualTo secondKey
}

@Test
fun sameCacheKeysForTheSameDevicePools() {
runBlocking {
val firstKey = createCacheKey(devicePoolId = DevicePoolId("abc"))
val secondKey = createCacheKey(devicePoolId = DevicePoolId("abc"))
fun sameCacheKeysForTheSameDevicePools() = runTest {
val firstKey = createCacheKey(devicePoolId = DevicePoolId("abc"))
val secondKey = createCacheKey(devicePoolId = DevicePoolId("abc"))

firstKey shouldBeEqualTo secondKey
}
firstKey shouldBeEqualTo secondKey
}

@Test
fun differentCacheKeysForDifferentTestPackageNames() {
runBlocking {
val firstKey = createCacheKey(test = createTest(packageName = "abc"))
val secondKey = createCacheKey(test = createTest(packageName = "def"))
fun differentCacheKeysForDifferentTestPackageNames() = runTest {
val firstKey = createCacheKey(test = createTest(packageName = "abc"))
val secondKey = createCacheKey(test = createTest(packageName = "def"))

firstKey shouldNotBeEqualTo secondKey
}
firstKey shouldNotBeEqualTo secondKey
}

@Test
fun sameCacheKeysForTheSamePackageNames() {
runBlocking {
val firstKey = createCacheKey(test = createTest(packageName = "abc"))
val secondKey = createCacheKey(test = createTest(packageName = "abc"))
fun sameCacheKeysForTheSamePackageNames() = runTest {
val firstKey = createCacheKey(test = createTest(packageName = "abc"))
val secondKey = createCacheKey(test = createTest(packageName = "abc"))

firstKey shouldBeEqualTo secondKey
}
firstKey shouldBeEqualTo secondKey
}

@Test
fun differentCacheKeysForDifferentClassNames() {
runBlocking {
val firstKey = createCacheKey(test = createTest(clazz = "abc"))
val secondKey = createCacheKey(test = createTest(clazz = "def"))
fun differentCacheKeysForDifferentClassNames() = runTest {
val firstKey = createCacheKey(test = createTest(clazz = "abc"))
val secondKey = createCacheKey(test = createTest(clazz = "def"))

firstKey shouldNotBeEqualTo secondKey
}
firstKey shouldNotBeEqualTo secondKey
}

@Test
fun sameCacheKeysForTheSameClassNames() {
runBlocking {
val firstKey = createCacheKey(test = createTest(clazz = "abc"))
val secondKey = createCacheKey(test = createTest(clazz = "abc"))
fun sameCacheKeysForTheSameClassNames() = runTest {
val firstKey = createCacheKey(test = createTest(clazz = "abc"))
val secondKey = createCacheKey(test = createTest(clazz = "abc"))

firstKey shouldBeEqualTo secondKey
}
firstKey shouldBeEqualTo secondKey
}

@Test
fun differentCacheKeysForDifferentMethodNames() {
runBlocking {
val firstKey = createCacheKey(test = createTest(method = "abc"))
val secondKey = createCacheKey(test = createTest(method = "def"))
fun differentCacheKeysForDifferentMethodNames() = runTest {
val firstKey = createCacheKey(test = createTest(method = "abc"))
val secondKey = createCacheKey(test = createTest(method = "def"))

firstKey shouldNotBeEqualTo secondKey
}
firstKey shouldNotBeEqualTo secondKey
}

@Test
fun sameCacheKeysForTheSameMethodNames() {
runBlocking {
val firstKey = createCacheKey(test = createTest(method = "abc"))
val secondKey = createCacheKey(test = createTest(method = "abc"))
fun sameCacheKeysForTheSameMethodNames() = runTest {
val firstKey = createCacheKey(test = createTest(method = "abc"))
val secondKey = createCacheKey(test = createTest(method = "abc"))

firstKey shouldBeEqualTo secondKey
}
firstKey shouldBeEqualTo secondKey
}
}

private fun createCacheKey(
private suspend fun createCacheKey(
marathonVersion: String = "123",
componentCacheKey: String = "abc",
devicePoolId: DevicePoolId = DevicePoolId("omni"),
test: MarathonTest = createTest()
): CacheKey = runBlocking {
): CacheKey {
val componentCacheKeyProvider = object : ComponentCacheKeyProvider {
override suspend fun getCacheKey(componentInfo: ComponentInfo): String = componentCacheKey
}
val versionNameProvider = mock<VersionNameProvider> {
on { this.versionName }.thenReturn(marathonVersion)
}
val cacheKeyFactory = TestCacheKeyFactory(componentCacheKeyProvider, versionNameProvider)
cacheKeyFactory.getCacheKey(devicePoolId, test)
return cacheKeyFactory.getCacheKey(devicePoolId, test)
}

private fun createTest(
Expand Down
Loading

0 comments on commit e3ea0e6

Please sign in to comment.