Skip to content

Commit 3250ce4

Browse files
committed
Add in timeout to the test
1 parent 065c47b commit 3250ce4

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

analytics/integration_test/src/integration_test.cc

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
#include <inttypes.h>
1616

1717
#include <algorithm>
18+
#include <chrono>
1819
#include <cstdio>
1920
#include <cstdlib>
2021
#include <cstring>
2122
#include <ctime>
23+
#include <future>
2224

2325
#include "app_framework.h" // NOLINT
2426
#include "firebase/analytics.h"
@@ -245,21 +247,31 @@ TEST_F(FirebaseAnalyticsTest, TestSetProperties) {
245247
#if defined(_WIN32)
246248
TEST_F(FirebaseAnalyticsTest, TestSetLogCallback) {
247249
std::promise<void> finishedPromise;
248-
std::future<void> finished Future = readyPromise.get_future();
250+
std::future<void> finished = finishedPromise.get_future();
249251
bool log_callback_called = false;
252+
250253
firebase::analytics::SetLogCallback(
251254
[&](firebase::LogLevel log_level, const char* message) {
252255
log_callback_called = true;
253256
finishedPromise.set_value();
254257
});
255-
// Log an event with an invalid parameter to trigger a log message.
258+
259+
// Trigger the event
256260
const firebase::analytics::Parameter kInvalidParameters[] = {
257261
firebase::analytics::Parameter("invalid_character_!", 5),
258262
};
259263
firebase::analytics::LogEvent(
260264
"invalid_event", kInvalidParameters,
261265
sizeof(kInvalidParameters) / sizeof(kInvalidParameters[0]));
262-
readyPromise.set_value();
266+
267+
// Idiomatic Timeout Handling
268+
if (finished.wait_for(std::chrono::seconds(5)) ==
269+
std::future_status::timeout) {
270+
ADD_FAILURE() << "Timed out waiting for log callback";
271+
} else {
272+
finished.get(); // Synchronizes threads and propagates any exceptions
273+
}
274+
263275
EXPECT_TRUE(log_callback_called);
264276
firebase::analytics::SetLogCallback(nullptr);
265277
}

0 commit comments

Comments
 (0)