Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix auto poll initial config load from cache #51

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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