Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tyiuhc committed Jan 26, 2024
1 parent b5eb1f2 commit aa4f1d8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ java {
dependencies {
implementation(kotlin("stdlib"))
testImplementation(kotlin("test"))
testImplementation("org.mockito:mockito-core:${Versions.mockito}")
testImplementation("io.mockk:mockk:${Versions.mockk}")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:${Versions.serializationRuntime}")
implementation("com.squareup.okhttp3:okhttp:${Versions.okhttp}")
implementation("com.amplitude:evaluation-core:${Versions.evaluationCore}")
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ object Versions {
const val evaluationCore = "1.1.1"
const val evaluationSerialization = "1.1.1"
const val amplitudeAnalytics = "1.12.0"
const val mockito = "4.8.1"
const val mockk = "1.13.9"
}
2 changes: 1 addition & 1 deletion src/main/kotlin/util/FetchException.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.amplitude.experiment.util

import okio.IOException

class FetchException internal constructor(
internal class FetchException internal constructor(
val statusCode: Int,
message: String
) : IOException(message)
40 changes: 40 additions & 0 deletions src/test/kotlin/RemoteEvaluationClientTest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.amplitude.experiment

import com.amplitude.experiment.util.FetchException
import com.amplitude.experiment.util.Logger
import com.amplitude.experiment.util.SystemLogger
import io.mockk.every
import io.mockk.spyk
import io.mockk.verify
import org.junit.Assert
import java.util.Date
import java.util.concurrent.CancellationException
Expand Down Expand Up @@ -84,6 +88,7 @@ class RemoteEvaluationClientTest {
}
}


// @Test
// fun test() {
// val client = RemoteEvaluationClient(API_KEY, RemoteEvaluationConfig())
Expand All @@ -100,6 +105,41 @@ class RemoteEvaluationClientTest {
// }
// }
// }

@Test
fun `fetch retry with different response codes`() {
// Response code, error message, and whether retry should be called
val testData = listOf(
Triple(300, "Fetch Exception 300", 2),
Triple(400, "Fetch Exception 400", 1),
Triple(429, "Fetch Exception 429", 2),
Triple(500, "Fetch Exception 500", 2),
Triple(0, "Other Exception", 2)
)

testData.forEach { (responseCode, errorMessage, fetchCalls) ->
val config = RemoteEvaluationConfig(fetchRetries = 1, debug = true)
val client = spyk(RemoteEvaluationClient("apiKey", config), recordPrivateCalls = true)
// Mock the private method to throw FetchException or other exceptions
every { client["doFetch"](any<ExperimentUser>(), any<Long>()) } answers {
val future = CompletableFuture<Map<String, Variant>>()
if (responseCode == 0) {
future.completeExceptionally(Exception(errorMessage))
} else {
future.completeExceptionally(FetchException(responseCode, errorMessage))
}
future
}

try {
client.fetch(ExperimentUser("test_user")).get()
} catch (t: Throwable) {
println(t.toString())
}

verify(exactly = fetchCalls) { client["doFetch"](any<ExperimentUser>(), any<Long>()) }
}
}
}

@Suppress("SameParameterValue")
Expand Down

0 comments on commit aa4f1d8

Please sign in to comment.