|
15 | 15 | #include <inttypes.h> |
16 | 16 |
|
17 | 17 | #include <algorithm> |
| 18 | +#include <chrono> |
18 | 19 | #include <cstdio> |
19 | 20 | #include <cstdlib> |
20 | 21 | #include <cstring> |
21 | 22 | #include <ctime> |
| 23 | +#include <future> |
22 | 24 |
|
23 | 25 | #include "app_framework.h" // NOLINT |
24 | 26 | #include "firebase/analytics.h" |
@@ -245,21 +247,31 @@ TEST_F(FirebaseAnalyticsTest, TestSetProperties) { |
245 | 247 | #if defined(_WIN32) |
246 | 248 | TEST_F(FirebaseAnalyticsTest, TestSetLogCallback) { |
247 | 249 | std::promise<void> finishedPromise; |
248 | | - std::future<void> finished Future = readyPromise.get_future(); |
| 250 | + std::future<void> finished = finishedPromise.get_future(); |
249 | 251 | bool log_callback_called = false; |
| 252 | + |
250 | 253 | firebase::analytics::SetLogCallback( |
251 | 254 | [&](firebase::LogLevel log_level, const char* message) { |
252 | 255 | log_callback_called = true; |
253 | 256 | finishedPromise.set_value(); |
254 | 257 | }); |
255 | | - // Log an event with an invalid parameter to trigger a log message. |
| 258 | + |
| 259 | + // Trigger the event |
256 | 260 | const firebase::analytics::Parameter kInvalidParameters[] = { |
257 | 261 | firebase::analytics::Parameter("invalid_character_!", 5), |
258 | 262 | }; |
259 | 263 | firebase::analytics::LogEvent( |
260 | 264 | "invalid_event", kInvalidParameters, |
261 | 265 | 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 | + |
263 | 275 | EXPECT_TRUE(log_callback_called); |
264 | 276 | firebase::analytics::SetLogCallback(nullptr); |
265 | 277 | } |
|
0 commit comments