Skip to content

Commit 26f05bb

Browse files
authored
Fix issue with ueh only storing one exp to var paring (#31)
* Fix issue with ueh only storing one exp to var paring * Remove noisy log
1 parent aa8ce85 commit 26f05bb

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

user-experiment-record/src/androidTest/java/com/optimizely/user_experiment_record/AndroidUserExperimentRecordTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,11 @@ public void startHandlesJSONException() throws IOException {
195195
public void start() throws JSONException {
196196
androidUserExperimentRecord.start();
197197
androidUserExperimentRecord.save("user1", "exp1", "var1");
198+
androidUserExperimentRecord.save("user1", "exp2", "var2");
198199

199200
Map<String, String> expKeyToVarKeyMap = new HashMap<>();
200201
expKeyToVarKeyMap.put("exp1", "var1");
202+
expKeyToVarKeyMap.put("exp2", "var2");
201203
Map<String, Map<String, String>> recordMap = new HashMap<>();
202204
recordMap.put("user1", expKeyToVarKeyMap);
203205

user-experiment-record/src/androidTest/java/com/optimizely/user_experiment_record/UserExperimentRecordCacheTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@ public void loadWhenNoFile() throws JSONException {
6868
@Test
6969
public void testSaveAndLoad() throws JSONException {
7070
assertTrue(userExperimentRecordCache.save("foo", "exp1", "var1"));
71+
assertTrue(userExperimentRecordCache.save("foo", "exp2", "var2"));
7172
JSONObject expectedActivation = new JSONObject();
7273
JSONObject expectedExpIdToVarId = new JSONObject();
7374
expectedExpIdToVarId.put("exp1", "var1");
75+
expectedExpIdToVarId.put("exp2", "var2");
7476
expectedActivation.put("foo", expectedExpIdToVarId);
7577
assertEquals(expectedActivation.toString(), userExperimentRecordCache.load().toString());
7678
}

user-experiment-record/src/main/java/com/optimizely/user_experiment_record/AndroidUserExperimentRecord.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ public void start() {
9494
while (iterator2.hasNext()) {
9595
String expId = iterator2.next();
9696
String varId = expIdToVarIdJson.getString(expId);
97-
Map<String, String> expIdToVarIdMap = new HashMap<>();
97+
Map<String, String> expIdToVarIdMap = writeThroughCacheTaskFactory.getMemoryUserExperimentRecordCache().get(userId);
98+
if (expIdToVarIdMap == null) {
99+
expIdToVarIdMap = new HashMap<>();
100+
}
98101
expIdToVarIdMap.put(expId, varId);
99102
writeThroughCacheTaskFactory.getMemoryUserExperimentRecordCache().put(userId, expIdToVarIdMap);
100103
}
@@ -160,10 +163,6 @@ public String lookup(String userId, String experimentKey) {
160163
variationKey = expIdToVarIdMap.get(experimentKey);
161164
}
162165

163-
if (variationKey == null) {
164-
logger.error("Project config did not contain matching experiment and variation ids");
165-
}
166-
167166
return variationKey;
168167
}
169168

user-experiment-record/src/main/java/com/optimizely/user_experiment_record/UserExperimentRecordCache.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@ boolean remove(@NonNull String userId, @NonNull String experimentId) {
7979
boolean save(@NonNull String userId, @NonNull String experimentId, @NonNull String variationId) {
8080
try {
8181
JSONObject userExperimentRecord = load();
82-
userExperimentRecord.put(userId, null);
83-
JSONObject expIdToVarId = new JSONObject();
82+
JSONObject expIdToVarId = userExperimentRecord.optJSONObject(userId);
83+
if (expIdToVarId == null) {
84+
expIdToVarId = new JSONObject();
85+
}
8486
expIdToVarId.put(experimentId, variationId);
8587
userExperimentRecord.put(userId, expIdToVarId);
8688
return cache.save(getFileName(), userExperimentRecord.toString());

0 commit comments

Comments
 (0)