Skip to content

Commit 4dad14b

Browse files
authored
Fix auto poll initial config load from cache (#34)
* Fix initial config load when auto poll enabled with results from cache * Format * Update Constants.kt
1 parent 3d4bd02 commit 4dad14b

File tree

4 files changed

+38
-18
lines changed

4 files changed

+38
-18
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
group=com.configcat
2-
version=3.1.0
2+
version=3.1.1
33

44
ktorVersion=2.3.10
55
kotlinxSerializationVersion=1.6.3

src/commonMain/kotlin/com/configcat/ConfigService.kt

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,29 +65,30 @@ internal class ConfigService(
6565
}
6666

6767
suspend fun getSettings(): SettingResult {
68-
return when (mode) {
69-
is LazyLoadMode -> {
70-
val result =
68+
val result =
69+
when (mode) {
70+
is LazyLoadMode -> {
7171
fetchIfOlder(
7272
DateTime.now()
7373
.add(0, -mode.configuration.cacheRefreshInterval.inWholeMilliseconds.toDouble()),
7474
)
75-
if (result.first.isEmpty()) {
76-
SettingResult.empty
77-
} else {
78-
SettingResult(result.first.config.settings ?: emptyMap(), result.first.fetchTime)
7975
}
80-
}
81-
82-
else -> {
83-
// If we are initialized, we prefer the cached results
84-
val result = fetchIfOlder(Constants.distantPast, preferCached = initialized.value)
85-
if (result.first.isEmpty()) {
86-
SettingResult.empty
87-
} else {
88-
SettingResult(result.first.config.settings ?: emptyMap(), result.first.fetchTime)
76+
else -> {
77+
// If we are initialized, we prefer the cached results
78+
val threshold =
79+
if (!initialized.value && mode is AutoPollMode) {
80+
DateTime.now()
81+
.add(0, -mode.configuration.pollingInterval.inWholeMilliseconds.toDouble())
82+
} else {
83+
Constants.distantPast
84+
}
85+
fetchIfOlder(threshold, preferCached = initialized.value)
8986
}
9087
}
88+
return if (result.first.isEmpty()) {
89+
SettingResult.empty
90+
} else {
91+
SettingResult(result.first.config.settings ?: emptyMap(), result.first.fetchTime)
9192
}
9293
}
9394

src/commonMain/kotlin/com/configcat/Constants.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ internal interface Closeable {
2323
}
2424

2525
internal object Constants {
26-
const val VERSION: String = "3.1.0"
26+
const val VERSION: String = "3.1.1"
2727
const val CONFIG_FILE_NAME: String = "config_v6.json"
2828
const val SERIALIZATION_FORMAT_VERSION: String = "v2"
2929
const val GLOBAL_CDN_URL = "https://cdn-global.configcat.com"

src/commonTest/kotlin/com/configcat/ConfigServiceTests.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,25 @@ class ConfigServiceTests {
312312
}
313313
}
314314

315+
@Test
316+
fun testPollsWhenCacheExpired() =
317+
runTest {
318+
val mockEngine =
319+
MockEngine.create {
320+
this.addHandler {
321+
respond(content = Data.formatJsonBodyWithString("test1"), status = HttpStatusCode.OK)
322+
}
323+
} as MockEngine
324+
325+
val cache = SingleValueCache(Data.formatCacheEntryWithDate("test", DateTime.now().add(0, -5000.0)))
326+
val service = Services.createConfigService(mockEngine, autoPoll { pollingInterval = 1.seconds }, cache)
327+
328+
val setting = service.getSettings().settings["fakeKey"]
329+
assertEquals("test1", setting?.settingValue?.stringValue)
330+
331+
assertEquals(1, mockEngine.requestHistory.size)
332+
}
333+
315334
@Test
316335
fun testAutoPollOnlineOffline() =
317336
runTest {

0 commit comments

Comments
 (0)