Skip to content

Commit b36a48a

Browse files
author
Sayaan Saha
committed
Added waiter interface for Request.java
1 parent 9d386a8 commit b36a48a

File tree

3 files changed

+116
-23
lines changed

3 files changed

+116
-23
lines changed

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

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.util.ArrayList;
4646
import java.util.Date;
4747
import java.util.HashMap;
48+
import java.util.LinkedList;
4849
import java.util.List;
4950
import java.util.Map;
5051
import java.util.Stack;
@@ -59,6 +60,7 @@ public class Request {
5960
private static final long DEVELOPMENT_MIN_DELAY_MS = 100;
6061
private static final long DEVELOPMENT_MAX_DELAY_MS = 5000;
6162
private static final long PRODUCTION_DELAY = 60000;
63+
private Waiter waiter;
6264
static final int MAX_EVENTS_PER_API_CALL;
6365
static final String LEANPLUM = "__leanplum__";
6466
static final String UUID_KEY = "uuid";
@@ -164,6 +166,28 @@ public static void saveToken() {
164166
SharedPreferencesUtil.commitChanges(editor);
165167
}
166168

169+
private static class NoWaiter implements Waiter {
170+
@Override
171+
public void beforeRead() {
172+
// No op.
173+
}
174+
175+
@Override
176+
public void afterRead() {
177+
// No op.
178+
}
179+
180+
@Override
181+
public void beforeWrite() {
182+
// No op.
183+
}
184+
185+
@Override
186+
public void afterWrite() {
187+
// No op.
188+
}
189+
}
190+
167191
public static String appId() {
168192
return appId;
169193
}
@@ -177,6 +201,10 @@ public static String userId() {
177201
}
178202

179203
public Request(String httpMethod, String apiMethod, Map<String, Object> params) {
204+
this(httpMethod, apiMethod, params, new NoWaiter());
205+
}
206+
207+
Request(String httpMethod, String apiMethod, Map<String, Object> params, Waiter waiter) {
180208
this.httpMethod = httpMethod;
181209
this.apiMethod = apiMethod;
182210
this.params = params != null ? params : new HashMap<String, Object>();
@@ -187,6 +215,7 @@ public Request(String httpMethod, String apiMethod, Map<String, Object> params)
187215
// Make sure the Handler is initialized on the main thread.
188216
OsHandler.getInstance();
189217
dataBaseIndex = -1;
218+
this.waiter = waiter;
190219
}
191220

192221
public static Request get(String apiMethod, Map<String, Object> params) {
@@ -231,28 +260,34 @@ private Map<String, Object> createArgsDictionary() {
231260
}
232261

233262
private void saveRequestForLater(Map<String, Object> args) {
234-
synchronized (Request.class) {
235-
Context context = Leanplum.getContext();
236-
SharedPreferences preferences = context.getSharedPreferences(
237-
LEANPLUM, Context.MODE_PRIVATE);
238-
SharedPreferences.Editor editor = preferences.edit();
239-
long count = LeanplumEventDataManager.getEventsCount();
240-
String uuid = preferences.getString(Constants.Defaults.UUID_KEY, null);
241-
if (uuid == null || count % MAX_EVENTS_PER_API_CALL == 0) {
242-
uuid = UUID.randomUUID().toString();
243-
editor.putString(Constants.Defaults.UUID_KEY, uuid);
244-
SharedPreferencesUtil.commitChanges(editor);
245-
}
246-
args.put(UUID_KEY, uuid);
247-
LeanplumEventDataManager.insertEvent(JsonConverter.toJson(args));
248-
249-
dataBaseIndex = count;
250-
// Checks if here response and/or error callback for this request. We need to add callbacks to
251-
// eventCallbackManager only if here was internet connection, otherwise triggerErrorCallback
252-
// will handle error callback for this event.
253-
if (response != null || error != null && !Util.isConnected()) {
254-
eventCallbackManager.addCallbacks(this, response, error);
263+
try {
264+
waiter.beforeWrite();
265+
synchronized (Request.class) {
266+
Context context = Leanplum.getContext();
267+
SharedPreferences preferences = context.getSharedPreferences(
268+
LEANPLUM, Context.MODE_PRIVATE);
269+
SharedPreferences.Editor editor = preferences.edit();
270+
long count = LeanplumEventDataManager.getEventsCount();
271+
String uuid = preferences.getString(Constants.Defaults.UUID_KEY, null);
272+
if (uuid == null || count % MAX_EVENTS_PER_API_CALL == 0) {
273+
uuid = UUID.randomUUID().toString();
274+
editor.putString(Constants.Defaults.UUID_KEY, uuid);
275+
SharedPreferencesUtil.commitChanges(editor);
276+
}
277+
args.put(UUID_KEY, uuid);
278+
LeanplumEventDataManager.insertEvent(JsonConverter.toJson(args));
279+
280+
dataBaseIndex = count;
281+
// Checks if here response and/or error callback for this request. We need to add callbacks to
282+
// eventCallbackManager only if here was internet connection, otherwise triggerErrorCallback
283+
// will handle error callback for this event.
284+
if (response != null || error != null && !Util.isConnected()) {
285+
eventCallbackManager.addCallbacks(this, response, error);
286+
}
287+
waiter.afterWrite();
255288
}
289+
} catch (Throwable t) {
290+
Util.handleException(t);
256291
}
257292
}
258293

@@ -454,13 +489,14 @@ private void sendNow() {
454489
return;
455490
}
456491

457-
this.sendEventually();
492+
//use something like a callback interface and pass it through here
493+
this.sendEventually(); //write
458494

459495
Util.executeAsyncTask(true, new AsyncTask<Void, Void, Void>() {
460496
@Override
461497
protected Void doInBackground(Void... params) {
462498
try {
463-
sendRequests();
499+
sendRequests(); // read
464500
} catch (Throwable t) {
465501
Util.handleException(t);
466502
}
@@ -545,6 +581,7 @@ private RequestsWithEncoding getRequestsWithEncodedString() {
545581
}
546582

547583
private void sendRequests() {
584+
waiter.beforeRead();
548585
RequestsWithEncoding requestsWithEncoding = getRequestsWithEncodedString();
549586

550587
List<Map<String, Object>> unsentRequests = requestsWithEncoding.unsentRequests;
@@ -611,6 +648,7 @@ private void sendRequests() {
611648
parseResponseBody(responseBody, requestsToSend, errorException, unsentRequests.size());
612649
}
613650
}
651+
waiter.afterRead();
614652
} catch (JSONException e) {
615653
Log.e("Error parsing JSON response: " + e.toString() + "\n" + Log.getStackTraceString(e));
616654
deleteSentRequests(unsentRequests.size());
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.leanplum.internal;
2+
3+
/**
4+
* Created by sayaan on 6/28/18.
5+
*/
6+
7+
public interface Waiter {
8+
void beforeRead();
9+
10+
void afterRead();
11+
12+
void beforeWrite();
13+
14+
void afterWrite();
15+
}

AndroidSDKTests/src/test/java/com/leanplum/internal/RequestTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,14 @@
3737

3838
import java.lang.reflect.InvocationTargetException;
3939
import java.lang.reflect.Method;
40+
import java.sql.Time;
41+
import java.time.Instant;
4042
import java.util.ArrayList;
43+
import java.util.Date;
4144
import java.util.HashMap;
4245
import java.util.List;
4346
import java.util.Map;
47+
import java.util.concurrent.CountDownLatch;
4448

4549
import static org.mockito.AdditionalMatchers.not;
4650
import static org.mockito.Matchers.eq;
@@ -67,6 +71,42 @@ public void setUp() {
6771
Leanplum.setApplicationContext(context);
6872
}
6973

74+
/**
75+
* Test that read writes happened sequentially when calling sendNow().
76+
*/
77+
@Test
78+
public void testMultiThreaded() {
79+
Map<String, Object> params = new HashMap<>();
80+
params.put("data1", "value1");
81+
params.put("data2", "value2");
82+
Waiter waiter = new Waiter() {
83+
public Instant t1, t2, t3, t4;
84+
@Override
85+
public void beforeRead() {
86+
t1 = Instant.now();
87+
}
88+
89+
@Override
90+
public void afterRead() {
91+
t2 = Instant.now();
92+
}
93+
94+
@Override
95+
public void beforeWrite() {
96+
t3 = Instant.now();
97+
}
98+
99+
@Override
100+
public void afterWrite() {
101+
t4 = Instant.now();
102+
}
103+
};
104+
Request request = new Request("POST", Constants.Methods.START, params, waiter);
105+
request.setAppId("fskadfshdbfa", "weew22323");
106+
request.sendIfConnected();
107+
108+
109+
}
70110
/**
71111
* Tests the testRemoveIrrelevantBackgroundStartRequests method.
72112
* <p>

0 commit comments

Comments
 (0)