From 7f48aef43ad8c158bef3d428df392e46f62eccb2 Mon Sep 17 00:00:00 2001 From: Peter Csajtai Date: Sun, 5 May 2024 02:09:34 +0200 Subject: [PATCH] Fix initial config load when auto poll enabled with results from cache --- gradle.properties | 2 +- .../java/com/configcat/ConfigService.java | 7 +++++- src/main/java/com/configcat/Utils.java | 2 +- .../java/com/configcat/AutoPollingTest.java | 23 +++++++++++++++++++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/gradle.properties b/gradle.properties index eff8650..f3244d4 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ -version=10.1.1 +version=10.1.2 org.gradle.jvmargs=-Xmx2g \ No newline at end of file diff --git a/src/main/java/com/configcat/ConfigService.java b/src/main/java/com/configcat/ConfigService.java index 7c88024..fbc7c6c 100644 --- a/src/main/java/com/configcat/ConfigService.java +++ b/src/main/java/com/configcat/ConfigService.java @@ -103,7 +103,12 @@ public CompletableFuture getSettings() { ? new SettingResult(entryResult.value().getConfig().getEntries(), entryResult.value().getFetchTime()) : SettingResult.EMPTY); } else { - return fetchIfOlder(Constants.DISTANT_PAST, initialized.get()) // If we are initialized, we prefer the cached results + long threshold = Constants.DISTANT_PAST; + if (!initialized.get() && mode instanceof AutoPollingMode) { + AutoPollingMode autoPollingMode = (AutoPollingMode) mode; + threshold = System.currentTimeMillis() - (autoPollingMode.getAutoPollRateInSeconds() * 1000L); + } + return fetchIfOlder(threshold, initialized.get()) // If we are initialized, we prefer the cached results .thenApply(entryResult -> !entryResult.value().isEmpty() ? new SettingResult(entryResult.value().getConfig().getEntries(), entryResult.value().getFetchTime()) : SettingResult.EMPTY); diff --git a/src/main/java/com/configcat/Utils.java b/src/main/java/com/configcat/Utils.java index 10814a7..7b6c393 100644 --- a/src/main/java/com/configcat/Utils.java +++ b/src/main/java/com/configcat/Utils.java @@ -30,7 +30,7 @@ private Constants() { /* prevent from instantiation*/ } static final long DISTANT_PAST = 0; static final String CONFIG_JSON_NAME = "config_v6.json"; static final String SERIALIZATION_FORMAT_VERSION = "v2"; - static final String VERSION = "10.1.1"; + static final String VERSION = "10.1.2"; static final String SDK_KEY_PROXY_PREFIX = "configcat-proxy/"; static final String SDK_KEY_PREFIX = "configcat-sdk-1"; diff --git a/src/test/java/com/configcat/AutoPollingTest.java b/src/test/java/com/configcat/AutoPollingTest.java index 393b590..cab5b9f 100644 --- a/src/test/java/com/configcat/AutoPollingTest.java +++ b/src/test/java/com/configcat/AutoPollingTest.java @@ -222,6 +222,29 @@ void testPollIntervalRespectsCacheExpiration() throws Exception { policy.close(); } + @Test + void testPollsWhenCacheExpired() throws Exception { + this.server.enqueue(new MockResponse().setResponseCode(200).setBody(String.format(TEST_JSON, "test1"))); + + ConfigCache cache = new SingleValueCache(Helpers.cacheValueFromConfigJsonAndTime(String.format(TEST_JSON, "test"), System.currentTimeMillis() - 5000)); + + PollingMode pollingMode = PollingModes.autoPoll(1); + ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient(), + logger, + "", + this.server.url("/").toString(), + false, + pollingMode.getPollingIdentifier()); + ConfigService configService = new ConfigService("", pollingMode, cache, logger, fetcher, new ConfigCatHooks(), false); + + configService.getSettings().get(); + + assertEquals("test1", configService.getSettings().get().settings().get("fakeKey").getSettingsValue().getStringValue()); + assertEquals(1, this.server.getRequestCount()); + + configService.close(); + } + @Test void testNonExpiredCacheCallsReady() throws Exception { ConfigCache cache = new SingleValueCache(Helpers.cacheValueFromConfigJson(String.format(TEST_JSON, "test")));