|
50 | 50 | import android.os.Bundle; |
51 | 51 | import androidx.annotation.NonNull; |
52 | 52 | import androidx.test.core.app.ApplicationProvider; |
| 53 | +import com.google.android.gms.common.util.Clock; |
| 54 | +import com.google.android.gms.common.util.DefaultClock; |
53 | 55 | import com.google.android.gms.shadows.common.internal.ShadowPreconditions; |
54 | 56 | import com.google.android.gms.tasks.Task; |
55 | 57 | import com.google.android.gms.tasks.TaskCompletionSource; |
|
104 | 106 | import org.junit.Test; |
105 | 107 | import org.junit.runner.RunWith; |
106 | 108 | import org.mockito.ArgumentCaptor; |
| 109 | +import org.mockito.ArgumentMatcher; |
107 | 110 | import org.mockito.Mock; |
108 | 111 | import org.mockito.MockitoAnnotations; |
109 | 112 | import org.robolectric.RobolectricTestRunner; |
@@ -200,6 +203,7 @@ public final class FirebaseRemoteConfigTest { |
200 | 203 |
|
201 | 204 | private final ScheduledExecutorService scheduledExecutorService = |
202 | 205 | Executors.newSingleThreadScheduledExecutor(); |
| 206 | + private final Clock clock = DefaultClock.getInstance(); |
203 | 207 |
|
204 | 208 | @Before |
205 | 209 | public void setUp() throws Exception { |
@@ -1556,18 +1560,28 @@ public void realtimeStreamListen_andUnableToParseMessage() throws Exception { |
1556 | 1560 | public void realtime_updatesBackoffMetadataWithProvidedRetryInterval() throws Exception { |
1557 | 1561 | ConfigRealtimeHttpClient configRealtimeHttpClientSpy = spy(configRealtimeHttpClient); |
1558 | 1562 | when(mockHttpURLConnection.getResponseCode()).thenReturn(200); |
1559 | | - int expectedRetryInterval = 240; |
| 1563 | + int expectedRetryIntervalInSeconds = 240; |
1560 | 1564 | when(mockHttpURLConnection.getInputStream()) |
1561 | 1565 | .thenReturn( |
1562 | 1566 | new ByteArrayInputStream( |
1563 | 1567 | String.format( |
1564 | 1568 | "{ \"latestTemplateVersionNumber\": 1, \"retryIntervalSeconds\": %d }", |
1565 | | - expectedRetryInterval) |
| 1569 | + expectedRetryIntervalInSeconds) |
1566 | 1570 | .getBytes(StandardCharsets.UTF_8))); |
1567 | 1571 | when(mockFetchHandler.getTemplateVersionNumber()).thenReturn(1L); |
1568 | 1572 | configAutoFetch.listenForNotifications(); |
1569 | 1573 |
|
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)); |
1571 | 1585 | } |
1572 | 1586 |
|
1573 | 1587 | @Test |
|
0 commit comments