Skip to content

Deflake a few flaky tests #516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 84 additions & 20 deletions installations/integration_test/src/integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -237,34 +237,98 @@ TEST_F(FirebaseInstallationsTest, TestCanGetToken) {
}

TEST_F(FirebaseInstallationsTest, TestGettingTokenTwiceMatches) {
firebase::Future<std::string> token = installations_->GetToken(false);
WaitForCompletion(token, "GetToken");
EXPECT_NE(*token.result(), "");
std::string first_token = *token.result();
token = installations_->GetToken(false);
WaitForCompletion(token, "GetToken 2");
EXPECT_EQ(*token.result(), first_token);
if (!RunFlakyBlock(
[](firebase::installations::Installations* installations) {
firebase::Future<std::string> token =
installations->GetToken(false);
WaitForCompletionAnyResult(token, "GetToken");
if (token.error() != 0) {
LogError("GetToken returned error %d: %s", token.error(),
token.error_message());
return false;
}
if (*token.result() == "") {
LogError("GetToken returned blank");
return false;
}
std::string first_token = *token.result();
token = installations->GetToken(false);
WaitForCompletionAnyResult(token, "GetToken 2");
if (token.error() != 0) {
LogError("GetId 2 returned error %d: %s", token.error(),
token.error_message());
return false;
}
if (*token.result() == "") {
LogError("GetToken 2 returned blank");
return false;
}
if (*token.result() != first_token) {
LogError(
"GetToken 2 returned non-matching token: first(%s) vs "
"second(%s)",
first_token.c_str(), token.result()->c_str());
return false;
}
return true;
},
installations_)) {
FAIL() << "Test failed, check error log for details.";
}
}

TEST_F(FirebaseInstallationsTest, TestDeleteGivesNewTokenNextTime) {
firebase::Future<std::string> token = installations_->GetToken(false);
WaitForCompletion(token, "GetToken");
EXPECT_NE(*token.result(), "");
std::string first_token = *token.result();
if (!RunFlakyBlock(
[](firebase::installations::Installations* installations) {
firebase::Future<std::string> token =
installations->GetToken(false);
WaitForCompletionAnyResult(token, "GetToken");
if (token.error() != 0) {
LogError("GetToken returned error %d: %s", token.error(),
token.error_message());
return false;
}
if (*token.result() == "") {
LogError("GetToken returned blank");
return false;
}
std::string first_token = *token.result();

firebase::Future<void> del = installations_->Delete();
WaitForCompletion(del, "Delete");
firebase::Future<void> del = installations->Delete();
WaitForCompletionAnyResult(del, "Delete");
if (del.error() != 0) {
LogError("Delete returned error %d: %s", token.error(),
token.error_message());
return false;
}

// Ensure that we now get a different installations token.
token = installations_->GetToken(false);
WaitForCompletion(token, "GetToken 2");
EXPECT_NE(*token.result(), "");
// Ensure that we now get a different installations token.
token = installations->GetToken(false);
WaitForCompletionAnyResult(token, "GetToken 2");
if (token.error() != 0) {
LogError("GetToken 2 returned error %d: %s", token.error(),
token.error_message());
return false;
}
if (*token.result() == "") {
LogError("GetToken 2 returned blank");
return false;
}
#if defined(__ANDROID__) || (defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE)
// Desktop is a stub and returns the same token, but on mobile it should
// return a new token.
EXPECT_NE(*token.result(), first_token);
// Desktop is a stub and returns the same token, but on mobile it
// should return a new token.
if (*token.result() == first_token) {
LogError("Tokens match (should be different): %s",
first_token.c_str());
return false;
}
#endif // defined(__ANDROID__) || (defined(TARGET_OS_IPHONE) &&
// TARGET_OS_IPHONE)
return true;
},
installations_)) {
FAIL() << "Test failed, check error log for details.";
}
}

TEST_F(FirebaseInstallationsTest, TestCanGetIdAndTokenTogether) {
Expand Down
111 changes: 80 additions & 31 deletions messaging/integration_test/src/integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ const int kTimeoutSeconds = 120;
const char kTestingNotificationKey[] = "fcm_testing_notification";

using app_framework::LogDebug;
using app_framework::LogError;
using app_framework::LogInfo;
using app_framework::LogWarning;

using app_framework::GetCurrentTimeInMicroseconds;
using app_framework::PathForResource;
Expand Down Expand Up @@ -90,7 +92,6 @@ class FirebaseMessagingTest : public FirebaseTest {
const char* notification_body,
const std::map<std::string, std::string>& message_fields);

protected:
// Get a unique message ID so we can confirm the correct message is being
// received.
std::string GetUniqueMessageId();
Expand All @@ -106,6 +107,7 @@ class FirebaseMessagingTest : public FirebaseTest {
bool WaitForMessage(firebase::messaging::Message* message_out,
int timeout = kTimeoutSeconds);

protected:
static firebase::App* shared_app_;
static firebase::messaging::PollableListener* shared_listener_;
static std::string* shared_token_;
Expand Down Expand Up @@ -218,6 +220,13 @@ bool FirebaseMessagingTest::CreateTestMessage(
// Don't send HTTP requests in stub mode.
return false;
}
if (strcmp(kFcmServerKey, "REPLACE_WITH_YOUR_SERVER_KEY") == 0) {
LogWarning(
"Please put your Firebase Cloud Messaging server key in "
"kFcmServerKey.");
LogWarning("Without a server key, most of these tests will fail.");
return false;
}
std::map<std::string, std::string> headers;
headers.insert(std::make_pair("Content-type", "application/json"));
headers.insert(
Expand Down Expand Up @@ -504,37 +513,77 @@ TEST_F(FirebaseMessagingTest, TestSendMessageToTopic) {

EXPECT_TRUE(RequestPermission());
EXPECT_TRUE(WaitForToken());
std::string unique_id = GetUniqueMessageId();
const char kNotificationTitle[] = "Topic Test";
const char kNotificationBody[] = "Topic Test notification body";
// Create a somewhat unique topic name using 2 digits near the end of
// unique_id (but not the LAST 2 digits, due to timestamp resolution on some
// platforms).
std::string unique_id_tag =
(unique_id.length() >= 7 ? unique_id.substr(unique_id.length() - 5, 2)
: "00");
std::string topic = "FCMTestTopic" + unique_id_tag;
EXPECT_TRUE(WaitForCompletion(firebase::messaging::Subscribe(topic.c_str()),
"Subscribe"));
SendTestMessage(("/topics/" + topic).c_str(), kNotificationTitle,
kNotificationBody,
{{"message", "Hello, world!"}, {"unique_id", unique_id}});
firebase::messaging::Message message;
EXPECT_TRUE(WaitForMessage(&message));
EXPECT_EQ(message.data["unique_id"], unique_id);
if (message.notification) {
EXPECT_EQ(message.notification->title, kNotificationTitle);
EXPECT_EQ(message.notification->body, kNotificationBody);
}

EXPECT_TRUE(WaitForCompletion(firebase::messaging::Unsubscribe(topic.c_str()),
"Unsubscribe"));

// Ensure that we *don't* receive a message now.
unique_id = GetUniqueMessageId();
SendTestMessage(("/topics/" + topic).c_str(), "Topic Title 2", "Topic Body 2",
{{"message", "Hello, world!"}, {"unique_id", unique_id}});
EXPECT_FALSE(WaitForMessage(&message, 5));
if (!RunFlakyBlock(
[](FirebaseMessagingTest* this_) {
std::string unique_id = this_->GetUniqueMessageId();
const char kNotificationTitle[] = "Topic Test";
const char kNotificationBody[] = "Topic Test notification body";
// Create a somewhat unique topic name using 2 digits near the end
// of unique_id (but not the LAST 2 digits, due to timestamp
// resolution on some platforms).
std::string unique_id_tag =
(unique_id.length() >= 7
? unique_id.substr(unique_id.length() - 5, 2)
: "00");
std::string topic = "FCMTestTopic" + unique_id_tag;
firebase::Future<void> sub =
firebase::messaging::Subscribe(topic.c_str());
WaitForCompletionAnyResult(sub, "Subscribe");
if (sub.error() != 0) {
LogError("Subscribe returned error %d: %s", sub.error(),
sub.error_message());
}
this_->SendTestMessage(
("/topics/" + topic).c_str(), kNotificationTitle,
kNotificationBody,
{{"message", "Hello, world!"}, {"unique_id", unique_id}});
firebase::messaging::Message message;
if (!this_->WaitForMessage(&message)) {
LogError("WaitForMessage failed");
return false;
}
if (message.data["unique_id"] != unique_id) {
LogError("unique_id doesn't match: got %s, expected %s",
unique_id.c_str(), message.data["unique_id"].c_str());
return false;
}
if (message.notification) {
if (message.notification->title != kNotificationTitle) {
LogError(
"notification.title doesn't match: got %s, expected %s",
message.notification->title.c_str(), kNotificationTitle);
return false;
}
if (message.notification->body != kNotificationBody) {
LogError("notification.body doesn't match: got %s, expected %s",
message.notification->body.c_str(), kNotificationBody);
return false;
}
}
firebase::Future<void> unsub =
firebase::messaging::Unsubscribe(topic.c_str());
WaitForCompletionAnyResult(unsub, "Unsubscribe");
if (unsub.error() != 0) {
LogError("Unsubscribe returned error %d: %s", unsub.error(),
unsub.error_message());
}

// Ensure that we *don't* receive a message now.
unique_id = this_->GetUniqueMessageId();
this_->SendTestMessage(
("/topics/" + topic).c_str(), "Topic Title 2", "Topic Body 2",
{{"message", "Hello, world!"}, {"unique_id", unique_id}});
if (this_->WaitForMessage(&message, 5)) {
LogError("WaitForMessage received a message but shouldn't have.");
return false;
}

return true;
},
this)) {
FAIL() << "Test failed, check error log for details.";
}
}

TEST_F(FirebaseMessagingTest, TestChangingListener) {
Expand Down