Skip to content

Commit 5234d4f

Browse files
committed
Add more tests.
1 parent 4bbdba8 commit 5234d4f

File tree

2 files changed

+74
-10
lines changed

2 files changed

+74
-10
lines changed

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

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

239+
/** Create HTTP connection and set headers. */
239240
@SuppressLint("VisibleForTests")
240241
public HttpURLConnection createRealtimeConnection() throws IOException {
241242
URL realtimeUrl = getUrl();
@@ -246,7 +247,7 @@ public HttpURLConnection createRealtimeConnection() throws IOException {
246247
return httpURLConnection;
247248
}
248249

249-
// Try to reopen HTTP connection after a random amount of time
250+
/** Retries HTTP stream connection asyncly in random time intervals. */
250251
@SuppressLint("VisibleForTests")
251252
public synchronized void retryHTTPConnection() {
252253
if (canMakeHttpStreamConnection() && httpRetriesRemaining > 0) {
@@ -275,7 +276,12 @@ synchronized void stopRealtime() {
275276
scheduledExecutorService.shutdownNow();
276277
}
277278

278-
private synchronized ConfigAutoFetch startAutoFetch(HttpURLConnection httpURLConnection) {
279+
/**
280+
* Create Autofetch class that listens on HTTP stream for ConfigUpdate messages and calls Fetch
281+
* accordingly.
282+
*/
283+
@SuppressLint("VisibleForTests")
284+
public synchronized ConfigAutoFetch startAutoFetch(HttpURLConnection httpURLConnection) {
279285
ConfigUpdateListener retryCallback =
280286
new ConfigUpdateListener() {
281287
@Override
@@ -300,6 +306,7 @@ public void onError(Exception error) {
300306
return new ConfigAutoFetch(httpURLConnection, configFetchHandler, listeners, retryCallback);
301307
}
302308

309+
// HTTP status code that the Realtime client should retry on.
303310
private boolean isStatusCodeRetryable(int statusCode) {
304311
return statusCode == HttpURLConnection.HTTP_CLIENT_TIMEOUT
305312
|| statusCode == HttpURLConnection.HTTP_BAD_GATEWAY
@@ -323,10 +330,12 @@ public synchronized void beginRealtimeHttpStream() {
323330

324331
int responseCode = 200;
325332
try {
326-
// Create the open the connection.
333+
// Create the connection.
327334
httpURLConnection = createRealtimeConnection();
335+
// Open the connection and get response code.
328336
responseCode = httpURLConnection.getResponseCode();
329337

338+
// If the connection returned a 200 response code state Autofetch.
330339
if (responseCode == HttpURLConnection.HTTP_OK) {
331340
// Reset the retries remaining if we opened the connection without an exception.
332341
resetRetryParameters();
@@ -349,8 +358,9 @@ public synchronized void beginRealtimeHttpStream() {
349358
}
350359
}
351360

352-
// Pauses Http stream listening
353-
synchronized void closeRealtimeHttpStream() {
361+
/** Closes HTTP stream. */
362+
@SuppressLint("VisibleForTests")
363+
public synchronized void closeRealtimeHttpStream() {
354364
if (httpURLConnection != null) {
355365
this.httpURLConnection.disconnect();
356366

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

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@
3030
import static org.mockito.ArgumentMatchers.eq;
3131
import static org.mockito.Matchers.any;
3232
import static org.mockito.Mockito.doAnswer;
33+
import static org.mockito.Mockito.doNothing;
34+
import static org.mockito.Mockito.doReturn;
3335
import static org.mockito.Mockito.doThrow;
3436
import static org.mockito.Mockito.never;
37+
import static org.mockito.Mockito.spy;
3538
import static org.mockito.Mockito.times;
3639
import static org.mockito.Mockito.verify;
3740
import static org.mockito.Mockito.when;
@@ -141,7 +144,7 @@ public final class FirebaseRemoteConfigTest {
141144
@Mock private ConfigMetadataClient metadataClient;
142145

143146
@Mock private ConfigRealtimeHandler mockConfigRealtimeHandler;
144-
@Mock private ConfigRealtimeHttpClient mockConfigRealtimeHttpClient;
147+
@Mock private ConfigAutoFetch mockConfigAutoFetch;
145148
@Mock private ConfigUpdateListenerRegistration mockRealtimeRegistration;
146149
@Mock private HttpURLConnection mockHttpURLConnection;
147150
@Mock private ConfigUpdateListener mockListener;
@@ -167,6 +170,7 @@ public final class FirebaseRemoteConfigTest {
167170
private FetchResponse realtimeFetchedContainerResponse;
168171
private ConfigContainer realtimeFetchedContainer;
169172
private ConfigAutoFetch configAutoFetch;
173+
private ConfigRealtimeHttpClient configRealtimeHttpClient;
170174

171175
private FetchResponse firstFetchedContainerResponse;
172176

@@ -271,6 +275,14 @@ public void setUp() throws Exception {
271275
listeners.add(mockListener);
272276
configAutoFetch =
273277
new ConfigAutoFetch(mockHttpURLConnection, mockFetchHandler, listeners, mockRetryListener);
278+
configRealtimeHttpClient =
279+
new ConfigRealtimeHttpClient(
280+
firebaseApp,
281+
mockFirebaseInstallations,
282+
mockFetchHandler,
283+
context,
284+
"firebase",
285+
listeners);
274286
}
275287

276288
@Test
@@ -1206,11 +1218,53 @@ public void realtime_stream_autofetch_failure() {
12061218
}
12071219

12081220
@Test
1209-
public void realtime_checkStatusCode_beforeRetryStream() throws Exception {
1210-
when(mockConfigRealtimeHttpClient.createRealtimeConnection()).thenReturn(mockHttpURLConnection);
1221+
public void realtime_redirectStatusCode_noRetries() throws Exception {
1222+
ConfigRealtimeHttpClient configRealtimeHttpClientSpy = spy(configRealtimeHttpClient);
1223+
doReturn(mockHttpURLConnection).when(configRealtimeHttpClientSpy).createRealtimeConnection();
1224+
doNothing().when(configRealtimeHttpClientSpy).closeRealtimeHttpStream();
1225+
when(mockHttpURLConnection.getResponseCode()).thenReturn(301);
1226+
configRealtimeHttpClientSpy.beginRealtimeHttpStream();
1227+
verify(configRealtimeHttpClientSpy, never()).startAutoFetch(any());
1228+
verify(configRealtimeHttpClientSpy, never()).retryHTTPConnection();
1229+
}
1230+
1231+
@Test
1232+
public void realtime_okStatusCode_startAutofetchAndRetries() throws Exception {
1233+
ConfigRealtimeHttpClient configRealtimeHttpClientSpy = spy(configRealtimeHttpClient);
1234+
doReturn(mockHttpURLConnection).when(configRealtimeHttpClientSpy).createRealtimeConnection();
1235+
doReturn(mockConfigAutoFetch).when(configRealtimeHttpClientSpy).startAutoFetch(any());
1236+
doNothing().when(configRealtimeHttpClientSpy).retryHTTPConnection();
1237+
doNothing().when(configRealtimeHttpClientSpy).closeRealtimeHttpStream();
1238+
when(mockHttpURLConnection.getResponseCode()).thenReturn(200);
1239+
1240+
configRealtimeHttpClientSpy.beginRealtimeHttpStream();
1241+
verify(mockConfigAutoFetch).listenForNotifications();
1242+
verify(configRealtimeHttpClientSpy).retryHTTPConnection();
1243+
}
1244+
1245+
@Test
1246+
public void realtime_badGatewayStatusCode_noAutofetchButRetries() throws Exception {
1247+
ConfigRealtimeHttpClient configRealtimeHttpClientSpy = spy(configRealtimeHttpClient);
1248+
doReturn(mockHttpURLConnection).when(configRealtimeHttpClientSpy).createRealtimeConnection();
1249+
doNothing().when(configRealtimeHttpClientSpy).retryHTTPConnection();
1250+
doNothing().when(configRealtimeHttpClientSpy).closeRealtimeHttpStream();
12111251
when(mockHttpURLConnection.getResponseCode()).thenReturn(502);
1212-
mockConfigRealtimeHttpClient.beginRealtimeHttpStream();
1213-
verify(mockConfigRealtimeHttpClient, never()).retryHTTPConnection();
1252+
1253+
configRealtimeHttpClientSpy.beginRealtimeHttpStream();
1254+
verify(configRealtimeHttpClientSpy, never()).startAutoFetch(any());
1255+
verify(configRealtimeHttpClientSpy).retryHTTPConnection();
1256+
}
1257+
1258+
@Test
1259+
public void realtime_exceptionThrown_noAutofetchButRetries() throws Exception {
1260+
ConfigRealtimeHttpClient configRealtimeHttpClientSpy = spy(configRealtimeHttpClient);
1261+
doThrow(IOException.class).when(configRealtimeHttpClientSpy).createRealtimeConnection();
1262+
doNothing().when(configRealtimeHttpClientSpy).retryHTTPConnection();
1263+
doNothing().when(configRealtimeHttpClientSpy).closeRealtimeHttpStream();
1264+
1265+
configRealtimeHttpClientSpy.beginRealtimeHttpStream();
1266+
verify(configRealtimeHttpClientSpy, never()).startAutoFetch(any());
1267+
verify(configRealtimeHttpClientSpy).retryHTTPConnection();
12141268
}
12151269

12161270
private static void loadCacheWithConfig(

0 commit comments

Comments
 (0)