Skip to content

Commit 7bce25c

Browse files
Update test and nit changes.
1 parent 923e64e commit 7bce25c

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

firebase-config/src/main/java/com/google/firebase/remoteconfig/internal/ConfigAutoFetch.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,14 @@ public ConfigAutoFetch(
7979
this.random = new Random();
8080
this.isInBackground = false;
8181
this.sharedPrefsClient = sharedPrefsClient;
82-
clock = DefaultClock.getInstance();
82+
this.clock = DefaultClock.getInstance();
8383
}
8484

8585
// Increase the backoff duration with a new end time based on Retry Interval
86-
private synchronized void updateBackoffMetadataWithRetryInterval(int realtimeRetryInterval) {
86+
private synchronized void updateBackoffMetadataWithRetryInterval(
87+
int realtimeRetryIntervalInSeconds) {
8788
Date currentTime = new Date(clock.currentTimeMillis());
88-
long backoffDurationInMillis = realtimeRetryInterval * 1000L;
89+
long backoffDurationInMillis = realtimeRetryIntervalInSeconds * 1000L;
8990
Date backoffEndTime = new Date(currentTime.getTime() + backoffDurationInMillis);
9091

9192
// Persist the new values to disk-backed metadata.
@@ -215,8 +216,8 @@ private void handleNotifications(InputStream inputStream) throws IOException {
215216
// backoff duration without affecting the number of retries, so it will not enter an
216217
// exponential backoff state.
217218
if (jsonObject.has(REALTIME_RETRY_INTERVAL)) {
218-
int realtimeRetryInterval = jsonObject.getInt(REALTIME_RETRY_INTERVAL);
219-
updateBackoffMetadataWithRetryInterval(realtimeRetryInterval);
219+
int realtimeRetryIntervalInSeconds = jsonObject.getInt(REALTIME_RETRY_INTERVAL);
220+
updateBackoffMetadataWithRetryInterval(realtimeRetryIntervalInSeconds);
220221
}
221222
} catch (JSONException ex) {
222223
// Message was mangled up and so it was unable to be parsed. User is notified of this

firebase-config/src/test/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigTest.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
import android.os.Bundle;
5151
import androidx.annotation.NonNull;
5252
import androidx.test.core.app.ApplicationProvider;
53+
import com.google.android.gms.common.util.Clock;
54+
import com.google.android.gms.common.util.DefaultClock;
5355
import com.google.android.gms.shadows.common.internal.ShadowPreconditions;
5456
import com.google.android.gms.tasks.Task;
5557
import com.google.android.gms.tasks.TaskCompletionSource;
@@ -104,6 +106,7 @@
104106
import org.junit.Test;
105107
import org.junit.runner.RunWith;
106108
import org.mockito.ArgumentCaptor;
109+
import org.mockito.ArgumentMatcher;
107110
import org.mockito.Mock;
108111
import org.mockito.MockitoAnnotations;
109112
import org.robolectric.RobolectricTestRunner;
@@ -200,6 +203,7 @@ public final class FirebaseRemoteConfigTest {
200203

201204
private final ScheduledExecutorService scheduledExecutorService =
202205
Executors.newSingleThreadScheduledExecutor();
206+
private final Clock clock = DefaultClock.getInstance();
203207

204208
@Before
205209
public void setUp() throws Exception {
@@ -1556,18 +1560,28 @@ public void realtimeStreamListen_andUnableToParseMessage() throws Exception {
15561560
public void realtime_updatesBackoffMetadataWithProvidedRetryInterval() throws Exception {
15571561
ConfigRealtimeHttpClient configRealtimeHttpClientSpy = spy(configRealtimeHttpClient);
15581562
when(mockHttpURLConnection.getResponseCode()).thenReturn(200);
1559-
int expectedRetryInterval = 240;
1563+
int expectedRetryIntervalInSeconds = 240;
15601564
when(mockHttpURLConnection.getInputStream())
15611565
.thenReturn(
15621566
new ByteArrayInputStream(
15631567
String.format(
15641568
"{ \"latestTemplateVersionNumber\": 1, \"retryIntervalSeconds\": %d }",
1565-
expectedRetryInterval)
1569+
expectedRetryIntervalInSeconds)
15661570
.getBytes(StandardCharsets.UTF_8)));
15671571
when(mockFetchHandler.getTemplateVersionNumber()).thenReturn(1L);
15681572
configAutoFetch.listenForNotifications();
15691573

1570-
verify(sharedPrefsClient, times(1)).setRealtimeBackoffEndTime(any());
1574+
ArgumentMatcher<Date> backoffEndTimeWithinTolerance =
1575+
argument -> {
1576+
Date currentTime = new Date(clock.currentTimeMillis());
1577+
long backoffDurationInMillis = expectedRetryIntervalInSeconds * 1000L;
1578+
Date expectedBackoffEndTime = new Date(currentTime.getTime() + backoffDurationInMillis);
1579+
return Math.abs(argument.getTime() - expectedBackoffEndTime.getTime())
1580+
<= TimeUnit.SECONDS.toSeconds(1);
1581+
};
1582+
1583+
verify(sharedPrefsClient, times(1))
1584+
.setRealtimeBackoffEndTime(argThat(backoffEndTimeWithinTolerance));
15711585
}
15721586

15731587
@Test

0 commit comments

Comments
 (0)