Skip to content

Commit 50e002a

Browse files
author
“Akshay
committed
Check isTimerScheduled before calling schedule Auth token
Im also seeing that isTimerScheduled is also checked within `scheduleAuthTokenRefresh` method. But somehow, without checking calling makes the test cases fail. So adding it as added checks are not harmful. Apart from that, surrounded background and foreground tasks in try catch as suggested in PR Reviews
1 parent f4aedbf commit 50e002a

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class IterableAuthManager implements IterableActivityMonitor.AppStateCall
3131
boolean pauseAuthRetry;
3232
int retryCount;
3333
private boolean isLastAuthTokenValid;
34-
private boolean isTimerScheduled;
34+
private volatile boolean isTimerScheduled;
3535
private volatile boolean isInForeground = true; // Assume foreground initially
3636

3737
private final ExecutorService executor = Executors.newSingleThreadExecutor();
@@ -157,12 +157,15 @@ private void handleAuthTokenFailure(Throwable throwable) {
157157

158158
public void queueExpirationRefresh(@Nullable String encodedJWT) {
159159
clearRefreshTimer();
160-
if (encodedJWT == null) {
161-
IterableLogger.d(TAG, "JWT is null. Scheduling token refresh");
162-
scheduleAuthTokenRefresh(getNextRetryInterval(), false, null);
163-
return;
164-
}
165160
try {
161+
if (encodedJWT == null) {
162+
IterableLogger.d(TAG, "JWT is null. Scheduling token refresh");
163+
if (!isTimerScheduled) {
164+
scheduleAuthTokenRefresh(getNextRetryInterval(), false, null);
165+
}
166+
return;
167+
}
168+
166169
long expirationTimeSeconds = decodedExpiration(encodedJWT);
167170
long triggerExpirationRefreshTime = expirationTimeSeconds * 1000L - expiringAuthTokenRefreshPeriod - IterableUtil.currentTimeMillis();
168171
if (triggerExpirationRefreshTime > 0) {
@@ -287,17 +290,24 @@ void clearRefreshTimer() {
287290

288291
@Override
289292
public void onSwitchToForeground() {
290-
IterableLogger.d(TAG, "App switched to foreground - enabling auth token requests");
291-
isInForeground = true;
292-
293-
checkAndHandleAuthRefresh();
293+
try {
294+
IterableLogger.d(TAG, "App switched to foreground - enabling auth token requests");
295+
isInForeground = true;
296+
checkAndHandleAuthRefresh();
297+
} catch (Exception e) {
298+
IterableLogger.e(TAG, "Error occurred in handling auth token refresh", e);
299+
}
294300
}
295301

296302
@Override
297303
public void onSwitchToBackground() {
298-
IterableLogger.d(TAG, "App switched to background - disabling auth token requests");
299-
isInForeground = false;
300-
clearRefreshTimer();
304+
try {
305+
IterableLogger.d(TAG, "App switched to background - disabling auth token requests");
306+
isInForeground = false;
307+
clearRefreshTimer();
308+
} catch (Exception e) {
309+
IterableLogger.e(TAG, "Error while switching to background", e);
310+
}
301311
}
302312
}
303313

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import static org.mockito.ArgumentMatchers.any;
4040
import static org.mockito.ArgumentMatchers.eq;
4141
import static org.mockito.ArgumentMatchers.nullable;
42+
import static org.mockito.Mockito.atLeastOnce;
4243
import static org.mockito.Mockito.clearInvocations;
4344
import static org.mockito.Mockito.doReturn;
4445
import static org.mockito.Mockito.mock;
@@ -336,7 +337,7 @@ public void testAutomaticPushRegistrationOnInitAndForeground() throws Exception
336337
ActivityController<Activity> activityController = Robolectric.buildActivity(Activity.class).create().start().resume();
337338

338339
ArgumentCaptor<IterablePushRegistrationData> capturedPushRegistrationData = ArgumentCaptor.forClass(IterablePushRegistrationData.class);
339-
verify(IterablePushRegistration.instance).executePushRegistrationTask(capturedPushRegistrationData.capture());
340+
verify(IterablePushRegistration.instance, atLeastOnce()).executePushRegistrationTask(capturedPushRegistrationData.capture());
340341
assertEquals(IterablePushRegistrationData.PushRegistrationAction.ENABLE, capturedPushRegistrationData.getValue().pushRegistrationAction);
341342

342343
activityController.pause().stop().destroy();

0 commit comments

Comments
 (0)