Skip to content

Commit d794b46

Browse files
authored
Add flaky test handling for Storage test (#531)
Add retry logic via RunFlakyBlock to TestPutFileAndGetFile.
1 parent 6e31f80 commit d794b46

File tree

1 file changed

+84
-68
lines changed

1 file changed

+84
-68
lines changed

storage/integration_test/src/integration_test.cc

+84-68
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ class FirebaseStorageTest : public FirebaseTest {
7979
// Called after each test.
8080
void TearDown() override;
8181

82+
// Create a unique working folder and return a reference to it.
83+
firebase::storage::StorageReference CreateFolder();
84+
85+
// File references that we need to delete on test exit.
86+
std::vector<firebase::storage::StorageReference>& cleanup_files() {
87+
return cleanup_files_;
88+
}
89+
8290
protected:
8391
// Initialize Firebase App and Firebase Auth.
8492
static void InitializeAppAndAuth();
@@ -97,9 +105,6 @@ class FirebaseStorageTest : public FirebaseTest {
97105
// Shut down Firebase Storage.
98106
void TerminateStorage();
99107

100-
// Create a unique working folder and return a reference to it.
101-
firebase::storage::StorageReference CreateFolder();
102-
103108
static firebase::App* shared_app_;
104109
static firebase::auth::Auth* shared_auth_;
105110

@@ -496,72 +501,83 @@ const char kFileUriScheme[] = "file://";
496501

497502
TEST_F(FirebaseStorageTest, TestPutFileAndGetFile) {
498503
SignIn();
499-
firebase::storage::StorageReference ref =
500-
CreateFolder().Child("TestFile-FileIO.txt");
501-
cleanup_files_.push_back(ref);
502-
503-
// Upload a file.
504-
{
505-
// Write file that we're going to upload.
506-
std::string path = PathForResource() + kPutFileTestFile;
507-
// Cloud Storage expects a URI, so add file:// in front of local paths.
508-
std::string file_path = kFileUriScheme + path;
509-
510-
LogDebug("Creating local file: %s", path.c_str());
511-
512-
FILE* file = fopen(path.c_str(), "wb");
513-
std::fwrite(kSimpleTestFile.c_str(), 1, kSimpleTestFile.size(), file);
514-
fclose(file);
515-
516-
firebase::storage::Metadata new_metadata;
517-
std::string content_type = "text/plain";
518-
new_metadata.set_content_type(content_type);
519-
520-
LogDebug("Uploading sample file from disk.");
521-
firebase::Future<firebase::storage::Metadata> future =
522-
ref.PutFile(file_path.c_str(), new_metadata);
523-
WaitForCompletion(future, "PutFile");
524-
ASSERT_NE(future.result(), nullptr);
525-
const firebase::storage::Metadata* metadata = future.result();
526-
EXPECT_EQ(metadata->size_bytes(), kSimpleTestFile.size());
527-
EXPECT_EQ(metadata->content_type(), content_type);
528-
}
529504

530-
// Use GetBytes to ensure the file uploaded correctly.
531-
{
532-
LogDebug("Downloading file to disk.");
533-
const size_t kBufferSize = 1024;
534-
char buffer[kBufferSize];
535-
memset(buffer, 0, sizeof(buffer));
536-
537-
firebase::Future<size_t> future = ref.GetBytes(buffer, kBufferSize);
538-
WaitForCompletion(future, "GetBytes");
539-
ASSERT_NE(future.result(), nullptr);
540-
size_t file_size = *future.result();
541-
EXPECT_EQ(file_size, kSimpleTestFile.size());
542-
EXPECT_THAT(kSimpleTestFile, ElementsAreArray(buffer, file_size))
543-
<< "Read file to byte buffer failed, file contents did not match.";
544-
}
545-
// Test GetFile to ensure we can download to a file.
546-
{
547-
std::string path = PathForResource() + kGetFileTestFile;
548-
// Cloud Storage expects a URI, so add file:// in front of local paths.
549-
std::string file_path = kFileUriScheme + path;
550-
551-
LogDebug("Saving to local file: %s", path.c_str());
552-
553-
firebase::Future<size_t> future = ref.GetFile(file_path.c_str());
554-
WaitForCompletion(future, "GetFile");
555-
ASSERT_NE(future.result(), nullptr);
556-
EXPECT_EQ(*future.result(), kSimpleTestFile.size());
557-
558-
std::vector<char> buffer(kSimpleTestFile.size());
559-
FILE* file = fopen(path.c_str(), "rb");
560-
ASSERT_NE(file, nullptr);
561-
std::fread(&buffer[0], 1, kSimpleTestFile.size(), file);
562-
fclose(file);
563-
EXPECT_THAT(kSimpleTestFile, ElementsAreArray(&buffer[0], buffer.size()))
564-
<< "Download to disk failed, file contents did not match.";
505+
if (!RunFlakyBlock(
506+
[](FirebaseStorageTest* this_) {
507+
firebase::storage::StorageReference ref =
508+
this_->CreateFolder().Child("TestFile-FileIO.txt");
509+
this_->cleanup_files().push_back(ref);
510+
511+
// Upload a file.
512+
{
513+
// Write file that we're going to upload.
514+
std::string path = PathForResource() + kPutFileTestFile;
515+
// Cloud Storage expects a URI, so add file:// in front of local
516+
// paths.
517+
std::string file_path = kFileUriScheme + path;
518+
519+
LogDebug("Creating local file: %s", path.c_str());
520+
521+
FILE* file = fopen(path.c_str(), "wb");
522+
std::fwrite(kSimpleTestFile.c_str(), 1, kSimpleTestFile.size(),
523+
file);
524+
fclose(file);
525+
526+
firebase::storage::Metadata new_metadata;
527+
std::string content_type = "text/plain";
528+
new_metadata.set_content_type(content_type);
529+
530+
LogDebug("Uploading sample file from disk.");
531+
firebase::Future<firebase::storage::Metadata> future =
532+
ref.PutFile(file_path.c_str(), new_metadata);
533+
FLAKY_WAIT_FOR_COMPLETION(future, "PutFile");
534+
FLAKY_EXPECT_NONNULL(future.result());
535+
const firebase::storage::Metadata* metadata = future.result();
536+
FLAKY_EXPECT_EQ(metadata->size_bytes(), kSimpleTestFile.size());
537+
FLAKY_EXPECT_EQ(metadata->content_type(), content_type);
538+
}
539+
// Use GetBytes to ensure the file uploaded correctly.
540+
{
541+
LogDebug("Downloading file to disk.");
542+
const size_t kBufferSize = 1024;
543+
char buffer[kBufferSize];
544+
memset(buffer, 0, sizeof(buffer));
545+
546+
firebase::Future<size_t> future =
547+
ref.GetBytes(buffer, kBufferSize);
548+
FLAKY_WAIT_FOR_COMPLETION(future, "GetBytes");
549+
FLAKY_EXPECT_NONNULL(future.result());
550+
size_t file_size = *future.result();
551+
FLAKY_EXPECT_EQ(file_size, kSimpleTestFile.size());
552+
FLAKY_EXPECT_EQ(
553+
memcmp(&kSimpleTestFile[0], &buffer[0], file_size), 0);
554+
}
555+
// Test GetFile to ensure we can download to a file.
556+
{
557+
std::string path = PathForResource() + kGetFileTestFile;
558+
// Cloud Storage expects a URI, so add file:// in front of local
559+
// paths.
560+
std::string file_path = kFileUriScheme + path;
561+
562+
LogDebug("Saving to local file: %s", path.c_str());
563+
564+
firebase::Future<size_t> future = ref.GetFile(file_path.c_str());
565+
FLAKY_WAIT_FOR_COMPLETION(future, "GetFile");
566+
FLAKY_EXPECT_NONNULL(future.result());
567+
FLAKY_EXPECT_EQ(*future.result(), kSimpleTestFile.size());
568+
569+
std::vector<char> buffer(kSimpleTestFile.size());
570+
FILE* file = fopen(path.c_str(), "rb");
571+
FLAKY_EXPECT_NONNULL(file);
572+
std::fread(&buffer[0], 1, kSimpleTestFile.size(), file);
573+
fclose(file);
574+
FLAKY_EXPECT_EQ(
575+
memcmp(&kSimpleTestFile[0], &buffer[0], buffer.size()), 0);
576+
}
577+
return true;
578+
},
579+
this)) {
580+
FAIL() << "Upload and download file failed, check log for details.";
565581
}
566582
}
567583

0 commit comments

Comments
 (0)