Skip to content

Commit 4bbdba8

Browse files
committed
Add tests.
1 parent 0092415 commit 4bbdba8

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ private URL getUrl() {
236236
return realtimeURL;
237237
}
238238

239-
private HttpURLConnection createRealtimeConnection() throws IOException {
239+
@SuppressLint("VisibleForTests")
240+
public HttpURLConnection createRealtimeConnection() throws IOException {
240241
URL realtimeUrl = getUrl();
241242
HttpURLConnection httpURLConnection = (HttpURLConnection) realtimeUrl.openConnection();
242243
setCommonRequestHeaders(httpURLConnection);
@@ -246,7 +247,8 @@ private HttpURLConnection createRealtimeConnection() throws IOException {
246247
}
247248

248249
// Try to reopen HTTP connection after a random amount of time
249-
private synchronized void retryHTTPConnection() {
250+
@SuppressLint("VisibleForTests")
251+
public synchronized void retryHTTPConnection() {
250252
if (canMakeHttpStreamConnection() && httpRetriesRemaining > 0) {
251253
if (httpRetriesRemaining < ORIGINAL_RETRIES) {
252254
httpRetrySeconds *= getRetryMultiplier();
@@ -314,7 +316,7 @@ private boolean isStatusCodeRetryable(int statusCode) {
314316
* chunk-encoded HTTP body. When the connection closes, it attempts to reestablish the stream.
315317
*/
316318
@SuppressLint("VisibleForTests")
317-
synchronized void beginRealtimeHttpStream() {
319+
public synchronized void beginRealtimeHttpStream() {
318320
if (!canMakeHttpStreamConnection()) {
319321
return;
320322
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import com.google.firebase.remoteconfig.internal.ConfigGetParameterHandler;
6666
import com.google.firebase.remoteconfig.internal.ConfigMetadataClient;
6767
import com.google.firebase.remoteconfig.internal.ConfigRealtimeHandler;
68+
import com.google.firebase.remoteconfig.internal.ConfigRealtimeHttpClient;
6869
import com.google.firebase.remoteconfig.internal.Personalization;
6970
import java.io.ByteArrayInputStream;
7071
import java.io.IOException;
@@ -140,6 +141,7 @@ public final class FirebaseRemoteConfigTest {
140141
@Mock private ConfigMetadataClient metadataClient;
141142

142143
@Mock private ConfigRealtimeHandler mockConfigRealtimeHandler;
144+
@Mock private ConfigRealtimeHttpClient mockConfigRealtimeHttpClient;
143145
@Mock private ConfigUpdateListenerRegistration mockRealtimeRegistration;
144146
@Mock private HttpURLConnection mockHttpURLConnection;
145147
@Mock private ConfigUpdateListener mockListener;
@@ -1203,6 +1205,14 @@ public void realtime_stream_autofetch_failure() {
12031205
verify(mockListener).onError(any(FirebaseRemoteConfigRealtimeUpdateFetchException.class));
12041206
}
12051207

1208+
@Test
1209+
public void realtime_checkStatusCode_beforeRetryStream() throws Exception {
1210+
when(mockConfigRealtimeHttpClient.createRealtimeConnection()).thenReturn(mockHttpURLConnection);
1211+
when(mockHttpURLConnection.getResponseCode()).thenReturn(502);
1212+
mockConfigRealtimeHttpClient.beginRealtimeHttpStream();
1213+
verify(mockConfigRealtimeHttpClient, never()).retryHTTPConnection();
1214+
}
1215+
12061216
private static void loadCacheWithConfig(
12071217
ConfigCacheClient cacheClient, ConfigContainer container) {
12081218
when(cacheClient.getBlocking()).thenReturn(container);

0 commit comments

Comments
 (0)