Skip to content

Commit e72f341

Browse files
authored
set new batch UUID when partial events are sent (#340)
1 parent b86e546 commit e72f341

File tree

3 files changed

+88
-2
lines changed

3 files changed

+88
-2
lines changed

AndroidSDKCore/src/main/java/com/leanplum/internal/RequestOld.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,14 +727,19 @@ public List<Map<String, Object>> getUnsentRequests(double fraction) {
727727
lastSendTimeMs = System.currentTimeMillis();
728728
Context context = Leanplum.getContext();
729729
SharedPreferences preferences = context.getSharedPreferences(
730-
LEANPLUM, Context.MODE_PRIVATE);
730+
LEANPLUM, Context.MODE_PRIVATE);
731731
SharedPreferences.Editor editor = preferences.edit();
732732
int count = (int) (fraction * MAX_EVENTS_PER_API_CALL);
733733
requestData = LeanplumEventDataManager.getEvents(count);
734734
editor.remove(Constants.Defaults.UUID_KEY);
735735
SharedPreferencesUtil.commitChanges(editor);
736+
// if we send less than 100% of requests, we need to reset the batch
737+
// UUID for the next batch
738+
if (fraction < 1) {
739+
RequestOldUtil util = new RequestOldUtil();
740+
util.setNewBatchUUID(requestData);
741+
}
736742
}
737-
738743
return requestData;
739744
}
740745

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.leanplum.internal;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
import java.util.UUID;
6+
7+
import static com.leanplum.internal.RequestOld.UUID_KEY;
8+
9+
public class RequestOldUtil {
10+
11+
public void setNewBatchUUID(List<Map<String, Object>> requests) {
12+
String uuid = UUID.randomUUID().toString();
13+
for (Map<String, Object> request : requests) {
14+
request.put(UUID_KEY, uuid);
15+
}
16+
}
17+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.leanplum.internal;
2+
3+
import android.app.Application;
4+
5+
import com.leanplum.Leanplum;
6+
import com.leanplum.__setup.LeanplumTestApp;
7+
import com.leanplum._whitebox.utilities.SynchronousExecutor;
8+
9+
import junit.framework.TestCase;
10+
11+
import org.junit.Before;
12+
import org.junit.Test;
13+
import org.junit.runner.RunWith;
14+
import org.powermock.core.classloader.annotations.PowerMockIgnore;
15+
import org.robolectric.RobolectricTestRunner;
16+
import org.robolectric.RuntimeEnvironment;
17+
import org.robolectric.annotation.Config;
18+
import org.robolectric.util.ReflectionHelpers;
19+
20+
import java.util.List;
21+
import java.util.Map;
22+
23+
@RunWith(RobolectricTestRunner.class)
24+
@Config(
25+
sdk = 16,
26+
application = LeanplumTestApp.class
27+
)
28+
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "org.json.*", "org.powermock.*"})
29+
public class RequestOldUtilTest extends TestCase {
30+
private final String POST = "POST";
31+
/**
32+
* Runs before every test case.
33+
*/
34+
@Before
35+
public void setUp() {
36+
Application context = RuntimeEnvironment.application;
37+
assertNotNull(context);
38+
Leanplum.setApplicationContext(context);
39+
40+
// Mock this so async things run synchronously
41+
ReflectionHelpers.setStaticField(Util.class, "singleThreadExecutor", new SynchronousExecutor());
42+
}
43+
44+
@Test
45+
public void testSetNewBatchUUID() {
46+
LeanplumEventDataManager.init(Leanplum.getContext());
47+
48+
RequestOld request1 = new RequestOld(this.POST, Constants.Methods.START, null);
49+
request1.sendEventually();
50+
RequestOld request2 = new RequestOld(this.POST, Constants.Methods.TRACK, null);
51+
request2.sendEventually();
52+
List<Map<String, Object>> unsentRequests1 = request1.getUnsentRequests(1.0);
53+
String oldUUID1 = (String) unsentRequests1.get(0).get(request1.UUID_KEY);
54+
55+
List<Map<String, Object>> unsentRequests2 = request1.getUnsentRequests(1.0);
56+
String oldUUID2 = (String) unsentRequests2.get(0).get(request1.UUID_KEY);
57+
58+
List<Map<String, Object>> unsentRequests3 = request1.getUnsentRequests(0.5);
59+
String newUUID = (String) unsentRequests3.get(0).get(request1.UUID_KEY);
60+
61+
assertTrue(oldUUID1.equals(oldUUID2));
62+
assertFalse(oldUUID1.equals(newUUID));
63+
}
64+
}

0 commit comments

Comments
 (0)