Skip to content

Commit 0864a38

Browse files
Fix polling behavior (#468)
There is a bug that we fetch from multiple sources.
1 parent 66cc371 commit 0864a38

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

src/main/kotlin/com/statsig/sdk/SpecUpdater.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ internal class SpecUpdater(
241241
if (config != null) {
242242
logger.debug("[StatsigSpecUpdater] Successfully fetched config specs from source: $source")
243243
emit(Pair(config, source))
244+
break
244245
}
245246
}
246247
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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

Comments
 (0)