|
| 1 | +package com.statsig.sdk |
| 2 | +import kotlinx.coroutines.runBlocking |
| 3 | +import okhttp3.mockwebserver.Dispatcher |
| 4 | +import okhttp3.mockwebserver.MockResponse |
| 5 | +import okhttp3.mockwebserver.MockWebServer |
| 6 | +import okhttp3.mockwebserver.RecordedRequest |
| 7 | +import org.junit.After |
| 8 | +import org.junit.Before |
| 9 | +import org.junit.Test |
| 10 | + |
| 11 | +class StatsigPollingTest { |
| 12 | + private lateinit var statsig: StatsigServer |
| 13 | + private lateinit var options: StatsigOptions |
| 14 | + private val logLines: MutableList<String> = mutableListOf() |
| 15 | + |
| 16 | + @Before |
| 17 | + fun setUp() { |
| 18 | + options = |
| 19 | + StatsigOptions( |
| 20 | + fallbackToStatsigAPI = true, |
| 21 | + rulesetsSyncIntervalMs = 1000, |
| 22 | + customLogger = |
| 23 | + object : LoggerInterface { |
| 24 | + override fun error(message: String) { |
| 25 | + } |
| 26 | + |
| 27 | + override fun warn(message: String) { |
| 28 | + } |
| 29 | + |
| 30 | + override fun info(message: String) { |
| 31 | + logLines.add(message) |
| 32 | + } |
| 33 | + |
| 34 | + override fun debug(message: String) { |
| 35 | + } |
| 36 | + |
| 37 | + override fun setLogLevel(level: LogLevel) { |
| 38 | + } |
| 39 | + }, |
| 40 | + logLevel = LogLevel.INFO, |
| 41 | + ) |
| 42 | + statsig = StatsigServer.create() |
| 43 | + } |
| 44 | + |
| 45 | + @After |
| 46 | + fun cleanup() { |
| 47 | + logLines.clear() |
| 48 | + statsig.shutdown() |
| 49 | + } |
| 50 | + |
| 51 | + fun setupWebServer(success: Boolean): MockWebServer { |
| 52 | + val server = MockWebServer() |
| 53 | + server.dispatcher = |
| 54 | + object : Dispatcher() { |
| 55 | + override fun dispatch(request: RecordedRequest): MockResponse { |
| 56 | + if ("/v1/download_config_specs" in request.path!!) { |
| 57 | + if (success) { |
| 58 | + val downloadConfigSpecsResponse = |
| 59 | + StatsigE2ETest::class.java.getResource("/download_config_specs.json")?.readText() ?: "" |
| 60 | + return MockResponse().setResponseCode(200).setBody(downloadConfigSpecsResponse) |
| 61 | + } else { |
| 62 | + return MockResponse().setResponseCode(500) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return MockResponse().setResponseCode(202) |
| 67 | + } |
| 68 | + } |
| 69 | + return server |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + fun testPollFromSources() { |
| 74 | + val server = setupWebServer(true) |
| 75 | + options.api = server.url("/v1").toString() |
| 76 | + runBlocking { |
| 77 | + statsig.initialize("secret-key", options) |
| 78 | + } |
| 79 | + val dcsRequestCount = |
| 80 | + logLines |
| 81 | + .filter { |
| 82 | + it.startsWith("[StatsigHTTPHelper]") && it.contains("v1/download_config_specs") |
| 83 | + }.size |
| 84 | + assert(dcsRequestCount == 1) |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + fun testFallbackOnPollFromSources() { |
| 89 | + val server = setupWebServer(false) |
| 90 | + options.api = server.url("/v1").toString() |
| 91 | + runBlocking { |
| 92 | + statsig.initialize("secret-key", options) |
| 93 | + } |
| 94 | + val dcsRequestCount = |
| 95 | + logLines |
| 96 | + .filter { |
| 97 | + it.startsWith("[StatsigHTTPHelper]") && it.contains("v1/download_config_specs") |
| 98 | + }.size |
| 99 | + assert(dcsRequestCount == 2) |
| 100 | + } |
| 101 | +} |
0 commit comments