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 authored May 7, 2024
1 parent a6e1425 commit 77e029f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=9.1.1
version=9.1.2
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 @@ -105,7 +105,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() && pollingMode instanceof AutoPollingMode) {
AutoPollingMode autoPollingMode = (AutoPollingMode) pollingMode;
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/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,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 = "9.1.1";
static final String VERSION = "9.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 @@ -224,6 +224,29 @@ void testPollIntervalRespectsCacheExpiration() throws Exception {
configService.close();
}

@Test
void testPollsWhenCacheExpired() throws Exception {
this.server.enqueue(new MockResponse().setResponseCode(200).setBody(String.format(TEST_JSON, "test1")));

ConfigCache cache = new SingleValueCache(Helpers.cacheValueFromConfigJsonWithTime(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("", fetcher, pollingMode, cache, logger, false, new ConfigCatHooks());

configService.getSettings().get();

assertEquals("test1", configService.getSettings().get().settings().get("fakeKey").getSettingsValue().getStringValue());
assertEquals(1, this.server.getRequestCount());

configService.close();
}

@Test
void testOnlineOffline() throws Exception {
this.server.enqueue(new MockResponse().setResponseCode(200).setBody(String.format(TEST_JSON, "test")));
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/com/configcat/Helpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ static String cacheValueFromConfigJson(String json) {
return entry.serialize();
}

static String cacheValueFromConfigJsonWithTime(String json, long time) {
Config config = Utils.gson.fromJson(json, Config.class);
Entry entry = new Entry(config, "fakeTag", json, time);
return entry.serialize();
}

static String cacheValueFromConfigJsonWithEtag(String json, String etag) {
Config config = Utils.gson.fromJson(json, Config.class);
Entry entry = new Entry(config, etag, json, System.currentTimeMillis());
Expand Down

0 comments on commit 77e029f

Please sign in to comment.