14
14
15
15
package com .google .firebase .remoteconfig ;
16
16
17
- import static android .os .AsyncTask .THREAD_POOL_EXECUTOR ;
18
17
import static androidx .test .ext .truth .os .BundleSubject .assertThat ;
19
18
import static com .google .common .truth .Truth .assertThat ;
20
19
import static com .google .common .truth .Truth .assertWithMessage ;
44
43
45
44
import android .content .Context ;
46
45
import android .content .res .Resources ;
47
- import android .os .AsyncTask ;
48
46
import android .os .Bundle ;
49
47
import androidx .annotation .NonNull ;
50
48
import androidx .test .core .app .ApplicationProvider ;
86
84
import java .util .List ;
87
85
import java .util .Map ;
88
86
import java .util .Set ;
89
- import java .util .concurrent .Callable ;
90
87
import java .util .concurrent .CountDownLatch ;
91
88
import java .util .concurrent .Executor ;
92
- import java .util .concurrent .ExecutorService ;
93
89
import java .util .concurrent .Executors ;
94
90
import java .util .concurrent .ScheduledExecutorService ;
95
91
import java .util .concurrent .TimeUnit ;
96
-
97
92
import org .json .JSONArray ;
98
93
import org .json .JSONException ;
99
94
import org .json .JSONObject ;
103
98
import org .mockito .ArgumentCaptor ;
104
99
import org .mockito .Mock ;
105
100
import org .mockito .MockitoAnnotations ;
106
- import org .robolectric .Robolectric ;
107
101
import org .robolectric .RobolectricTestRunner ;
108
- import org .robolectric .android .util .concurrent .InlineExecutorService ;
109
102
import org .robolectric .annotation .Config ;
110
103
import org .robolectric .annotation .LooperMode ;
111
104
import org .skyscreamer .jsonassert .JSONAssert ;
@@ -193,7 +186,8 @@ public final class FirebaseRemoteConfigTest {
193
186
private ConfigRealtimeHttpClient configRealtimeHttpClient ;
194
187
private FetchResponse firstFetchedContainerResponse ;
195
188
196
- private final ScheduledExecutorService executorService = Executors .newSingleThreadScheduledExecutor ();
189
+ private final ScheduledExecutorService scheduledExecutorService =
190
+ Executors .newSingleThreadScheduledExecutor ();
197
191
198
192
@ Before
199
193
public void setUp () throws Exception {
@@ -338,7 +332,7 @@ public void onError(@NonNull FirebaseRemoteConfigException error) {
338
332
mockActivatedCache ,
339
333
listeners ,
340
334
mockRetryListener ,
341
- executorService );
335
+ scheduledExecutorService );
342
336
configRealtimeHttpClient =
343
337
new ConfigRealtimeHttpClient (
344
338
firebaseApp ,
@@ -348,7 +342,7 @@ public void onError(@NonNull FirebaseRemoteConfigException error) {
348
342
context ,
349
343
"firebase" ,
350
344
listeners ,
351
- executorService );
345
+ scheduledExecutorService );
352
346
}
353
347
354
348
@ Test
@@ -1321,21 +1315,22 @@ public void realtime_stream_autofetch_success() throws Exception {
1321
1315
when (mockFetchHandler .fetch (0L )).thenReturn (Tasks .forResult (realtimeFetchedContainerResponse ));
1322
1316
1323
1317
configAutoFetch .fetchLatestConfig (1 , 1 );
1324
- flushTasks ();
1318
+ flushScheduledTasks ();
1325
1319
1326
1320
Set <String > updatedParams = Sets .newHashSet ("realtime_param" );
1327
1321
verify (mockOnUpdateListener ).onUpdate (updatedParams );
1328
1322
}
1329
1323
1330
1324
@ Test
1331
- public void realtime_autofetchBeforeActivate_callsOnUpdateWithAllFetchedParams () throws Exception {
1325
+ public void realtime_autofetchBeforeActivate_callsOnUpdateWithAllFetchedParams ()
1326
+ throws Exception {
1332
1327
// The first call to get() returns null while the cache is loading.
1333
1328
loadCacheWithConfig (mockActivatedCache , null );
1334
1329
when (mockFetchHandler .getTemplateVersionNumber ()).thenReturn (1L );
1335
1330
when (mockFetchHandler .fetch (0 )).thenReturn (Tasks .forResult (realtimeFetchedContainerResponse ));
1336
1331
1337
1332
configAutoFetch .fetchLatestConfig (1 , 1 );
1338
- flushTasks ();
1333
+ flushScheduledTasks ();
1339
1334
1340
1335
Set <String > updatedParams = Sets .newHashSet ("string_param" , "long_param" , "realtime_param" );
1341
1336
verify (mockOnUpdateListener ).onUpdate (updatedParams );
@@ -1348,7 +1343,7 @@ public void realtime_stream_autofetch_failure() throws Exception {
1348
1343
when (mockFetchHandler .fetch (0 )).thenReturn (Tasks .forResult (realtimeFetchedContainerResponse ));
1349
1344
1350
1345
configAutoFetch .fetchLatestConfig (1 , 1000 );
1351
- flushTasks ();
1346
+ flushScheduledTasks ();
1352
1347
1353
1348
verify (mockNotFetchedEventListener ).onError (any (FirebaseRemoteConfigServerException .class ));
1354
1349
}
@@ -1431,14 +1426,16 @@ private static FirebaseApp initializeFirebaseApp(Context context) {
1431
1426
}
1432
1427
1433
1428
/**
1434
- * Flush tasks on the {@code executorService }'s thread.
1429
+ * Flush tasks on the {@code scheduledExecutorService }'s thread.
1435
1430
*
1436
- * @throws InterruptedException
1431
+ * @throws InterruptedException if the thread is interrupted while waiting.
1437
1432
*/
1438
- private void flushTasks () throws InterruptedException {
1433
+ private void flushScheduledTasks () throws InterruptedException {
1434
+ // Create a latch with a count of 1 and submit an execution request to countdown.
1435
+ // When the existing tasks have been executed, the countdown will execute and release the latch.
1439
1436
CountDownLatch latch = new CountDownLatch (1 );
1440
- executorService .execute (latch ::countDown );
1441
- assertTrue ("Task didn't finish" , latch .await (10 , TimeUnit .MILLISECONDS ));
1437
+ scheduledExecutorService .execute (latch ::countDown );
1438
+ assertTrue ("Task didn't finish. " , latch .await (10 , TimeUnit .MILLISECONDS ));
1442
1439
}
1443
1440
1444
1441
private ConfigUpdateListener generateEmptyRealtimeListener () {
0 commit comments