Skip to content

Commit e32b4b5

Browse files
committed
Revision 4
Adding suggested changes from PR reviews Also added error check while creatin-> inserting task into DB
1 parent ba681fe commit e32b4b5

File tree

7 files changed

+40
-98
lines changed

7 files changed

+40
-98
lines changed

iterableapi/src/main/java/com/iterable/iterableapi/HealthMonitor.java

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,66 +3,40 @@
33
public class HealthMonitor implements IterableTaskStorage.IterableDatabaseStatusListeners {
44
private static final String TAG = "HealthMonitor";
55

6-
private boolean errored = false;
6+
private boolean databaseErrored = false;
77

88
private IterableTaskStorage iterableTaskStorage;
99

1010
public HealthMonitor(IterableTaskStorage storage) {
1111
this.iterableTaskStorage = storage;
12-
this.iterableTaskStorage.addDataBaseListener(this);
12+
this.iterableTaskStorage.addDatabaseStatusListener(this);
1313
}
1414

1515
public boolean canSchedule() {
1616
IterableLogger.d(TAG, "canSchedule");
1717
try {
18-
return !(iterableTaskStorage.numberOfTasks() >= IterableConstants.MAX_OFFLINE_OPERATION);
19-
} catch (Exception e) {
18+
return !(iterableTaskStorage.getNumberOfTasks() >= IterableConstants.OFFLINE_TASKS_LIMIT);
19+
} catch (IllegalStateException e) {
2020
IterableLogger.e(TAG, e.getLocalizedMessage());
21-
errored = true;
21+
databaseErrored = true;
2222
}
2323
return false;
2424
}
2525

2626
public boolean canProcess() {
27-
IterableLogger.d(TAG, "Health monitor can process: " + !errored);
28-
return !errored;
29-
}
30-
31-
public void onScheduleError() {
32-
IterableLogger.w(TAG, "onScheduleError");
33-
}
34-
35-
void onNextTaskError() {
36-
IterableLogger.w(TAG, "onNextTaskError");
37-
onDBError();
38-
}
39-
40-
void onDeleteAllTasksError() {
41-
IterableLogger.w(TAG, "onDeleteAllTasksError");
42-
onDBError();
43-
}
44-
45-
@Override
46-
public void isNotReady() {
47-
IterableLogger.v(TAG, "DB Not ready notified to healthMonitor");
48-
errored = true;
27+
IterableLogger.d(TAG, "Health monitor can process: " + !databaseErrored);
28+
return !databaseErrored;
4929
}
5030

5131
@Override
5232
public void onDBError() {
53-
IterableLogger.v(TAG, "DB Error notified to healthMonitor");
54-
errored = true;
55-
}
56-
57-
@Override
58-
public void isClosed() {
59-
IterableLogger.v(TAG, "DB Closed notified to healthMonitor");
60-
errored = true;
33+
IterableLogger.e(TAG, "DB Error notified to healthMonitor");
34+
databaseErrored = true;
6135
}
6236

6337
@Override
6438
public void isReady() {
6539
IterableLogger.v(TAG, "DB Ready notified to healthMonitor");
66-
errored = false;
40+
databaseErrored = false;
6741
}
6842
}

iterableapi/src/main/java/com/iterable/iterableapi/IterableConstants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public final class IterableConstants {
236236
public static final String ITERABLE_IN_APP_ACTION_DELETE = "delete";
237237

238238
//Offline operation
239-
public static final long MAX_OFFLINE_OPERATION = 1000;
239+
public static final long OFFLINE_TASKS_LIMIT = 1000;
240240

241241
// URL schemes
242242
public static final String URL_SCHEME_ITBL = "itbl://";

iterableapi/src/main/java/com/iterable/iterableapi/IterableTaskRunner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ private boolean processTask(@NonNull IterableTask task) {
144144
response = IterableRequestTask.executeApiRequest(request);
145145
} catch (Exception e) {
146146
IterableLogger.e(TAG, "Error while processing request task", e);
147-
healthMonitor.onNextTaskError();
147+
healthMonitor.onDBError();
148148
}
149149

150150
if (response != null) {

iterableapi/src/main/java/com/iterable/iterableapi/IterableTaskStorage.java

Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ void addTaskCreatedListener(TaskCreatedListener listener) {
9393
taskCreatedListeners.add(listener);
9494
}
9595

96-
void removeTaskCreatedListener(TaskCreatedListener listener) {
96+
void removeDatabaseStatusListener(TaskCreatedListener listener) {
9797
taskCreatedListeners.remove(listener);
9898
}
9999

@@ -139,7 +139,11 @@ String createTask(String name, IterableTaskType type, String data) {
139139
contentValues.put(TYPE, iterableTask.taskType.toString());
140140
contentValues.put(ATTEMPTS, iterableTask.attempts);
141141

142-
database.insert(ITERABLE_TASK_TABLE_NAME, null, contentValues);
142+
long rowId = database.insert(ITERABLE_TASK_TABLE_NAME, null, contentValues);
143+
if (rowId == -1) {
144+
notifyDBError();
145+
return null;
146+
}
143147
contentValues.clear();
144148

145149
// Call through Handler to make sure we don't call the listeners immediately, as the caller may need additional processing
@@ -255,9 +259,8 @@ ArrayList<String> getAllTaskIds() {
255259
return taskIds;
256260
}
257261

258-
long numberOfTasks() throws Exception {
262+
long getNumberOfTasks() throws IllegalStateException {
259263
if (!isDatabaseReady()) {
260-
notifyDBNotReady();
261264
throw new IllegalStateException("Database is not ready");
262265
}
263266
return DatabaseUtils.queryNumEntries(database, ITERABLE_TASK_TABLE_NAME);
@@ -456,57 +459,34 @@ private boolean updateTaskWithContentValues(String id, ContentValues contentValu
456459
}
457460

458461
private boolean isDatabaseReady() {
459-
if (database == null) {
460-
notifyDBNotReady();
461-
IterableLogger.e(TAG, "Database not initialized");
462-
return false;
463-
}
464-
465-
if (!database.isOpen()) {
466-
notifyDBClosed();
467-
IterableLogger.e(TAG, "Database is closed");
462+
if (database == null || !database.isOpen()) {
463+
notifyDBError();
464+
IterableLogger.e(TAG, "Database not initialized or is closed");
468465
return false;
469466
}
470-
471467
return true;
472468
}
473469

474470
public interface IterableDatabaseStatusListeners {
475-
void isNotReady();
476-
477471
void onDBError();
478-
479-
void isClosed();
480-
481472
void isReady();
482473
}
483474

484475
private ArrayList<IterableDatabaseStatusListeners> databaseStatusListeners = new ArrayList<>();
485476

486-
void addDataBaseListener(IterableDatabaseStatusListeners listener) {
477+
void addDatabaseStatusListener(IterableDatabaseStatusListeners listener) {
487478
if (isDatabaseReady()) {
488479
listener.isReady();
489480
} else {
490-
listener.isNotReady();
481+
listener.onDBError();
491482
}
492483
databaseStatusListeners.add(listener);
493484
}
494485

495-
void removeTaskCreatedListener(IterableDatabaseStatusListeners listener) {
486+
void removeDatabaseStatusListener(IterableDatabaseStatusListeners listener) {
496487
databaseStatusListeners.remove(listener);
497488
}
498489

499-
private void notifyDBClosed() {
500-
new Handler(Looper.getMainLooper()).post(new Runnable() {
501-
@Override
502-
public void run() {
503-
for (IterableDatabaseStatusListeners listener : databaseStatusListeners) {
504-
listener.isClosed();
505-
}
506-
}
507-
});
508-
}
509-
510490
private void notifyDBError() {
511491
new Handler(Looper.getMainLooper()).post(new Runnable() {
512492
@Override
@@ -517,26 +497,4 @@ public void run() {
517497
}
518498
});
519499
}
520-
521-
private void notifyDBReady() {
522-
new Handler(Looper.getMainLooper()).post(new Runnable() {
523-
@Override
524-
public void run() {
525-
for (IterableDatabaseStatusListeners listener : databaseStatusListeners) {
526-
listener.isReady();
527-
}
528-
}
529-
});
530-
}
531-
532-
private void notifyDBNotReady() {
533-
new Handler(Looper.getMainLooper()).post(new Runnable() {
534-
@Override
535-
public void run() {
536-
for (IterableDatabaseStatusListeners listener : databaseStatusListeners) {
537-
listener.isNotReady();
538-
}
539-
}
540-
});
541-
}
542500
}

iterableapi/src/main/java/com/iterable/iterableapi/OfflineRequestProcessor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ boolean isRequestOfflineCompatible(String baseUrl) {
7878
}
7979
}
8080

81-
//Mock TaskScheduler for testing purpose.
8281
class TaskScheduler implements IterableTaskRunner.TaskCompletedListener {
8382
static HashMap<String, IterableHelper.SuccessHandler> successCallbackMap = new HashMap<>();
8483
static HashMap<String, IterableHelper.FailureHandler> failureCallbackMap = new HashMap<>();
@@ -102,7 +101,10 @@ void scheduleTask(IterableApiRequest request, @Nullable IterableHelper.SuccessHa
102101
}
103102

104103
String taskId = taskStorage.createTask(request.resourcePath, IterableTaskType.API, serializedRequest.toString());
105-
104+
if (taskId == null) {
105+
new IterableRequestTask().execute(request);
106+
return;
107+
}
106108
successCallbackMap.put(taskId, onSuccess);
107109
failureCallbackMap.put(taskId, onFailure);
108110
}

iterableapi/src/test/java/com/iterable/iterableapi/HealthMonitorTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ public void setUp() {
2727
@Test
2828
public void canScheduleFailWhenMaxCountReached() throws Exception {
2929
HealthMonitor healthMonitor = new HealthMonitor(mockTaskStorage);
30-
when(mockTaskStorage.numberOfTasks()).thenReturn(IterableConstants.MAX_OFFLINE_OPERATION);
30+
when(mockTaskStorage.getNumberOfTasks()).thenReturn(IterableConstants.OFFLINE_TASKS_LIMIT);
3131
assertFalse(healthMonitor.canSchedule());
3232
}
3333

3434
@Test
3535
public void canScheduleWhenMaxCountNotReached() throws Exception {
3636
HealthMonitor healthMonitor = new HealthMonitor(mockTaskStorage);
37-
when(mockTaskStorage.numberOfTasks()).thenReturn(IterableConstants.MAX_OFFLINE_OPERATION - 1);
37+
when(mockTaskStorage.getNumberOfTasks()).thenReturn(IterableConstants.OFFLINE_TASKS_LIMIT - 1);
3838
assertTrue(healthMonitor.canSchedule());
3939
}
4040

@@ -48,7 +48,7 @@ public void canProcessReturnTrueIfDBok() throws Exception {
4848
public void canProcessReturnFalseIfDBError() throws Exception {
4949
IterableTaskStorage taskStorage = IterableTaskStorage.sharedInstance(getContext());
5050
HealthMonitor healthMonitor = new HealthMonitor(taskStorage);
51-
healthMonitor.onNextTaskError();
51+
healthMonitor.onDBError();
5252
assertFalse(healthMonitor.canProcess());
5353
}
5454
}

iterableapi/src/test/java/com/iterable/iterableapi/OfflineRequestProcessorTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ public void testNonOfflineRequestIsNotStored() {
4747
verifyNoInteractions(mockTaskScheduler);
4848
}
4949

50+
@Test
51+
public void testOnlineRequestWhenDBError() {
52+
IterableApiRequest request = new IterableApiRequest("apiKey", IterableConstants.ENDPOINT_TRACK_INAPP_CLICK, new JSONObject(), "POST", null, null, null);
53+
when(mockHealthMonitor.canSchedule()).thenReturn(false);
54+
offlineRequestProcessor.processPostRequest(request.apiKey, request.resourcePath, request.json, request.authToken, request.successCallback, request.failureCallback);
55+
verifyNoInteractions(mockTaskScheduler);
56+
}
57+
5058
@Test
5159
public void testAllOfflineApisUseTaskScheduler() {
5260
String[] offlineApis = new String[]{

0 commit comments

Comments
 (0)