Skip to content

Commit 7e6b03c

Browse files
authored
Merge pull request #15 from configcat/fix-useragent-cache
Fix user agent / cache
2 parents 179aa08 + ad7f5cb commit 7e6b03c

File tree

8 files changed

+59
-41
lines changed

8 files changed

+59
-41
lines changed

DEPLOY.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Steps to deploy
22
## Preparation
33
1. Run tests
4-
3. Increase the version in the gradle.properties file.
4+
3. Increase the version in the `gradle.properties` and `src/main/java/com.configcat/ConfigFetcher.java` file.
55
4. Commit & Push
66
## Publish
77
Use the **same version** for the git tag as in the properties file.
@@ -20,9 +20,11 @@ Use the **same version** for the git tag as in the properties file.
2020
Create a new [Github release](https://github.com/configcat/android-sdk/releases) with a new version tag and release notes.
2121

2222
## Sync
23-
1. Make sure the new version is available on [jcenter](https://bintray.com/configcat/releases/configcat-android-client).
24-
2. Log in to bintray.com and sync the new package to Maven Central. (https://bintray.com/configcat/releases/configcat-android-client#central)
25-
3. Make sure the new version is available on [Maven Central](https://search.maven.org/artifact/com.configcat/configcat-android-client).
23+
1. Log in to [Maven Repository](https://oss.sonatype.org/) and follow these steps:
24+
1. Select `Staging Repositories` and select the version you published.
25+
2. Click `Close`. The process might take some time, click `Refresh` to get the latest state.
26+
3. When the repo is closed click `Release`, tick the `Automatically drop when released` option.
27+
2. Make sure the new version is available on [Maven Central](https://search.maven.org/artifact/com.configcat/configcat-android-client).
2628

2729
## Update import examples in local README.md
2830

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version=7.0.0
1+
version=7.0.1

src/main/java/com/configcat/ConfigFetcher.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ConfigFetcher implements Closeable {
1414
private final ConfigCatLogger logger;
1515
private final OkHttpClient httpClient;
1616
private final String mode;
17-
private final String version;
17+
private final String version = "7.0.1";
1818
private final ConfigJsonCache configJsonCache;
1919
private final String sdkKey;
2020
private final boolean urlIsCustom;
@@ -41,7 +41,6 @@ enum RedirectMode {
4141
this.urlIsCustom = urlIsCustom;
4242
this.url = url;
4343
this.httpClient = httpClient;
44-
this.version = this.getClass().getPackage().getImplementationVersion();
4544
this.mode = pollingIdentifier;
4645
}
4746

@@ -169,7 +168,7 @@ public void close() throws IOException {
169168
Request getRequest(String etag) {
170169
String url = this.url + "/configuration-files/" + this.sdkKey + "/" + CONFIG_JSON_NAME + ".json";
171170
Request.Builder builder = new Request.Builder()
172-
.addHeader("X-ConfigCat-UserAgent", "ConfigCat-Java/" + this.mode + "-" + this.version);
171+
.addHeader("X-ConfigCat-UserAgent", "ConfigCat-Droid/" + this.mode + "-" + this.version);
173172

174173
if (etag != null && !etag.isEmpty())
175174
builder.addHeader("If-None-Match", etag);

src/main/java/com/configcat/ConfigJsonCache.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import org.apache.commons.codec.binary.Hex;
66
import org.apache.commons.codec.digest.DigestUtils;
77

8-
import java.time.Instant;
9-
108
class ConfigJsonCache {
119
private static final String CACHE_BASE = "java_" + ConfigFetcher.CONFIG_JSON_NAME + "_%s";
1210
private Config inMemoryConfig = Config.empty;
@@ -45,9 +43,6 @@ public Config readFromCache() {
4543

4644
try {
4745
Config config = this.deserialize(fromCache);
48-
if (this.inMemoryConfig.timeStamp >= config.timeStamp) {
49-
return this.inMemoryConfig;
50-
}
5146
this.inMemoryConfig = config;
5247
this.inMemoryConfigString = fromCache;
5348
return config;
@@ -59,7 +54,6 @@ public Config readFromCache() {
5954

6055
public void writeToCache(Config config) {
6156
try {
62-
config.timeStamp = Instant.now().getEpochSecond();
6357
String configToCache = this.gson.toJson(config);
6458
this.inMemoryConfig = config;
6559
this.inMemoryConfigString = configToCache;

src/main/java/com/configcat/ConfigModels.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ class Config {
2020
public Map<String, Setting> entries = new HashMap<>();
2121
@SerializedName(value = "e")
2222
public String eTag = "";
23-
@SerializedName(value = "t")
24-
public long timeStamp = -1;
2523

2624
public static Config empty = new Config();
2725
}

src/test/java/com/configcat/ConfigCatClientIntegrationTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.junit.jupiter.api.AfterEach;
77
import org.junit.jupiter.api.BeforeEach;
88
import org.junit.jupiter.api.Test;
9+
import org.slf4j.LoggerFactory;
910

1011
import java.io.IOException;
1112
import java.util.Collection;
@@ -217,4 +218,27 @@ public void getAllKeys() {
217218
assertEquals(16, keys.size());
218219
assertTrue(keys.contains("stringDefaultCat"));
219220
}
221+
222+
@Test
223+
public void ensureFailingCacheWriteDoesNotPreventFurtherWrites() {
224+
FailingWriteCache cache = new FailingWriteCache();
225+
ConfigJsonCache memoryCache = new ConfigJsonCache(
226+
new ConfigCatLogger(LoggerFactory.getLogger(ConfigCatClientIntegrationTest.class)), cache, "");
227+
228+
Config initialConfig = memoryCache.readFromJson(String.format(TEST_JSON, "initial"), "etag1");
229+
memoryCache.writeToCache(initialConfig);
230+
231+
Config updated = memoryCache.readFromJson(String.format(TEST_JSON, "updated"), "etag2");
232+
memoryCache.writeToCache(updated); // this will fail
233+
234+
Config fromCache1 = memoryCache.readFromCache();
235+
assertEquals(initialConfig.eTag, fromCache1.eTag);
236+
237+
memoryCache.writeToCache(updated);
238+
239+
Config fromCache2 = memoryCache.readFromCache();
240+
assertEquals(updated.eTag, fromCache2.eTag);
241+
242+
assertEquals(2, cache.successCounter.get());
243+
}
220244
}

src/test/java/com/configcat/ConfigFetcherTest.java

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -140,38 +140,13 @@ public void getCacheFails() throws Exception {
140140
fetcher.close();
141141
}
142142

143-
@Test
144-
public void cacheWriteFails() throws Exception {
145-
this.server.enqueue(new MockResponse().setResponseCode(200).setBody(TEST_JSON2));
146-
ConfigCache cache = mock(ConfigCache.class);
147-
148-
Gson gson = new GsonBuilder().create();
149-
Config config = gson.fromJson(TEST_JSON, Config.class);
150-
config.timeStamp = Instant.now().getEpochSecond();
151-
152-
when(cache.read(anyString())).thenReturn(gson.toJson(config));
153-
doThrow(new Exception()).when(cache).write(anyString(), anyString());
154-
155-
ConfigJsonCache configJsonCache = new ConfigJsonCache(logger, cache, "");
156-
ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient.Builder().build(), logger, configJsonCache,
157-
"", this.server.url("/").toString(), false, PollingModes.manualPoll().getPollingIdentifier());
158-
159-
FetchResponse result = fetcher.fetchAsync().get();
160-
configJsonCache.writeToCache(result.config());
161-
162-
assertEquals("fakeValue2", configJsonCache.readFromCache().entries.get("fakeKey").value.getAsString());
163-
164-
fetcher.close();
165-
}
166-
167143
@Test
168144
public void cacheWriteFailsCachedTakesPrecedence() throws Exception {
169145
this.server.enqueue(new MockResponse().setResponseCode(200).setBody(TEST_JSON2));
170146
ConfigCache cache = mock(ConfigCache.class);
171147

172148
Gson gson = new GsonBuilder().create();
173149
Config config = gson.fromJson(TEST_JSON, Config.class);
174-
config.timeStamp = Instant.now().getEpochSecond() + 50;
175150

176151
when(cache.read(anyString())).thenReturn(gson.toJson(config));
177152
doThrow(new Exception()).when(cache).write(anyString(), anyString());
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.configcat;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.concurrent.atomic.AtomicInteger;
6+
7+
public class FailingWriteCache extends ConfigCache {
8+
Map<String, String> map = new HashMap<>();
9+
AtomicInteger counter = new AtomicInteger(1);
10+
public AtomicInteger successCounter = new AtomicInteger(0);
11+
12+
@Override
13+
protected String read(String key) {
14+
return map.get(key);
15+
}
16+
17+
@Override
18+
protected void write(String key, String value) throws Exception {
19+
if(counter.getAndIncrement() % 2 == 0){
20+
throw new Exception();
21+
}
22+
23+
successCounter.incrementAndGet();
24+
map.put(key, value);
25+
}
26+
}

0 commit comments

Comments
 (0)