@@ -56,6 +56,7 @@ namespace firebase_testapp_automated {
56
56
57
57
using app_framework::LogDebug;
58
58
using app_framework::LogError;
59
+ using app_framework::LogWarning;
59
60
60
61
// You can customize the Storage URL here.
61
62
const char * kStorageUrl = nullptr ;
@@ -1209,7 +1210,8 @@ class StorageListener : public firebase::storage::Listener {
1209
1210
: on_paused_was_called_(false ),
1210
1211
on_progress_was_called_ (false ),
1211
1212
resume_succeeded_(false ),
1212
- last_bytes_transferred_(-1 ) {}
1213
+ last_bytes_transferred_(-1 ),
1214
+ timeout_time_(0 ) {}
1213
1215
1214
1216
// Tracks whether OnPaused was ever called and resumes the transfer.
1215
1217
void OnPaused (firebase::storage::Controller* controller) override {
@@ -1231,6 +1233,14 @@ class StorageListener : public firebase::storage::Listener {
1231
1233
}
1232
1234
1233
1235
void OnProgress (firebase::storage::Controller* controller) override {
1236
+ // Check for timeout.
1237
+ if (timeout_time_ > 0 ) {
1238
+ if (app_framework::GetCurrentTimeInMicroseconds () >= timeout_time_) {
1239
+ timeout_time_ = -1 ;
1240
+ controller->Cancel ();
1241
+ }
1242
+ }
1243
+
1234
1244
int64_t bytes_transferred = controller->bytes_transferred ();
1235
1245
// Only update when the byte count changed, to avoid spamming the log.
1236
1246
if (last_bytes_transferred_ != bytes_transferred) {
@@ -1245,11 +1255,22 @@ class StorageListener : public firebase::storage::Listener {
1245
1255
bool on_progress_was_called () const { return on_progress_was_called_; }
1246
1256
bool resume_succeeded () const { return resume_succeeded_; }
1247
1257
1258
+ void SetTimeoutSeconds (int seconds_from_now) {
1259
+ int64_t microseconds_from_now =
1260
+ static_cast <int64_t >(seconds_from_now) * 1000000L ;
1261
+ timeout_time_ =
1262
+ app_framework::GetCurrentTimeInMicroseconds () + microseconds_from_now;
1263
+ }
1264
+
1265
+ bool DidTimeout () { return (timeout_time_ == -1 ); }
1266
+
1248
1267
public:
1249
1268
bool on_paused_was_called_;
1250
1269
bool on_progress_was_called_;
1251
1270
bool resume_succeeded_;
1252
1271
int64_t last_bytes_transferred_;
1272
+
1273
+ int64_t timeout_time_;
1253
1274
};
1254
1275
1255
1276
// Contents of a large file, "X" will be replaced with a different character
@@ -1332,21 +1353,40 @@ TEST_F(FirebaseStorageTest, TestLargeFilePauseResumeAndDownloadCancel) {
1332
1353
EXPECT_EQ (metadata->size_bytes (), kLargeFileSize );
1333
1354
1334
1355
FLAKY_TEST_SECTION_END ();
1356
+ const int kDownloadTimeoutSeconds = 120 ;
1335
1357
1336
1358
// Download the file and confirm it's correct.
1337
1359
{
1338
1360
std::vector<char > buffer (kLargeFileSize );
1339
1361
memset (&buffer[0 ], 0 , kLargeFileSize );
1340
1362
LogDebug (" Downloading large file for comparison." );
1341
1363
StorageListener listener;
1342
- firebase::Future<size_t > future = RunWithRetry<size_t >(
1343
- [&]() { return ref.GetBytes (&buffer[0 ], kLargeFileSize , &listener); });
1344
- WaitForCompletion (future, " GetBytes" );
1345
- ASSERT_NE (future.result (), nullptr );
1346
- size_t file_size = *future.result ();
1347
- EXPECT_EQ (file_size, kLargeFileSize ) << " Read size did not match" ;
1348
- EXPECT_TRUE (memcmp (kLargeTestFile .c_str (), &buffer[0 ], kLargeFileSize ) == 0 )
1349
- << " Read large file failed, contents did not match." ;
1364
+ firebase::storage::Controller controller;
1365
+ firebase::Future<size_t > future;
1366
+
1367
+ FLAKY_TEST_SECTION_BEGIN ();
1368
+
1369
+ future = ref.GetBytes (&buffer[0 ], kLargeFileSize , &listener, &controller);
1370
+ listener.SetTimeoutSeconds (kDownloadTimeoutSeconds );
1371
+ ASSERT_TRUE (controller.is_valid ());
1372
+ WaitForCompletionAnyResult (future, " GetBytes" );
1373
+ if (!listener.DidTimeout ()) {
1374
+ EXPECT_EQ (future.error (), 0 );
1375
+ }
1376
+
1377
+ FLAKY_TEST_SECTION_END ();
1378
+
1379
+ if (!listener.DidTimeout ()) {
1380
+ ASSERT_NE (future.result (), nullptr );
1381
+ size_t file_size = *future.result ();
1382
+ EXPECT_EQ (file_size, kLargeFileSize ) << " Read size did not match" ;
1383
+ EXPECT_TRUE (memcmp (kLargeTestFile .c_str (), &buffer[0 ], kLargeFileSize ) ==
1384
+ 0 )
1385
+ << " Read large file failed, contents did not match." ;
1386
+ } else {
1387
+ LogWarning (" Download timed out after %d seconds." ,
1388
+ kDownloadTimeoutSeconds );
1389
+ }
1350
1390
}
1351
1391
#if FIREBASE_PLATFORM_DESKTOP
1352
1392
FLAKY_TEST_SECTION_BEGIN ();
@@ -1374,18 +1414,24 @@ TEST_F(FirebaseStorageTest, TestLargeFilePauseResumeAndDownloadCancel) {
1374
1414
FAIL () << " Pause failed" ;
1375
1415
}
1376
1416
1377
- WaitForCompletion (future, " GetBytes" );
1417
+ listener.SetTimeoutSeconds (kDownloadTimeoutSeconds );
1418
+ WaitForCompletionAnyResult (future, " GetBytes" );
1378
1419
1379
1420
LogDebug (" Download complete." );
1380
1421
1381
1422
// Ensure the progress and pause callbacks were called.
1382
1423
EXPECT_TRUE (listener.on_paused_was_called ());
1383
1424
EXPECT_TRUE (listener.on_progress_was_called ());
1384
1425
EXPECT_TRUE (listener.resume_succeeded ());
1385
- EXPECT_NE (future.result (), nullptr );
1386
- size_t file_size = *future.result ();
1387
- EXPECT_EQ (file_size, kLargeFileSize );
1388
- EXPECT_EQ (memcmp (kLargeTestFile .c_str (), &buffer[0 ], kLargeFileSize ), 0 );
1426
+ if (!listener.DidTimeout ()) {
1427
+ EXPECT_EQ (future.error (), 0 );
1428
+ EXPECT_NE (future.result (), nullptr );
1429
+ size_t file_size = *future.result ();
1430
+ EXPECT_EQ (file_size, kLargeFileSize );
1431
+ EXPECT_EQ (memcmp (kLargeTestFile .c_str (), &buffer[0 ], kLargeFileSize ), 0 );
1432
+ } else {
1433
+ LogWarning (" Download timed out after %d seconds." , kDownloadTimeoutSeconds );
1434
+ }
1389
1435
1390
1436
FLAKY_TEST_SECTION_END ();
1391
1437
#else
@@ -1397,23 +1443,36 @@ TEST_F(FirebaseStorageTest, TestLargeFilePauseResumeAndDownloadCancel) {
1397
1443
LogDebug (" Downloading large file." );
1398
1444
StorageListener listener;
1399
1445
firebase::storage::Controller controller;
1400
- firebase::Future<size_t > future = RunWithRetry<size_t >([&]() {
1401
- return ref.GetBytes (&buffer[0 ], kLargeFileSize , &listener, &controller);
1402
- });
1446
+ firebase::Future<size_t > future;
1447
+
1448
+ FLAKY_TEST_SECTION_BEGIN ();
1449
+
1450
+ future = ref.GetBytes (&buffer[0 ], kLargeFileSize , &listener, &controller);
1451
+ listener.SetTimeoutSeconds (kDownloadTimeoutSeconds );
1403
1452
ASSERT_TRUE (controller.is_valid ());
1453
+ WaitForCompletionAnyResult (future, " GetBytes" );
1454
+ if (!listener.DidTimeout ()) {
1455
+ EXPECT_EQ (future.error (), 0 );
1456
+ }
1457
+
1458
+ FLAKY_TEST_SECTION_END ();
1404
1459
1405
- WaitForCompletion (future, " GetBytes" );
1406
1460
LogDebug (" Download complete." );
1407
1461
1408
1462
// Ensure the progress callback was called.
1409
1463
EXPECT_TRUE (listener.on_progress_was_called ());
1410
1464
EXPECT_FALSE (listener.on_paused_was_called ());
1411
-
1412
- ASSERT_NE (future.result (), nullptr );
1413
- size_t file_size = *future.result ();
1414
- EXPECT_EQ (file_size, kLargeFileSize ) << " Read size did not match" ;
1415
- EXPECT_TRUE (memcmp (kLargeTestFile .c_str (), &buffer[0 ], kLargeFileSize ) == 0 )
1416
- << " Read large file failed, contents did not match." ;
1465
+ if (!listener.DidTimeout ()) {
1466
+ ASSERT_NE (future.result (), nullptr );
1467
+ size_t file_size = *future.result ();
1468
+ EXPECT_EQ (file_size, kLargeFileSize ) << " Read size did not match" ;
1469
+ EXPECT_TRUE (memcmp (kLargeTestFile .c_str (), &buffer[0 ], kLargeFileSize ) ==
1470
+ 0 )
1471
+ << " Read large file failed, contents did not match." ;
1472
+ } else {
1473
+ LogWarning (" Download timed out after %d seconds." ,
1474
+ kDownloadTimeoutSeconds );
1475
+ }
1417
1476
}
1418
1477
#endif // FIREBASE_PLATFORM_DESKTOP
1419
1478
0 commit comments