Skip to content

Commit 1720d49

Browse files
committed
Add FLAKY_EXPECT_* and FLAKY_WAIT_FOR_COMPLETION.
Also remove test failure if server key unspecified.
1 parent bb3c48c commit 1720d49

File tree

3 files changed

+96
-145
lines changed

3 files changed

+96
-145
lines changed

installations/integration_test/src/integration_test.cc

Lines changed: 28 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -147,30 +147,15 @@ TEST_F(FirebaseInstallationsTest, TestGettingIdTwiceMatches) {
147147
if (!RunFlakyBlock(
148148
[](firebase::installations::Installations* installations) {
149149
firebase::Future<std::string> id = installations->GetId();
150-
WaitForCompletionAnyResult(id, "GetId");
151-
if (id.error() != 0) {
152-
LogError("GetId returned error %d: %s", id.error(),
153-
id.error_message());
154-
return false;
155-
}
156-
if (*id.result() == "") {
157-
LogError("GetId returned blank");
158-
return false;
159-
}
150+
FLAKY_WAIT_FOR_COMPLETION(id, "GetId");
151+
FLAKY_EXPECT_NE(*id.result(), ""); // ensure non-blank
160152
std::string first_id = *id.result();
161153
id = installations->GetId();
162-
WaitForCompletionAnyResult(id, "GetId 2");
163-
if (id.error() != 0) {
164-
LogError("GetId 2 returned error %d: %s", id.error(),
165-
id.error_message());
166-
return false;
167-
}
168-
if (*id.result() != first_id) {
169-
LogError(
170-
"GetId 2 returned non-matching ID: first(%s) vs second(%s)",
171-
first_id.c_str(), id.result()->c_str());
172-
return false;
173-
}
154+
FLAKY_WAIT_FOR_COMPLETION(id, "GetId 2");
155+
FLAKY_EXPECT_NE(*id.result(), ""); // ensure non-blank
156+
157+
// Ensure the second ID returned is the same as the first.
158+
FLAKY_EXPECT_EQ(*id.result(), first_id);
174159
return true;
175160
},
176161
installations_)) {
@@ -182,47 +167,25 @@ TEST_F(FirebaseInstallationsTest, TestDeleteGivesNewIdNextTime) {
182167
if (!RunFlakyBlock(
183168
[](firebase::installations::Installations* installations) {
184169
firebase::Future<std::string> id = installations->GetId();
185-
WaitForCompletionAnyResult(id, "GetId");
186-
if (id.error() != 0) {
187-
LogError("GetId returned error %d: %s", id.error(),
188-
id.error_message());
189-
return false;
190-
}
191-
if (*id.result() == "") {
192-
LogError("GetId returned blank");
193-
return false;
194-
}
170+
FLAKY_WAIT_FOR_COMPLETION(id, "GetId");
171+
FLAKY_EXPECT_NE(*id.result(), ""); // ensure non-blank
195172
std::string first_id = *id.result();
196173

197174
firebase::Future<void> del = installations->Delete();
198-
WaitForCompletionAnyResult(del, "Delete");
199-
if (del.error() != 0) {
200-
LogError("Delete returned error %d: %s", id.error(),
201-
id.error_message());
202-
return false;
203-
}
175+
FLAKY_WAIT_FOR_COMPLETION(del, "Delete");
204176

205177
// Ensure that we now get a different installations id.
206178
id = installations->GetId();
207-
WaitForCompletionAnyResult(id, "GetId 2");
208-
if (id.error() != 0) {
209-
LogError("GetId 2 returned error %d: %s", id.error(),
210-
id.error_message());
211-
return false;
212-
}
213-
if (*id.result() == "") {
214-
LogError("GetId 2 returned blank");
215-
return false;
216-
}
179+
FLAKY_WAIT_FOR_COMPLETION(id, "GetId 2");
180+
FLAKY_EXPECT_NE(*id.result(), ""); // ensure non-blank
181+
217182
#if defined(__ANDROID__) || (defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE)
218183
// Desktop is a stub and returns the same ID, but on mobile it
219184
// should return a new ID.
220-
if (*id.result() == first_id) {
221-
LogError("IDs match (should be different): %s", first_id.c_str());
222-
return false;
223-
}
185+
FLAKY_EXPECT_NE(*id.result(), first_id);
224186
#endif // defined(__ANDROID__) || (defined(TARGET_OS_IPHONE) &&
225187
// TARGET_OS_IPHONE)
188+
226189
return true;
227190
},
228191
installations_)) {
@@ -241,35 +204,13 @@ TEST_F(FirebaseInstallationsTest, TestGettingTokenTwiceMatches) {
241204
[](firebase::installations::Installations* installations) {
242205
firebase::Future<std::string> token =
243206
installations->GetToken(false);
244-
WaitForCompletionAnyResult(token, "GetToken");
245-
if (token.error() != 0) {
246-
LogError("GetToken returned error %d: %s", token.error(),
247-
token.error_message());
248-
return false;
249-
}
250-
if (*token.result() == "") {
251-
LogError("GetToken returned blank");
252-
return false;
253-
}
207+
FLAKY_WAIT_FOR_COMPLETION(token, "GetToken");
208+
FLAKY_EXPECT_NE(*token.result(), ""); // ensure non-blank
254209
std::string first_token = *token.result();
255210
token = installations->GetToken(false);
256-
WaitForCompletionAnyResult(token, "GetToken 2");
257-
if (token.error() != 0) {
258-
LogError("GetId 2 returned error %d: %s", token.error(),
259-
token.error_message());
260-
return false;
261-
}
262-
if (*token.result() == "") {
263-
LogError("GetToken 2 returned blank");
264-
return false;
265-
}
266-
if (*token.result() != first_token) {
267-
LogError(
268-
"GetToken 2 returned non-matching token: first(%s) vs "
269-
"second(%s)",
270-
first_token.c_str(), token.result()->c_str());
271-
return false;
272-
}
211+
FLAKY_WAIT_FOR_COMPLETION(token, "GetToken 2");
212+
FLAKY_EXPECT_NE(*token.result(), ""); // ensure non-blank
213+
FLAKY_EXPECT_EQ(*token.result(), first_token);
273214
return true;
274215
},
275216
installations_)) {
@@ -282,48 +223,25 @@ TEST_F(FirebaseInstallationsTest, TestDeleteGivesNewTokenNextTime) {
282223
[](firebase::installations::Installations* installations) {
283224
firebase::Future<std::string> token =
284225
installations->GetToken(false);
285-
WaitForCompletionAnyResult(token, "GetToken");
286-
if (token.error() != 0) {
287-
LogError("GetToken returned error %d: %s", token.error(),
288-
token.error_message());
289-
return false;
290-
}
291-
if (*token.result() == "") {
292-
LogError("GetToken returned blank");
293-
return false;
294-
}
226+
FLAKY_WAIT_FOR_COMPLETION(token, "GetToken");
227+
FLAKY_EXPECT_NE(*token.result(), ""); // ensure non-blank
295228
std::string first_token = *token.result();
296229

297230
firebase::Future<void> del = installations->Delete();
298-
WaitForCompletionAnyResult(del, "Delete");
299-
if (del.error() != 0) {
300-
LogError("Delete returned error %d: %s", token.error(),
301-
token.error_message());
302-
return false;
303-
}
231+
FLAKY_WAIT_FOR_COMPLETION(del, "Delete");
304232

305233
// Ensure that we now get a different installations token.
306234
token = installations->GetToken(false);
307-
WaitForCompletionAnyResult(token, "GetToken 2");
308-
if (token.error() != 0) {
309-
LogError("GetToken 2 returned error %d: %s", token.error(),
310-
token.error_message());
311-
return false;
312-
}
313-
if (*token.result() == "") {
314-
LogError("GetToken 2 returned blank");
315-
return false;
316-
}
235+
FLAKY_WAIT_FOR_COMPLETION(token, "GetToken 2");
236+
FLAKY_EXPECT_NE(*token.result(), ""); // ensure non-blank
237+
317238
#if defined(__ANDROID__) || (defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE)
318239
// Desktop is a stub and returns the same token, but on mobile it
319240
// should return a new token.
320-
if (*token.result() == first_token) {
321-
LogError("Tokens match (should be different): %s",
322-
first_token.c_str());
323-
return false;
324-
}
241+
FLAKY_EXPECT_EQ(*token.result(), first_token);
325242
#endif // defined(__ANDROID__) || (defined(TARGET_OS_IPHONE) &&
326243
// TARGET_OS_IPHONE)
244+
327245
return true;
328246
},
329247
installations_)) {

messaging/integration_test/src/integration_test.cc

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@ bool FirebaseMessagingTest::CreateTestMessage(
225225
"Please put your Firebase Cloud Messaging server key in "
226226
"kFcmServerKey.");
227227
LogWarning("Without a server key, most of these tests will fail.");
228-
return false;
229228
}
230229
std::map<std::string, std::string> headers;
231230
headers.insert(std::make_pair("Content-type", "application/json"));
@@ -529,55 +528,31 @@ TEST_F(FirebaseMessagingTest, TestSendMessageToTopic) {
529528
std::string topic = "FCMTestTopic" + unique_id_tag;
530529
firebase::Future<void> sub =
531530
firebase::messaging::Subscribe(topic.c_str());
532-
WaitForCompletionAnyResult(sub, "Subscribe");
533-
if (sub.error() != 0) {
534-
LogError("Subscribe returned error %d: %s", sub.error(),
535-
sub.error_message());
536-
}
531+
FLAKY_WAIT_FOR_COMPLETION(sub, "Subscribe");
537532
this_->SendTestMessage(
538533
("/topics/" + topic).c_str(), kNotificationTitle,
539534
kNotificationBody,
540535
{{"message", "Hello, world!"}, {"unique_id", unique_id}});
541536
firebase::messaging::Message message;
542-
if (!this_->WaitForMessage(&message)) {
543-
LogError("WaitForMessage failed");
544-
return false;
545-
}
546-
if (message.data["unique_id"] != unique_id) {
547-
LogError("unique_id doesn't match: got %s, expected %s",
548-
unique_id.c_str(), message.data["unique_id"].c_str());
549-
return false;
550-
}
537+
FLAKY_EXPECT_TRUE(this_->WaitForMessage(&message));
538+
539+
FLAKY_EXPECT_EQ(message.data["unique_id"], unique_id);
551540
if (message.notification) {
552-
if (message.notification->title != kNotificationTitle) {
553-
LogError(
554-
"notification.title doesn't match: got %s, expected %s",
555-
message.notification->title.c_str(), kNotificationTitle);
556-
return false;
557-
}
558-
if (message.notification->body != kNotificationBody) {
559-
LogError("notification.body doesn't match: got %s, expected %s",
560-
message.notification->body.c_str(), kNotificationBody);
561-
return false;
562-
}
541+
FLAKY_EXPECT_EQ(message.notification->title, kNotificationTitle);
542+
FLAKY_EXPECT_EQ(message.notification->body, kNotificationBody);
563543
}
564544
firebase::Future<void> unsub =
565545
firebase::messaging::Unsubscribe(topic.c_str());
566-
WaitForCompletionAnyResult(unsub, "Unsubscribe");
567-
if (unsub.error() != 0) {
568-
LogError("Unsubscribe returned error %d: %s", unsub.error(),
569-
unsub.error_message());
570-
}
546+
FLAKY_WAIT_FOR_COMPLETION(unsub, "Unsubscribe");
571547

572548
// Ensure that we *don't* receive a message now.
573549
unique_id = this_->GetUniqueMessageId();
574550
this_->SendTestMessage(
575551
("/topics/" + topic).c_str(), "Topic Title 2", "Topic Body 2",
576552
{{"message", "Hello, world!"}, {"unique_id", unique_id}});
577-
if (this_->WaitForMessage(&message, 5)) {
578-
LogError("WaitForMessage received a message but shouldn't have.");
579-
return false;
580-
}
553+
554+
// If this returns true, it means we received a message but shouldn't have.
555+
FLAKY_EXPECT_FALSE(this_->WaitForMessage(&message, 5));
581556

582557
return true;
583558
},

testing/test_framework/src/firebase_test_framework.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define FIREBASE_TEST_FRAMEWORK_H_ // NOLINT
1717

1818
#include <iostream>
19+
#include <sstream>
1920

2021
#include "app_framework.h" // NOLINT
2122
#include "firebase/app.h"
@@ -179,6 +180,63 @@ namespace firebase_test_framework {
179180
#define DEATHTEST_SIGABRT ""
180181
#endif
181182

183+
184+
// Helper macros to assist with RunFlakyBlock.
185+
// Unlike EXPECT_*, FLAKY_EXPECT_* just prints out the error and returns
186+
// false, which will cause RunFlakyBlock to retry.
187+
#define FLAKY_EXPECT_EQ(a, b) { \
188+
auto a_result = (a); \
189+
auto b_result = (b); \
190+
std::stringstream a_str, b_str; \
191+
a_str << a_result; \
192+
b_str << b_result; \
193+
if ((a_result) != (b_result)) { \
194+
app_framework::LogError( \
195+
"%s and %s expected to be equal, but differ. first(%s) vs second(%s)", \
196+
#a, #b, a_str.str().c_str(), b_str.str().c_str()); \
197+
return false; \
198+
} \
199+
}
200+
201+
#define FLAKY_EXPECT_NE(a, b) { \
202+
auto a_result = (a); \
203+
auto b_result = (b); \
204+
std::stringstream a_str, b_str; \
205+
a_str << a_result; \
206+
b_str << b_result; \
207+
if ((a_result) == (b_result)) { \
208+
app_framework::LogError( \
209+
"%s and %s expected to differ, but are equal. first(%s) vs second(%s)", \
210+
#a, #b, a_str.str().c_str(), b_str.str().c_str()); \
211+
return false; \
212+
} \
213+
}
214+
215+
#define FLAKY_EXPECT_TRUE(a) { \
216+
if (!(a)) { \
217+
app_framework::LogError("%s expected to be true, but was false.", #a); \
218+
return false; \
219+
} \
220+
}
221+
222+
#define FLAKY_EXPECT_FALSE(a) { \
223+
if ((a)) { \
224+
app_framework::LogError("%s expected to be false, but was true.", #a); \
225+
return false; \
226+
} \
227+
}
228+
229+
#define FLAKY_WAIT_FOR_COMPLETION(future, name) { \
230+
auto f = (future); \
231+
WaitForCompletionAnyResult(f, name); \
232+
if (f.error() != 0) { \
233+
app_framework::LogError( \
234+
"%s returned error %d: %s", \
235+
name, f.error(), f.error_message()); \
236+
return false; \
237+
} \
238+
}
239+
182240
class FirebaseTest : public testing::Test {
183241
public:
184242
FirebaseTest();

0 commit comments

Comments
 (0)