Skip to content

Commit f6d1bee

Browse files
authored
Clean up all flaky tests to be much more straightforward. (#543)
This PR adds RunFlakyTestSection, which lets you use regular EXPECT macros. (This touches some internal googletest stuff, so it may break in the future if we update to a later googletest version.) Rather than using RunFlakyTestSection directly, you should use the following two macros: FLAKY_TEST_SECTION_BEGIN(); FLAKY_TEST_SECTION_END(); This PR also cleans up existing tests to use this new method.
1 parent 4029966 commit f6d1bee

File tree

6 files changed

+457
-554
lines changed

6 files changed

+457
-554
lines changed

analytics/integration_test/src/integration_test.cc

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -132,26 +132,27 @@ TEST_F(FirebaseAnalyticsTest, TestLogEventWithMultipleParameters) {
132132

133133
TEST_F(FirebaseAnalyticsTest, TestResettingGivesNewInstanceId) {
134134
// Test is flaky on iPhone due to a known issue in iOS. See b/143656277.
135-
if (!RunFlakyBlock([&]() {
136-
firebase::Future<std::string> future =
137-
firebase::analytics::GetAnalyticsInstanceId();
138-
FLAKY_WAIT_FOR_COMPLETION(future, "GetAnalyticsInstanceId");
139-
FLAKY_EXPECT_FALSE(future.result()->empty());
140-
std::string instance_id = *future.result();
141-
142-
firebase::analytics::ResetAnalyticsData();
143-
144-
future = firebase::analytics::GetAnalyticsInstanceId();
145-
FLAKY_WAIT_FOR_COMPLETION(
146-
future, "GetAnalyticsInstanceId after ResetAnalyticsData");
147-
std::string new_instance_id = *future.result();
148-
FLAKY_EXPECT_FALSE(future.result()->empty());
149-
FLAKY_EXPECT_NE(instance_id, new_instance_id);
150-
151-
FLAKY_SUCCESS();
152-
})) {
153-
FAIL() << "Failed, see error log for details.";
154-
}
135+
#if TARGET_OS_IPHONE
136+
FLAKY_TEST_SECTION_BEGIN();
137+
#endif // TARGET_OS_IPHONE
138+
139+
firebase::Future<std::string> future =
140+
firebase::analytics::GetAnalyticsInstanceId();
141+
WaitForCompletion(future, "GetAnalyticsInstanceId");
142+
EXPECT_FALSE(future.result()->empty());
143+
std::string instance_id = *future.result();
144+
145+
firebase::analytics::ResetAnalyticsData();
146+
147+
future = firebase::analytics::GetAnalyticsInstanceId();
148+
WaitForCompletion(future, "GetAnalyticsInstanceId after ResetAnalyticsData");
149+
std::string new_instance_id = *future.result();
150+
EXPECT_FALSE(future.result()->empty());
151+
EXPECT_NE(instance_id, new_instance_id);
152+
153+
#if TARGET_OS_IPHONE
154+
FLAKY_TEST_SECTION_END();
155+
#endif // TARGET_OS_IPHONE
155156
}
156157

157158
} // namespace firebase_testapp_automated

auth/integration_test/src/integration_test.cc

Lines changed: 110 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -720,73 +720,67 @@ TEST_F(FirebaseAuthTest, TestWithCustomEmailAndPassword) {
720720
}
721721

722722
TEST_F(FirebaseAuthTest, TestAuthPersistenceWithAnonymousSignin) {
723-
// Test is disabled on linux due to the need to unlock the keystore.
723+
// Automated test is disabled on linux due to the need to unlock the keystore.
724724
SKIP_TEST_ON_LINUX;
725-
if (!RunFlakyBlock([&]() {
726-
FLAKY_WAIT_FOR_COMPLETION(auth_->SignInAnonymously(),
727-
"SignInAnonymously");
728-
FLAKY_EXPECT_NONNULL(auth_->current_user());
729-
FLAKY_EXPECT_TRUE(auth_->current_user()->is_anonymous());
730-
Terminate();
731-
ProcessEvents(2000);
732-
Initialize();
733-
FLAKY_EXPECT_NONNULL(auth_);
734-
FLAKY_EXPECT_NONNULL(auth_->current_user());
735-
FLAKY_EXPECT_TRUE(auth_->current_user()->is_anonymous());
736-
DeleteUser();
737-
738-
FLAKY_SUCCESS();
739-
})) {
740-
FAIL() << "Test failed, see log for details.";
741-
}
725+
726+
FLAKY_TEST_SECTION_BEGIN();
727+
728+
WaitForCompletion(auth_->SignInAnonymously(), "SignInAnonymously");
729+
EXPECT_NE(auth_->current_user(), nullptr);
730+
EXPECT_TRUE(auth_->current_user()->is_anonymous());
731+
Terminate();
732+
ProcessEvents(2000);
733+
Initialize();
734+
EXPECT_NE(auth_, nullptr);
735+
EXPECT_NE(auth_->current_user(), nullptr);
736+
EXPECT_TRUE(auth_->current_user()->is_anonymous());
737+
DeleteUser();
738+
739+
FLAKY_TEST_SECTION_END();
742740
}
743741
TEST_F(FirebaseAuthTest, TestAuthPersistenceWithEmailSignin) {
744-
// Test is disabled on linux due to the need to unlock the keystore.
742+
// Automated test is disabled on linux due to the need to unlock the keystore.
745743
SKIP_TEST_ON_LINUX;
746744

747-
if (!RunFlakyBlock([&]() {
748-
std::string email = GenerateEmailAddress();
749-
FLAKY_WAIT_FOR_COMPLETION(
750-
auth_->CreateUserWithEmailAndPassword(email.c_str(), kTestPassword),
751-
"CreateUserWithEmailAndPassword");
752-
FLAKY_EXPECT_NONNULL(auth_->current_user());
753-
FLAKY_EXPECT_FALSE(auth_->current_user()->is_anonymous());
754-
std::string prev_provider_id = auth_->current_user()->provider_id();
755-
// Save the old provider ID list so we can make sure it's the same once
756-
// it's loaded again.
757-
std::vector<std::string> prev_provider_data_ids;
758-
for (int i = 0; i < auth_->current_user()->provider_data().size();
759-
i++) {
760-
prev_provider_data_ids.push_back(
761-
auth_->current_user()->provider_data()[i]->provider_id());
762-
}
763-
Terminate();
764-
ProcessEvents(2000);
765-
Initialize();
766-
FLAKY_EXPECT_NONNULL(auth_);
767-
FLAKY_EXPECT_NONNULL(auth_->current_user());
768-
FLAKY_EXPECT_FALSE(auth_->current_user()->is_anonymous());
769-
// Make sure the provider IDs are the same as they were before.
770-
FLAKY_EXPECT_EQ(auth_->current_user()->provider_id(), prev_provider_id);
771-
std::vector<std::string> loaded_provider_data_ids;
772-
for (int i = 0; i < auth_->current_user()->provider_data().size();
773-
i++) {
774-
loaded_provider_data_ids.push_back(
775-
auth_->current_user()->provider_data()[i]->provider_id());
776-
}
777-
FLAKY_EXPECT_TRUE(loaded_provider_data_ids == prev_provider_data_ids);
778-
779-
// Cleanup, ensure we are signed in as the user so we can delete it.
780-
FLAKY_WAIT_FOR_COMPLETION(
781-
auth_->SignInWithEmailAndPassword(email.c_str(), kTestPassword),
782-
"SignInWithEmailAndPassword");
783-
FLAKY_EXPECT_NONNULL(auth_->current_user());
784-
DeleteUser();
785-
786-
FLAKY_SUCCESS();
787-
})) {
788-
FAIL() << "Test failed, see log for details.";
745+
FLAKY_TEST_SECTION_BEGIN();
746+
747+
std::string email = GenerateEmailAddress();
748+
WaitForCompletion(
749+
auth_->CreateUserWithEmailAndPassword(email.c_str(), kTestPassword),
750+
"CreateUserWithEmailAndPassword");
751+
EXPECT_NE(auth_->current_user(), nullptr);
752+
EXPECT_FALSE(auth_->current_user()->is_anonymous());
753+
std::string prev_provider_id = auth_->current_user()->provider_id();
754+
// Save the old provider ID list so we can make sure it's the same once
755+
// it's loaded again.
756+
std::vector<std::string> prev_provider_data_ids;
757+
for (int i = 0; i < auth_->current_user()->provider_data().size(); i++) {
758+
prev_provider_data_ids.push_back(
759+
auth_->current_user()->provider_data()[i]->provider_id());
760+
}
761+
Terminate();
762+
ProcessEvents(2000);
763+
Initialize();
764+
EXPECT_NE(auth_, nullptr);
765+
EXPECT_NE(auth_->current_user(), nullptr);
766+
EXPECT_FALSE(auth_->current_user()->is_anonymous());
767+
// Make sure the provider IDs are the same as they were before.
768+
EXPECT_EQ(auth_->current_user()->provider_id(), prev_provider_id);
769+
std::vector<std::string> loaded_provider_data_ids;
770+
for (int i = 0; i < auth_->current_user()->provider_data().size(); i++) {
771+
loaded_provider_data_ids.push_back(
772+
auth_->current_user()->provider_data()[i]->provider_id());
789773
}
774+
EXPECT_TRUE(loaded_provider_data_ids == prev_provider_data_ids);
775+
776+
// Cleanup, ensure we are signed in as the user so we can delete it.
777+
WaitForCompletion(
778+
auth_->SignInWithEmailAndPassword(email.c_str(), kTestPassword),
779+
"SignInWithEmailAndPassword");
780+
EXPECT_NE(auth_->current_user(), nullptr);
781+
DeleteUser();
782+
783+
FLAKY_TEST_SECTION_END();
790784
}
791785

792786
class PhoneListener : public firebase::auth::PhoneAuthProvider::Listener {
@@ -873,67 +867,63 @@ TEST_F(FirebaseAuthTest, TestPhoneAuth) {
873867
// Note: This test requires interactivity on iOS, as it displays a CAPTCHA.
874868
TEST_REQUIRES_USER_INTERACTION;
875869
#endif // TARGET_OS_IPHONE
876-
if (!RunFlakyBlock([&]() {
877-
{
878-
firebase::auth::PhoneAuthProvider& phone_provider =
879-
firebase::auth::PhoneAuthProvider::GetInstance(auth_);
880-
LogDebug("Creating listener.");
881-
PhoneListener listener;
882-
LogDebug("Calling VerifyPhoneNumber.");
883-
// Randomly choose one of the phone numbers to avoid collisions.
884-
const int random_phone_number =
885-
app_framework::GetCurrentTimeInMicroseconds() %
886-
kPhoneAuthTestNumPhoneNumbers;
887-
phone_provider.VerifyPhoneNumber(
888-
kPhoneAuthTestPhoneNumbers[random_phone_number],
889-
kPhoneAuthTimeoutMs, nullptr, &listener);
890-
// Wait for OnCodeSent() callback.
891-
int wait_ms = 0;
892-
LogDebug("Waiting for code send.");
893-
while (listener.waiting_to_send_code()) {
894-
if (wait_ms > kPhoneAuthCodeSendWaitMs) break;
895-
ProcessEvents(kWaitIntervalMs);
896-
wait_ms += kWaitIntervalMs;
897-
}
898-
FLAKY_EXPECT_EQ(listener.on_verification_failed_count(), 0);
899-
LogDebug("Waiting for verification ID.");
900-
// Wait for the listener to have a verification ID.
901-
wait_ms = 0;
902-
while (listener.waiting_for_verification_id()) {
903-
if (wait_ms > kPhoneAuthCompletionWaitMs) break;
904-
ProcessEvents(kWaitIntervalMs);
905-
wait_ms += kWaitIntervalMs;
906-
}
907-
if (listener.on_verification_complete_count() > 0) {
908-
LogDebug("Signing in with automatic verification code.");
909-
FLAKY_WAIT_FOR_COMPLETION(
910-
auth_->SignInWithCredential(listener.credential()),
911-
"SignInWithCredential(PhoneCredential) automatic");
912-
} else if (listener.on_verification_failed_count() > 0) {
913-
LogError("Automatic verification failed.");
914-
FLAKY_FAIL();
915-
} else {
916-
// Did not automatically verify, submit verification code manually.
917-
FLAKY_EXPECT_TRUE(listener.on_code_auto_retrieval_time_out_count() >
918-
0);
919-
FLAKY_EXPECT_NE(listener.verification_id(), "");
920-
LogDebug("Signing in with verification code.");
921-
const firebase::auth::Credential phone_credential =
922-
phone_provider.GetCredential(listener.verification_id().c_str(),
923-
kPhoneAuthTestVerificationCode);
924-
925-
FLAKY_WAIT_FOR_COMPLETION(
926-
auth_->SignInWithCredential(phone_credential),
927-
"SignInWithCredential(PhoneCredential)");
928-
}
929-
}
930-
ProcessEvents(1000);
931-
DeleteUser();
932870

933-
FLAKY_SUCCESS();
934-
})) {
935-
FAIL() << "Phone auth failed, see log for details.";
871+
FLAKY_TEST_SECTION_BEGIN();
872+
873+
firebase::auth::PhoneAuthProvider& phone_provider =
874+
firebase::auth::PhoneAuthProvider::GetInstance(auth_);
875+
LogDebug("Creating listener.");
876+
PhoneListener listener;
877+
LogDebug("Calling VerifyPhoneNumber.");
878+
// Randomly choose one of the phone numbers to avoid collisions.
879+
const int random_phone_number =
880+
app_framework::GetCurrentTimeInMicroseconds() %
881+
kPhoneAuthTestNumPhoneNumbers;
882+
phone_provider.VerifyPhoneNumber(
883+
kPhoneAuthTestPhoneNumbers[random_phone_number], kPhoneAuthTimeoutMs,
884+
nullptr, &listener);
885+
886+
// Wait for OnCodeSent() callback.
887+
int wait_ms = 0;
888+
LogDebug("Waiting for code send.");
889+
while (listener.waiting_to_send_code()) {
890+
if (wait_ms > kPhoneAuthCodeSendWaitMs) break;
891+
ProcessEvents(kWaitIntervalMs);
892+
wait_ms += kWaitIntervalMs;
893+
}
894+
EXPECT_EQ(listener.on_verification_failed_count(), 0);
895+
896+
LogDebug("Waiting for verification ID.");
897+
// Wait for the listener to have a verification ID.
898+
wait_ms = 0;
899+
while (listener.waiting_for_verification_id()) {
900+
if (wait_ms > kPhoneAuthCompletionWaitMs) break;
901+
ProcessEvents(kWaitIntervalMs);
902+
wait_ms += kWaitIntervalMs;
936903
}
904+
if (listener.on_verification_complete_count() > 0) {
905+
LogDebug("Signing in with automatic verification code.");
906+
WaitForCompletion(auth_->SignInWithCredential(listener.credential()),
907+
"SignInWithCredential(PhoneCredential) automatic");
908+
} else if (listener.on_verification_failed_count() > 0) {
909+
FAIL() << "Automatic verification failed.";
910+
} else {
911+
// Did not automatically verify, submit verification code manually.
912+
EXPECT_GT(listener.on_code_auto_retrieval_time_out_count(), 0);
913+
EXPECT_NE(listener.verification_id(), "");
914+
LogDebug("Signing in with verification code.");
915+
const firebase::auth::Credential phone_credential =
916+
phone_provider.GetCredential(listener.verification_id().c_str(),
917+
kPhoneAuthTestVerificationCode);
918+
919+
WaitForCompletion(auth_->SignInWithCredential(phone_credential),
920+
"SignInWithCredential(PhoneCredential)");
921+
}
922+
923+
ProcessEvents(1000);
924+
DeleteUser();
925+
926+
FLAKY_TEST_SECTION_END();
937927
}
938928

939929
#if defined(ENABLE_OAUTH_TESTS)

0 commit comments

Comments
 (0)