Skip to content

Commit 5474f9a

Browse files
committed
Inject executors for Realtime so async code can be tested. (#4375)
Allows the executor to be injected for testing. This came up testing Add updated params to the Realtime listener callback. #4339. Prepares Realtime to integrate with Firebase Executors.
1 parent 445b7ae commit 5474f9a

File tree

6 files changed

+29
-12
lines changed

6 files changed

+29
-12
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ protected RemoteConfigComponent(
129129
this.firebaseInstallations = firebaseInstallations;
130130
this.firebaseAbt = firebaseAbt;
131131
this.analyticsConnector = analyticsConnector;
132+
this.scheduledExecutorService = scheduledExecutorService;
132133

133134
this.appId = firebaseApp.getOptions().getApplicationId();
134135
GlobalBackgroundListener.ensureBackgroundListenerIsRegistered(context);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import java.net.HttpURLConnection;
3333
import java.util.Random;
3434
import java.util.Set;
35-
import java.util.concurrent.Executors;
3635
import java.util.concurrent.ScheduledExecutorService;
3736
import java.util.concurrent.TimeUnit;
3837
import org.json.JSONException;
@@ -58,12 +57,13 @@ public ConfigAutoFetch(
5857
HttpURLConnection httpURLConnection,
5958
ConfigFetchHandler configFetchHandler,
6059
Set<ConfigUpdateListener> eventListeners,
61-
ConfigUpdateListener retryCallback) {
60+
ConfigUpdateListener retryCallback,
61+
ScheduledExecutorService scheduledExecutorService) {
6262
this.httpURLConnection = httpURLConnection;
6363
this.configFetchHandler = configFetchHandler;
6464
this.eventListeners = eventListeners;
6565
this.retryCallback = retryCallback;
66-
this.scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
66+
this.scheduledExecutorService = scheduledExecutorService;
6767
this.random = new Random();
6868
}
6969

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.concurrent.ExecutorService;
2828
import java.util.concurrent.Future;
2929
import java.util.concurrent.FutureTask;
30+
import java.util.concurrent.ScheduledExecutorService;
3031

3132
public class ConfigRealtimeHandler {
3233

@@ -42,14 +43,16 @@ public class ConfigRealtimeHandler {
4243
private final Context context;
4344
private final String namespace;
4445
private final ExecutorService executorService;
46+
private final ScheduledExecutorService scheduledExecutorService;
4547

4648
public ConfigRealtimeHandler(
4749
FirebaseApp firebaseApp,
4850
FirebaseInstallationsApi firebaseInstallations,
4951
ConfigFetchHandler configFetchHandler,
5052
Context context,
5153
String namespace,
52-
ExecutorService executorService) {
54+
ExecutorService executorService,
55+
ScheduledExecutorService scheduledExecutorService) {
5356

5457
this.listeners = new LinkedHashSet<>();
5558
this.realtimeHttpClientTask = null;
@@ -60,6 +63,7 @@ public ConfigRealtimeHandler(
6063
this.context = context;
6164
this.namespace = namespace;
6265
this.executorService = executorService;
66+
this.scheduledExecutorService = scheduledExecutorService;
6367
}
6468

6569
private synchronized boolean canCreateRealtimeHttpClientTask() {
@@ -92,7 +96,8 @@ private synchronized void beginRealtime() {
9296
configFetchHandler,
9397
context,
9498
namespace,
95-
listeners);
99+
listeners,
100+
scheduledExecutorService);
96101
this.realtimeHttpClientTask =
97102
this.executorService.submit(
98103
new RealtimeHttpClientFutureTask(

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import java.util.Map;
4747
import java.util.Random;
4848
import java.util.Set;
49-
import java.util.concurrent.Executors;
5049
import java.util.concurrent.ScheduledExecutorService;
5150
import java.util.concurrent.TimeUnit;
5251
import java.util.regex.Matcher;
@@ -93,11 +92,12 @@ public ConfigRealtimeHttpClient(
9392
ConfigFetchHandler configFetchHandler,
9493
Context context,
9594
String namespace,
96-
Set<ConfigUpdateListener> listeners) {
95+
Set<ConfigUpdateListener> listeners,
96+
ScheduledExecutorService scheduledExecutorService) {
9797

9898
this.listeners = listeners;
9999
this.httpURLConnection = null;
100-
this.scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
100+
this.scheduledExecutorService = scheduledExecutorService;
101101

102102
// Retry parameters
103103
this.random = new Random();
@@ -303,7 +303,8 @@ public void onError(@NonNull FirebaseRemoteConfigException error) {
303303
}
304304
};
305305

306-
return new ConfigAutoFetch(httpURLConnection, configFetchHandler, listeners, retryCallback);
306+
return new ConfigAutoFetch(
307+
httpURLConnection, configFetchHandler, listeners, retryCallback, scheduledExecutorService);
307308
}
308309

309310
// HTTP status code that the Realtime client should retry on.

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@
8282
import java.util.Map;
8383
import java.util.Set;
8484
import java.util.concurrent.Executor;
85+
import java.util.concurrent.Executors;
86+
import java.util.concurrent.ScheduledExecutorService;
8587
import org.json.JSONArray;
8688
import org.json.JSONException;
8789
import org.json.JSONObject;
@@ -180,6 +182,9 @@ public final class FirebaseRemoteConfigTest {
180182

181183
private FetchResponse firstFetchedContainerResponse;
182184

185+
private final ScheduledExecutorService scheduledExecutorService =
186+
Executors.newSingleThreadScheduledExecutor();
187+
183188
@Before
184189
public void setUp() throws Exception {
185190
DEFAULTS_MAP.put("first_default_key", "first_default_value");
@@ -304,15 +309,21 @@ public void onError(@NonNull FirebaseRemoteConfigException error) {
304309

305310
listeners.add(listener);
306311
configAutoFetch =
307-
new ConfigAutoFetch(mockHttpURLConnection, mockFetchHandler, listeners, mockRetryListener);
312+
new ConfigAutoFetch(
313+
mockHttpURLConnection,
314+
mockFetchHandler,
315+
listeners,
316+
mockRetryListener,
317+
scheduledExecutorService);
308318
configRealtimeHttpClient =
309319
new ConfigRealtimeHttpClient(
310320
firebaseApp,
311321
mockFirebaseInstallations,
312322
mockFetchHandler,
313323
context,
314324
"firebase",
315-
listeners);
325+
listeners,
326+
scheduledExecutorService);
316327
}
317328

318329
@Test

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import java.util.concurrent.ExecutorService;
4444
import java.util.concurrent.Executors;
4545
import java.util.concurrent.ScheduledExecutorService;
46-
4746
import org.junit.Before;
4847
import org.junit.Test;
4948
import org.junit.runner.RunWith;

0 commit comments

Comments
 (0)