File tree Expand file tree Collapse file tree 4 files changed +38
-18
lines changed
commonMain/kotlin/com/configcat
commonTest/kotlin/com/configcat Expand file tree Collapse file tree 4 files changed +38
-18
lines changed Original file line number Diff line number Diff line change 1
1
group =com.configcat
2
- version =3.1.0
2
+ version =3.1.1
3
3
4
4
ktorVersion =2.3.10
5
5
kotlinxSerializationVersion =1.6.3
Original file line number Diff line number Diff line change @@ -65,29 +65,30 @@ internal class ConfigService(
65
65
}
66
66
67
67
suspend fun getSettings (): SettingResult {
68
- return when (mode) {
69
- is LazyLoadMode -> {
70
- val result =
68
+ val result =
69
+ when (mode) {
70
+ is LazyLoadMode -> {
71
71
fetchIfOlder(
72
72
DateTime .now()
73
73
.add(0 , - mode.configuration.cacheRefreshInterval.inWholeMilliseconds.toDouble()),
74
74
)
75
- if (result.first.isEmpty()) {
76
- SettingResult .empty
77
- } else {
78
- SettingResult (result.first.config.settings ? : emptyMap(), result.first.fetchTime)
79
75
}
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)
89
86
}
90
87
}
88
+ return if (result.first.isEmpty()) {
89
+ SettingResult .empty
90
+ } else {
91
+ SettingResult (result.first.config.settings ? : emptyMap(), result.first.fetchTime)
91
92
}
92
93
}
93
94
Original file line number Diff line number Diff line change @@ -23,7 +23,7 @@ internal interface Closeable {
23
23
}
24
24
25
25
internal object Constants {
26
- const val VERSION : String = " 3.1.0 "
26
+ const val VERSION : String = " 3.1.1 "
27
27
const val CONFIG_FILE_NAME : String = " config_v6.json"
28
28
const val SERIALIZATION_FORMAT_VERSION : String = " v2"
29
29
const val GLOBAL_CDN_URL = " https://cdn-global.configcat.com"
Original file line number Diff line number Diff line change @@ -312,6 +312,25 @@ class ConfigServiceTests {
312
312
}
313
313
}
314
314
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
+
315
334
@Test
316
335
fun testAutoPollOnlineOffline () =
317
336
runTest {
You can’t perform that action at this time.
0 commit comments