@@ -349,6 +349,7 @@ public void onError(@NonNull FirebaseRemoteConfigException error) {
349
349
listeners ,
350
350
mockRetryListener ,
351
351
scheduledExecutorService );
352
+ configAutoFetch .setInBackground (false );
352
353
realtimeMetadataClient =
353
354
new ConfigMetadataClient (context .getSharedPreferences ("test_file" , Context .MODE_PRIVATE ));
354
355
configRealtimeHttpClient =
@@ -1304,9 +1305,7 @@ public void realtime_redirectStatusCode_noRetries() throws Exception {
1304
1305
doReturn (Tasks .forResult (mockHttpURLConnection ))
1305
1306
.when (configRealtimeHttpClientSpy )
1306
1307
.createRealtimeConnection ();
1307
- doNothing ()
1308
- .when (configRealtimeHttpClientSpy )
1309
- .closeRealtimeHttpStream (any (HttpURLConnection .class ));
1308
+ doNothing ().when (configRealtimeHttpClientSpy ).closeRealtimeHttpStream ();
1310
1309
when (mockHttpURLConnection .getResponseCode ()).thenReturn (301 );
1311
1310
1312
1311
configRealtimeHttpClientSpy .beginRealtimeHttpStream ();
@@ -1325,9 +1324,7 @@ public void realtime_okStatusCode_startAutofetchAndRetries() throws Exception {
1325
1324
.createRealtimeConnection ();
1326
1325
doReturn (mockConfigAutoFetch ).when (configRealtimeHttpClientSpy ).startAutoFetch (any ());
1327
1326
doNothing ().when (configRealtimeHttpClientSpy ).retryHttpConnectionWhenBackoffEnds ();
1328
- doNothing ()
1329
- .when (configRealtimeHttpClientSpy )
1330
- .closeRealtimeHttpStream (any (HttpURLConnection .class ));
1327
+ doNothing ().when (configRealtimeHttpClientSpy ).closeRealtimeHttpStream ();
1331
1328
when (mockHttpURLConnection .getResponseCode ()).thenReturn (200 );
1332
1329
1333
1330
configRealtimeHttpClientSpy .beginRealtimeHttpStream ();
@@ -1344,9 +1341,7 @@ public void realtime_badGatewayStatusCode_noAutofetchButRetries() throws Excepti
1344
1341
.when (configRealtimeHttpClientSpy )
1345
1342
.createRealtimeConnection ();
1346
1343
doNothing ().when (configRealtimeHttpClientSpy ).retryHttpConnectionWhenBackoffEnds ();
1347
- doNothing ()
1348
- .when (configRealtimeHttpClientSpy )
1349
- .closeRealtimeHttpStream (any (HttpURLConnection .class ));
1344
+ doNothing ().when (configRealtimeHttpClientSpy ).closeRealtimeHttpStream ();
1350
1345
when (mockHttpURLConnection .getResponseCode ()).thenReturn (502 );
1351
1346
1352
1347
configRealtimeHttpClientSpy .beginRealtimeHttpStream ();
@@ -1363,9 +1358,7 @@ public void realtime_retryableStatusCode_increasesConfigMetadataFailedStreams()
1363
1358
.when (configRealtimeHttpClientSpy )
1364
1359
.createRealtimeConnection ();
1365
1360
doNothing ().when (configRealtimeHttpClientSpy ).retryHttpConnectionWhenBackoffEnds ();
1366
- doNothing ()
1367
- .when (configRealtimeHttpClientSpy )
1368
- .closeRealtimeHttpStream (any (HttpURLConnection .class ));
1361
+ doNothing ().when (configRealtimeHttpClientSpy ).closeRealtimeHttpStream ();
1369
1362
when (mockHttpURLConnection .getResponseCode ()).thenReturn (502 );
1370
1363
int failedStreams = configRealtimeHttpClientSpy .getNumberOfFailedStreams ();
1371
1364
@@ -1382,9 +1375,7 @@ public void realtime_retryableStatusCode_increasesConfigMetadataBackoffDate() th
1382
1375
.when (configRealtimeHttpClientSpy )
1383
1376
.createRealtimeConnection ();
1384
1377
doNothing ().when (configRealtimeHttpClientSpy ).retryHttpConnectionWhenBackoffEnds ();
1385
- doNothing ()
1386
- .when (configRealtimeHttpClientSpy )
1387
- .closeRealtimeHttpStream (any (HttpURLConnection .class ));
1378
+ doNothing ().when (configRealtimeHttpClientSpy ).closeRealtimeHttpStream ();
1388
1379
when (mockHttpURLConnection .getResponseCode ()).thenReturn (502 );
1389
1380
Date backoffDate = configRealtimeHttpClientSpy .getBackoffEndTime ();
1390
1381
@@ -1403,9 +1394,7 @@ public void realtime_successfulStatusCode_doesNotIncreaseConfigMetadataFailedStr
1403
1394
.createRealtimeConnection ();
1404
1395
doReturn (mockConfigAutoFetch ).when (configRealtimeHttpClientSpy ).startAutoFetch (any ());
1405
1396
doNothing ().when (configRealtimeHttpClientSpy ).retryHttpConnectionWhenBackoffEnds ();
1406
- doNothing ()
1407
- .when (configRealtimeHttpClientSpy )
1408
- .closeRealtimeHttpStream (any (HttpURLConnection .class ));
1397
+ doNothing ().when (configRealtimeHttpClientSpy ).closeRealtimeHttpStream ();
1409
1398
when (mockHttpURLConnection .getResponseCode ()).thenReturn (200 );
1410
1399
int failedStreams = configRealtimeHttpClientSpy .getNumberOfFailedStreams ();
1411
1400
@@ -1424,9 +1413,7 @@ public void realtime_successfulStatusCode_doesNotIncreaseConfigMetadataBackoffDa
1424
1413
.createRealtimeConnection ();
1425
1414
doReturn (mockConfigAutoFetch ).when (configRealtimeHttpClientSpy ).startAutoFetch (any ());
1426
1415
doNothing ().when (configRealtimeHttpClientSpy ).retryHttpConnectionWhenBackoffEnds ();
1427
- doNothing ()
1428
- .when (configRealtimeHttpClientSpy )
1429
- .closeRealtimeHttpStream (any (HttpURLConnection .class ));
1416
+ doNothing ().when (configRealtimeHttpClientSpy ).closeRealtimeHttpStream ();
1430
1417
when (mockHttpURLConnection .getResponseCode ()).thenReturn (200 );
1431
1418
Date backoffDate = configRealtimeHttpClientSpy .getBackoffEndTime ();
1432
1419
@@ -1442,9 +1429,7 @@ public void realtime_forbiddenStatusCode_returnsStreamError() throws Exception {
1442
1429
doReturn (Tasks .forResult (mockHttpURLConnection ))
1443
1430
.when (configRealtimeHttpClientSpy )
1444
1431
.createRealtimeConnection ();
1445
- doNothing ()
1446
- .when (configRealtimeHttpClientSpy )
1447
- .closeRealtimeHttpStream (any (HttpURLConnection .class ));
1432
+ doNothing ().when (configRealtimeHttpClientSpy ).closeRealtimeHttpStream ();
1448
1433
when (mockHttpURLConnection .getErrorStream ())
1449
1434
.thenReturn (
1450
1435
new ByteArrayInputStream (FORBIDDEN_ERROR_MESSAGE .getBytes (StandardCharsets .UTF_8 )));
@@ -1465,9 +1450,7 @@ public void realtime_exceptionThrown_noAutofetchButRetries() throws Exception {
1465
1450
.when (configRealtimeHttpClientSpy )
1466
1451
.createRealtimeConnection ();
1467
1452
doNothing ().when (configRealtimeHttpClientSpy ).retryHttpConnectionWhenBackoffEnds ();
1468
- doNothing ()
1469
- .when (configRealtimeHttpClientSpy )
1470
- .closeRealtimeHttpStream (any (HttpURLConnection .class ));
1453
+ doNothing ().when (configRealtimeHttpClientSpy ).closeRealtimeHttpStream ();
1471
1454
1472
1455
configRealtimeHttpClientSpy .beginRealtimeHttpStream ();
1473
1456
flushScheduledTasks ();
@@ -1534,11 +1517,31 @@ public void realtime_stream_listen_get_inputstream_fail() throws Exception {
1534
1517
when (mockFetchHandler .fetchNowWithTypeAndAttemptNumber (
1535
1518
ConfigFetchHandler .FetchType .REALTIME , 1 ))
1536
1519
.thenReturn (Tasks .forResult (realtimeFetchedContainerResponse ));
1520
+
1537
1521
configAutoFetch .listenForNotifications ();
1538
1522
1539
1523
verify (mockHttpURLConnection ).disconnect ();
1540
1524
}
1541
1525
1526
+ @ Test
1527
+ public void realtime_stream_listen_backgrounded_disconnects () throws Exception {
1528
+ when (mockHttpURLConnection .getResponseCode ()).thenReturn (200 );
1529
+ when (mockHttpURLConnection .getInputStream ())
1530
+ .thenReturn (
1531
+ new ByteArrayInputStream (
1532
+ "{ \" featureDisabled\" : false, \" latestTemplateVersionNumber\" : 2 } }"
1533
+ .getBytes (StandardCharsets .UTF_8 )));
1534
+ when (mockFetchHandler .getTemplateVersionNumber ()).thenReturn (1L );
1535
+ when (mockFetchHandler .fetchNowWithTypeAndAttemptNumber (
1536
+ ConfigFetchHandler .FetchType .REALTIME , 1 ))
1537
+ .thenReturn (Tasks .forResult (realtimeFetchedContainerResponse ));
1538
+
1539
+ configAutoFetch .listenForNotifications ();
1540
+ frc .setConfigUpdateBackgroundState (true );
1541
+
1542
+ verify (mockHttpURLConnection , times (1 )).disconnect ();
1543
+ }
1544
+
1542
1545
@ Test
1543
1546
public void realtime_stream_autofetch_success () throws Exception {
1544
1547
// Setup activated configs with keys "string_param", "long_param"
0 commit comments