Skip to content

Commit

Permalink
Fix initial config load when auto poll enabled with results from cache (
Browse files Browse the repository at this point in the history
  • Loading branch information
z4kn4fein committed May 7, 2024
1 parent 1285ce3 commit 0d3031a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
version=10.1.1
version=10.1.2

org.gradle.jvmargs=-Xmx2g
7 changes: 6 additions & 1 deletion src/main/java/com/configcat/ConfigService.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,12 @@ public CompletableFuture<SettingResult> 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);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/configcat/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/com/configcat/AutoPollingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")));
Expand Down

0 comments on commit 0d3031a

Please sign in to comment.