Skip to content

Commit e2894dc

Browse files
committed
Updated tests and comments based on review
1 parent c3a069f commit e2894dc

File tree

5 files changed

+47
-29
lines changed

5 files changed

+47
-29
lines changed

firebase-config/api.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,12 @@ package com.google.firebase.remoteconfig {
5757
ctor public FirebaseRemoteConfigException(@NonNull String, @Nullable Throwable);
5858
ctor public FirebaseRemoteConfigException(@NonNull String, @NonNull com.google.firebase.remoteconfig.FirebaseRemoteConfigException.Code);
5959
ctor public FirebaseRemoteConfigException(@NonNull String, @Nullable Throwable, @NonNull com.google.firebase.remoteconfig.FirebaseRemoteConfigException.Code);
60+
method @NonNull public com.google.firebase.remoteconfig.FirebaseRemoteConfigException.Code getCode();
6061
}
6162

6263
public enum FirebaseRemoteConfigException.Code {
6364
method public int value();
64-
enum_constant public static final com.google.firebase.remoteconfig.FirebaseRemoteConfigException.Code CONFIG_UPDATE_MESSAGE_UNAVAILABLE;
65+
enum_constant public static final com.google.firebase.remoteconfig.FirebaseRemoteConfigException.Code CONFIG_UPDATE_MESSAGE_INVALID;
6566
enum_constant public static final com.google.firebase.remoteconfig.FirebaseRemoteConfigException.Code CONFIG_UPDATE_NOT_FETCHED;
6667
enum_constant public static final com.google.firebase.remoteconfig.FirebaseRemoteConfigException.Code CONFIG_UPDATE_STREAM_ERROR;
6768
enum_constant public static final com.google.firebase.remoteconfig.FirebaseRemoteConfigException.Code CONFIG_UPDATE_UNAVAILABLE;

firebase-config/src/main/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigException.java

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
package com.google.firebase.remoteconfig;
1616

17-
import android.util.SparseArray;
1817
import androidx.annotation.NonNull;
1918
import androidx.annotation.Nullable;
2019
import com.google.firebase.FirebaseException;
@@ -54,7 +53,7 @@ public enum Code {
5453
CONFIG_UPDATE_STREAM_ERROR(0),
5554

5655
/** The stream invalidation message was unparsable. */
57-
CONFIG_UPDATE_MESSAGE_UNAVAILABLE(1),
56+
CONFIG_UPDATE_MESSAGE_INVALID(1),
5857

5958
/** Unable to fetch the latest config. */
6059
CONFIG_UPDATE_NOT_FETCHED(2),
@@ -74,20 +73,10 @@ public enum Code {
7473
public int value() {
7574
return value;
7675
}
76+
}
7777

78-
private static final SparseArray<Code> CODE_LIST = buildCodeList();
79-
80-
private static SparseArray<Code> buildCodeList() {
81-
SparseArray<Code> codes = new SparseArray<>();
82-
for (Code c : Code.values()) {
83-
Code existingValue = codes.get(c.value());
84-
if (existingValue != null) {
85-
throw new IllegalStateException(
86-
"Code value duplication between " + existingValue + "&" + c.name());
87-
}
88-
codes.put(c.value(), c);
89-
}
90-
return codes;
91-
}
78+
@NonNull
79+
public Code getCode() {
80+
return code;
9281
}
9382
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public void listenForNotifications() {
105105
new FirebaseRemoteConfigClientException(
106106
"Unable to parse config update message.",
107107
ex.getCause(),
108-
FirebaseRemoteConfigException.Code.CONFIG_UPDATE_MESSAGE_UNAVAILABLE));
108+
FirebaseRemoteConfigException.Code.CONFIG_UPDATE_MESSAGE_INVALID));
109109
} finally {
110110
httpURLConnection.disconnect();
111111
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,7 @@ public void onEvent() {
299299
@Override
300300
public void onError(@NonNull FirebaseRemoteConfigException error) {
301301
enableBackoff();
302-
propagateErrors(
303-
new FirebaseRemoteConfigServerException(
304-
"The server is temporarily unavailable. Try again in a few minutes.",
305-
FirebaseRemoteConfigException.Code.CONFIG_UPDATE_UNAVAILABLE));
302+
propagateErrors(error);
306303
}
307304
};
308305

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

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,12 @@ public final class FirebaseRemoteConfigTest {
149149
@Mock private ConfigAutoFetch mockConfigAutoFetch;
150150
@Mock private ConfigUpdateListenerRegistration mockRealtimeRegistration;
151151
@Mock private HttpURLConnection mockHttpURLConnection;
152-
@Mock private ConfigUpdateListener mockListener;
153152
@Mock private ConfigUpdateListener mockRetryListener;
153+
@Mock private ConfigUpdateListener mockOnEventListener;
154+
@Mock private ConfigUpdateListener mockStreamErrorEventListener;
155+
@Mock private ConfigUpdateListener mockInvalidMessageEventListener;
156+
@Mock private ConfigUpdateListener mockNotFetchedEventListener;
157+
@Mock private ConfigUpdateListener mockUnavailableEventListener;
154158

155159
@Mock private ConfigCacheClient mockFireperfFetchedCache;
156160
@Mock private ConfigCacheClient mockFireperfActivatedCache;
@@ -274,7 +278,31 @@ public void setUp() throws Exception {
274278
realtimeFetchedContainerResponse =
275279
FetchResponse.forBackendUpdatesFetched(realtimeFetchedContainer, ETAG);
276280
HashSet<ConfigUpdateListener> listeners = new HashSet();
277-
listeners.add(mockListener);
281+
282+
ConfigUpdateListener listener =
283+
new ConfigUpdateListener() {
284+
@Override
285+
public void onEvent() {
286+
mockOnEventListener.onEvent();
287+
}
288+
289+
@Override
290+
public void onError(@NonNull FirebaseRemoteConfigException error) {
291+
if (error.getCode() == FirebaseRemoteConfigException.Code.CONFIG_UPDATE_STREAM_ERROR) {
292+
mockStreamErrorEventListener.onError(error);
293+
} else if (error.getCode()
294+
== FirebaseRemoteConfigException.Code.CONFIG_UPDATE_MESSAGE_INVALID) {
295+
mockInvalidMessageEventListener.onError(error);
296+
} else if (error.getCode()
297+
== FirebaseRemoteConfigException.Code.CONFIG_UPDATE_NOT_FETCHED) {
298+
mockNotFetchedEventListener.onError(error);
299+
} else {
300+
mockUnavailableEventListener.onError(error);
301+
}
302+
}
303+
};
304+
305+
listeners.add(listener);
278306
configAutoFetch =
279307
new ConfigAutoFetch(mockHttpURLConnection, mockFetchHandler, listeners, mockRetryListener);
280308
configRealtimeHttpClient =
@@ -1139,7 +1167,7 @@ public void realtime_stream_listen_fail() throws Exception {
11391167
when(mockHttpURLConnection.getInputStream()).thenThrow(IOException.class);
11401168
configAutoFetch.listenForNotifications();
11411169

1142-
verify(mockListener).onError(any(FirebaseRemoteConfigClientException.class));
1170+
verify(mockInvalidMessageEventListener).onError(any(FirebaseRemoteConfigClientException.class));
11431171
}
11441172

11451173
@Test
@@ -1149,8 +1177,10 @@ public void realtime_redirectStatusCode_noRetries() throws Exception {
11491177
doNothing().when(configRealtimeHttpClientSpy).closeRealtimeHttpStream();
11501178
when(mockHttpURLConnection.getResponseCode()).thenReturn(301);
11511179
configRealtimeHttpClientSpy.beginRealtimeHttpStream();
1180+
11521181
verify(configRealtimeHttpClientSpy, never()).startAutoFetch(any());
11531182
verify(configRealtimeHttpClientSpy, never()).retryHTTPConnection();
1183+
verify(mockStreamErrorEventListener).onError(any(FirebaseRemoteConfigServerException.class));
11541184
}
11551185

11561186
@Test
@@ -1218,7 +1248,8 @@ public void realtime_stream_listen_and_failsafe_disabled() throws Exception {
12181248
when(mockFetchHandler.fetch(0)).thenReturn(Tasks.forResult(realtimeFetchedContainerResponse));
12191249
configAutoFetch.listenForNotifications();
12201250

1221-
verify(mockListener, never()).onError(any(FirebaseRemoteConfigServerException.class));
1251+
verify(mockUnavailableEventListener, never())
1252+
.onError(any(FirebaseRemoteConfigServerException.class));
12221253
verify(mockFetchHandler).getTemplateVersionNumber();
12231254
}
12241255

@@ -1230,7 +1261,7 @@ public void realtime_stream_listen_get_inputstream_fail() throws Exception {
12301261
when(mockFetchHandler.fetch(0)).thenReturn(Tasks.forResult(realtimeFetchedContainerResponse));
12311262
configAutoFetch.listenForNotifications();
12321263

1233-
verify(mockListener).onError(any(FirebaseRemoteConfigClientException.class));
1264+
verify(mockInvalidMessageEventListener).onError(any(FirebaseRemoteConfigClientException.class));
12341265
}
12351266

12361267
@Test
@@ -1239,7 +1270,7 @@ public void realtime_stream_autofetch_success() {
12391270
when(mockFetchHandler.fetch(0)).thenReturn(Tasks.forResult(realtimeFetchedContainerResponse));
12401271
configAutoFetch.fetchLatestConfig(1, 1);
12411272

1242-
verify(mockListener).onEvent();
1273+
verify(mockOnEventListener).onEvent();
12431274
}
12441275

12451276
@Test
@@ -1248,7 +1279,7 @@ public void realtime_stream_autofetch_failure() {
12481279
when(mockFetchHandler.fetch(0)).thenReturn(Tasks.forResult(realtimeFetchedContainerResponse));
12491280
configAutoFetch.fetchLatestConfig(1, 1000);
12501281

1251-
verify(mockListener).onError(any(FirebaseRemoteConfigServerException.class));
1282+
verify(mockNotFetchedEventListener).onError(any(FirebaseRemoteConfigServerException.class));
12521283
}
12531284

12541285
private static void loadCacheWithConfig(

0 commit comments

Comments
 (0)