diff --git a/src/test/kotlin/io/getunleash/UnleashConfigTest.kt b/src/test/kotlin/io/getunleash/UnleashConfigTest.kt index 04ad171..f101ae5 100644 --- a/src/test/kotlin/io/getunleash/UnleashConfigTest.kt +++ b/src/test/kotlin/io/getunleash/UnleashConfigTest.kt @@ -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 { @@ -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) + } } diff --git a/src/test/kotlin/io/getunleash/metrics/HttpMetricsReporterTest.kt b/src/test/kotlin/io/getunleash/metrics/HttpMetricsReporterTest.kt index f636d7e..f52fa6c 100644 --- a/src/test/kotlin/io/getunleash/metrics/HttpMetricsReporterTest.kt +++ b/src/test/kotlin/io/getunleash/metrics/HttpMetricsReporterTest.kt @@ -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") } } diff --git a/src/test/kotlin/io/getunleash/metrics/MetricsTest.kt b/src/test/kotlin/io/getunleash/metrics/MetricsTest.kt index 9afef09..7e86ca6 100644 --- a/src/test/kotlin/io/getunleash/metrics/MetricsTest.kt +++ b/src/test/kotlin/io/getunleash/metrics/MetricsTest.kt @@ -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 @@ -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 concurrentHashMapOf(vararg pairs: Pair): ConcurrentHashMap { val map: ConcurrentHashMap = ConcurrentHashMap() @@ -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")