From 0b249c24bffaa1652c3e2971cc2844aa36fd53f3 Mon Sep 17 00:00:00 2001 From: minyue-webrtc Date: Mon, 10 Jul 2017 11:52:21 +0200 Subject: [PATCH] Refactor gunit for synergy to gtest. BUG=webrtc:7958 Change-Id: I83ff689cef022967e6c58df5eaee8de6fc5cdba8 Reviewed-on: https://chromium-review.googlesource.com/563311 Reviewed-by: Taylor Brandstetter Cr-Commit-Position: refs/heads/master@{#18943} --- webrtc/rtc_base/gunit.h | 116 ++++++++++++++++++++++------------------ 1 file changed, 63 insertions(+), 53 deletions(-) diff --git a/webrtc/rtc_base/gunit.h b/webrtc/rtc_base/gunit.h index 3260019e0a..6f4e419a9a 100644 --- a/webrtc/rtc_base/gunit.h +++ b/webrtc/rtc_base/gunit.h @@ -43,52 +43,60 @@ } while (0) // The typical EXPECT_XXXX and ASSERT_XXXXs, but done until true or a timeout. -#define EXPECT_TRUE_WAIT(ex, timeout) \ - do { \ - bool res; \ - WAIT_(ex, timeout, res); \ - if (!res) EXPECT_TRUE(ex); \ - } while (0) +// One can add failure message by appending "<< msg". +#define EXPECT_TRUE_WAIT(ex, timeout) \ + GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ + if (bool res = true) { \ + WAIT_(ex, timeout, res); \ + if (!res) \ + goto GTEST_CONCAT_TOKEN_(gunit_label_, __LINE__); \ + } else \ + GTEST_CONCAT_TOKEN_(gunit_label_, __LINE__) : EXPECT_TRUE(ex) -#define EXPECT_EQ_WAIT(v1, v2, timeout) \ - do { \ - bool res; \ - WAIT_(v1 == v2, timeout, res); \ - if (!res) EXPECT_EQ(v1, v2); \ - } while (0) +#define EXPECT_EQ_WAIT(v1, v2, timeout) \ + GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ + if (bool res = true) { \ + WAIT_(v1 == v2, timeout, res); \ + if (!res) \ + goto GTEST_CONCAT_TOKEN_(gunit_label_, __LINE__); \ + } else \ + GTEST_CONCAT_TOKEN_(gunit_label_, __LINE__) : EXPECT_EQ(v1, v2) -#define ASSERT_TRUE_WAIT(ex, timeout) \ - do { \ - bool res; \ - WAIT_(ex, timeout, res); \ - if (!res) ASSERT_TRUE(ex); \ - } while (0) +#define ASSERT_TRUE_WAIT(ex, timeout) \ + GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ + if (bool res = true) { \ + WAIT_(ex, timeout, res); \ + if (!res) \ + goto GTEST_CONCAT_TOKEN_(gunit_label_, __LINE__); \ + } else \ + GTEST_CONCAT_TOKEN_(gunit_label_, __LINE__) : ASSERT_TRUE(ex) -#define ASSERT_EQ_WAIT(v1, v2, timeout) \ - do { \ - bool res; \ - WAIT_(v1 == v2, timeout, res); \ - if (!res) ASSERT_EQ(v1, v2); \ - } while (0) +#define ASSERT_EQ_WAIT(v1, v2, timeout) \ + GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ + if (bool res = true) { \ + WAIT_(v1 == v2, timeout, res); \ + if (!res) \ + goto GTEST_CONCAT_TOKEN_(gunit_label_, __LINE__); \ + } else \ + GTEST_CONCAT_TOKEN_(gunit_label_, __LINE__) : ASSERT_EQ(v1, v2) // Version with a "soft" timeout and a margin. This logs if the timeout is // exceeded, but it only fails if the expression still isn't true after the // margin time passes. #define EXPECT_TRUE_WAIT_MARGIN(ex, timeout, margin) \ - do { \ - bool res; \ + GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ + if (bool res = true) { \ WAIT_(ex, timeout, res); \ - if (res) { \ + if (res) \ break; \ - } \ LOG(LS_WARNING) << "Expression " << #ex << " still not true after " \ << (timeout) << "ms; waiting an additional " << margin \ << "ms"; \ WAIT_(ex, margin, res); \ - if (!res) { \ - EXPECT_TRUE(ex); \ - } \ - } while (0) + if (!res) \ + goto GTEST_CONCAT_TOKEN_(gunit_label_, __LINE__); \ + } else \ + GTEST_CONCAT_TOKEN_(gunit_label_, __LINE__) : EXPECT_TRUE(ex) // Wait until "ex" is true, or "timeout" expires, using fake clock where // messages are processed every millisecond. @@ -123,28 +131,30 @@ } while (0) #define EXPECT_EQ_SIMULATED_WAIT(v1, v2, timeout, clock) \ - do { \ - bool res; \ - SIMULATED_WAIT_(v1 == v2, timeout, res, clock); \ - if (!res) { \ - EXPECT_EQ(v1, v2); \ - } \ - } while (0) - -#define ASSERT_TRUE_SIMULATED_WAIT(ex, timeout, clock) \ - do { \ - bool res; \ - SIMULATED_WAIT_(ex, timeout, res, clock); \ - if (!res) \ - ASSERT_TRUE(ex); \ - } while (0) - -#define ASSERT_EQ_SIMULATED_WAIT(v1, v2, timeout, clock) \ - do { \ - bool res; \ + GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ + if (bool res = true) { \ SIMULATED_WAIT_(v1 == v2, timeout, res, clock); \ if (!res) \ - ASSERT_EQ(v1, v2); \ - } while (0) + goto GTEST_CONCAT_TOKEN_(gunit_label_, __LINE__); \ + } else \ + GTEST_CONCAT_TOKEN_(gunit_label_, __LINE__) : EXPECT_EQ(v1, v2) + +#define ASSERT_TRUE_SIMULATED_WAIT(ex, timeout, clock) \ + GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ + if (bool res = true) { \ + SIMULATED_WAIT_(ex, timeout, res, clock); \ + if (!res) \ + goto GTEST_CONCAT_TOKEN_(gunit_label_, __LINE__); \ + } else \ + GTEST_CONCAT_TOKEN_(gunit_label_, __LINE__) : ASSERT_TRUE(ex) + +#define ASSERT_EQ_SIMULATED_WAIT(v1, v2, timeout, clock) \ + GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ + if (bool res = true) { \ + SIMULATED_WAIT_(v1 == v2, timeout, res, clock); \ + if (!res) \ + goto GTEST_CONCAT_TOKEN_(gunit_label_, __LINE__); \ + } else \ + GTEST_CONCAT_TOKEN_(gunit_label_, __LINE__) : ASSERT_EQ(v1, v2) #endif // WEBRTC_RTC_BASE_GUNIT_H_