Skip to content

Commit 5940212

Browse files
author
Sayaan Saha
committed
Added javadocs renamed RequestSequence to RequestSequenceRecorder
1 parent 1af5c2e commit 5940212

File tree

4 files changed

+48
-36
lines changed

4 files changed

+48
-36
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public class Request {
5959
private static final long DEVELOPMENT_MIN_DELAY_MS = 100;
6060
private static final long DEVELOPMENT_MAX_DELAY_MS = 5000;
6161
private static final long PRODUCTION_DELAY = 60000;
62-
private RequestSequence requestSequence;
62+
private RequestSequenceRecorder requestSequenceRecorder;
6363
static final int MAX_EVENTS_PER_API_CALL;
6464
static final String LEANPLUM = "__leanplum__";
6565
static final String UUID_KEY = "uuid";
@@ -165,7 +165,7 @@ public static void saveToken() {
165165
SharedPreferencesUtil.commitChanges(editor);
166166
}
167167

168-
private static class NoRequestSequence implements RequestSequence {
168+
private static class NoRequestSequenceRecorder implements RequestSequenceRecorder {
169169
@Override
170170
public void beforeRead() {
171171
// No op.
@@ -200,10 +200,10 @@ public static String userId() {
200200
}
201201

202202
public Request(String httpMethod, String apiMethod, Map<String, Object> params) {
203-
this(httpMethod, apiMethod, params, new NoRequestSequence());
203+
this(httpMethod, apiMethod, params, new NoRequestSequenceRecorder());
204204
}
205205

206-
Request(String httpMethod, String apiMethod, Map<String, Object> params, RequestSequence requestSequence) {
206+
Request(String httpMethod, String apiMethod, Map<String, Object> params, RequestSequenceRecorder requestSequenceRecorder) {
207207
this.httpMethod = httpMethod;
208208
this.apiMethod = apiMethod;
209209
this.params = params != null ? params : new HashMap<String, Object>();
@@ -214,7 +214,7 @@ public Request(String httpMethod, String apiMethod, Map<String, Object> params)
214214
// Make sure the Handler is initialized on the main thread.
215215
OsHandler.getInstance();
216216
dataBaseIndex = -1;
217-
this.requestSequence = requestSequence;
217+
this.requestSequenceRecorder = requestSequenceRecorder;
218218
}
219219

220220
public static Request get(String apiMethod, Map<String, Object> params) {
@@ -260,7 +260,7 @@ private Map<String, Object> createArgsDictionary() {
260260

261261
private void saveRequestForLater(Map<String, Object> args) {
262262
try {
263-
requestSequence.beforeWrite();
263+
requestSequenceRecorder.beforeWrite();
264264

265265
synchronized (Request.class) {
266266
Context context = Leanplum.getContext();
@@ -286,7 +286,7 @@ private void saveRequestForLater(Map<String, Object> args) {
286286
}
287287
}
288288

289-
requestSequence.afterWrite();
289+
requestSequenceRecorder.afterWrite();
290290
} catch (Throwable t) {
291291
Util.handleException(t);
292292
}
@@ -581,11 +581,11 @@ private RequestsWithEncoding getRequestsWithEncodedString() {
581581
}
582582

583583
private void sendRequests() {
584-
requestSequence.beforeRead();
584+
requestSequenceRecorder.beforeRead();
585585

586586
RequestsWithEncoding requestsWithEncoding = getRequestsWithEncodedString();
587587

588-
requestSequence.afterRead();
588+
requestSequenceRecorder.afterRead();
589589

590590
List<Map<String, Object>> unsentRequests = requestsWithEncoding.unsentRequests;
591591
List<Map<String, Object>> requestsToSend = requestsWithEncoding.requestsToSend;

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

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.leanplum.internal;
2+
3+
/**
4+
* Records request call sequence of read/write operations to database.
5+
*
6+
*/
7+
8+
public interface RequestSequenceRecorder {
9+
/**
10+
* Executes before database read in Request.
11+
*/
12+
void beforeRead();
13+
14+
/**
15+
* Executes after database read in Request.
16+
*/
17+
void afterRead();
18+
19+
/**
20+
* Executes before database write in Request.
21+
*/
22+
void beforeWrite();
23+
24+
/**
25+
* Executes after database write in Request.
26+
*/
27+
void afterWrite();
28+
}

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ public void shouldWriteRequestAndSendInSequence() throws InterruptedException {
8080
Map<String, Object> params = new HashMap<>();
8181
params.put("data1", "value1");
8282
params.put("data2", "value2");
83-
final ThreadRequestSequence threadRequestSequence = new ThreadRequestSequence();
84-
Request request = new Request(POST, Constants.Methods.START, params, threadRequestSequence);
83+
final ThreadRequestSequenceRecorder threadRequestSequenceRecorder = new ThreadRequestSequenceRecorder();
84+
Request request = new Request(POST, Constants.Methods.START, params, threadRequestSequenceRecorder);
8585
request.setAppId("fskadfshdbfa", "wee5w4waer422323");
8686

8787
new Thread(new Runnable() {
@@ -92,17 +92,17 @@ public void run() {
9292
} catch (InterruptedException e) {
9393
throw new RuntimeException(e);
9494
}
95-
threadRequestSequence.writeSemaphore.release(1);
95+
threadRequestSequenceRecorder.writeSemaphore.release(1);
9696
}
9797
}).start();
9898

99-
// When the request is sent.
99+
// Then the request is written to the local db first, and then read and sent.
100100
request.sendIfConnected();
101101

102-
threadRequestSequence.testThreadSemaphore.tryAcquire(5000, TimeUnit.MILLISECONDS);
102+
threadRequestSequenceRecorder.testThreadSemaphore.tryAcquire(5000, TimeUnit.MILLISECONDS);
103103

104104
// When the request is sent.
105-
threadRequestSequence.assertCallSequence();
105+
threadRequestSequenceRecorder.assertCallSequence();
106106
}
107107
/**
108108
* Tests the testRemoveIrrelevantBackgroundStartRequests method.
@@ -360,7 +360,7 @@ private List<Map<String, Object>> mockRequests(int requestSize) {
360360
return requests;
361361
}
362362

363-
private static class ThreadRequestSequence implements RequestSequence {
363+
private static class ThreadRequestSequenceRecorder implements RequestSequenceRecorder {
364364
Instant beforeReadTime, afterReadTime, beforeWriteTime, afterWriteTime;
365365
final Semaphore writeSemaphore = new Semaphore(0);
366366
final Semaphore readSemaphore = new Semaphore(1);
@@ -369,8 +369,10 @@ private static class ThreadRequestSequence implements RequestSequence {
369369
@Override
370370
public void beforeRead() {
371371
try {
372-
readSemaphore.tryAcquire();
372+
readSemaphore.tryAcquire(10, TimeUnit.SECONDS);
373373
beforeReadTime = Instant.now();
374+
} catch (InterruptedException e) {
375+
throw new RuntimeException(e);
374376
} finally {
375377
readSemaphore.release();
376378
}
@@ -386,7 +388,7 @@ public void afterRead() {
386388
public void beforeWrite() {
387389
// since we are blocking on main thread
388390
try {
389-
writeSemaphore.tryAcquire();
391+
writeSemaphore.tryAcquire(10, TimeUnit.SECONDS);
390392
Thread.sleep(2000);
391393
} catch (InterruptedException e) {
392394
throw new RuntimeException(e);

0 commit comments

Comments
 (0)