Skip to content

Commit

Permalink
Updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vitoksmile committed Jan 23, 2024
1 parent af73500 commit f2855ec
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
33 changes: 33 additions & 0 deletions src/test/kotlin/io/getunleash/UnleashConfigTest.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package io.getunleash

import okhttp3.OkHttpClient
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.junit.jupiter.api.Test
import java.util.concurrent.TimeUnit
import kotlin.test.assertEquals

class UnleashConfigTest {

Expand Down Expand Up @@ -107,4 +110,34 @@ class UnleashConfigTest {
val configWithMetricsSetInSeconds = config.newBuilder().enableMetrics().metricsIntervalInSeconds(5).build()
assertThat(configInMs.reportMetrics!!.metricsInterval).isEqualTo(configWithMetricsSetInSeconds.reportMetrics!!.metricsInterval)
}

@Test
fun `Default http client for metrics uses config`() {
val config = UnleashConfig(
proxyUrl = "https://localhost:4242/proxy",
clientKey = "some-key",
appName = "my-app",
environment = "default"
)
assertThat(config.reportMetrics).isNull()
val withMetrics = config.newBuilder().enableMetrics().build()
assertThat(withMetrics.reportMetrics).isNotNull
assertThat(withMetrics.reportMetrics!!.httpClient.connectTimeoutMillis).isEqualTo(config.httpClientConnectionTimeout)
assertThat(withMetrics.reportMetrics!!.httpClient.readTimeoutMillis).isEqualTo(config.httpClientReadTimeout)
}

@Test
fun `Can override http client for metrics`() {
val config = UnleashConfig(
proxyUrl = "https://localhost:4242/proxy",
clientKey = "some-key",
appName = "my-app",
environment = "default"
)
assertThat(config.reportMetrics).isNull()
val okHttpClient = OkHttpClient.Builder().build()
val withMetrics = config.newBuilder().enableMetrics().metricsHttpClient(okHttpClient).build()
assertThat(withMetrics.reportMetrics).isNotNull
assertEquals(okHttpClient, withMetrics.reportMetrics!!.httpClient)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ package io.getunleash.metrics

import io.getunleash.UnleashConfig
import io.getunleash.data.Variant
import okhttp3.OkHttpClient
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import java.util.concurrent.TimeUnit

class HttpMetricsReporterTest {

@Test
fun metricsUrlIsCorrect() {
HttpMetricsReporter(UnleashConfig.newBuilder().proxyUrl("http://localhost:4242/proxy").clientKey("some-key").build()).use { reporter ->
val okHttpClient = OkHttpClient.Builder().build()
HttpMetricsReporter(UnleashConfig.newBuilder().proxyUrl("http://localhost:4242/proxy").clientKey("some-key").build(), okHttpClient).use { reporter ->
assertThat(reporter.metricsUrl.toString()).isEqualTo("http://localhost:4242/proxy/client/metrics")
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/test/kotlin/io/getunleash/metrics/MetricsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.getunleash.UnleashContext
import io.getunleash.data.Parser
import io.getunleash.data.Variant
import io.getunleash.polling.PollingModes
import okhttp3.OkHttpClient
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import org.assertj.core.api.Assertions.assertThat
Expand All @@ -18,6 +19,7 @@ import java.time.ZoneOffset
import java.time.ZonedDateTime
import java.util.Date
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.TimeUnit

fun <K, V> concurrentHashMapOf(vararg pairs: Pair<K, V>): ConcurrentHashMap<K, V> {
val map: ConcurrentHashMap<K, V> = ConcurrentHashMap()
Expand Down Expand Up @@ -158,7 +160,8 @@ class MetricsTest {

@Test
fun `http reporter does actually report toggles to metrics endpoint`() {
val reporter = HttpMetricsReporter(config)
val okHttpClient = OkHttpClient.Builder().build()
val reporter = HttpMetricsReporter(config, okHttpClient)
val client = UnleashClient(config, context, metricsReporter = reporter)
repeat(100) {
client.isEnabled("unleash-android-proxy-sdk")
Expand Down

0 comments on commit f2855ec

Please sign in to comment.