diff --git a/p2p/base/p2p_transport_channel_unittest.cc b/p2p/base/p2p_transport_channel_unittest.cc index 422ffff822..22feeccbc3 100644 --- a/p2p/base/p2p_transport_channel_unittest.cc +++ b/p2p/base/p2p_transport_channel_unittest.cc @@ -3204,7 +3204,10 @@ TEST_F(P2PTransportChannelMultihomedTest, TestPreferWifiToWifiConnection) { // Create channels and let them go writable, as usual. CreateChannels(); - EXPECT_TRUE_WAIT_MARGIN(CheckConnected(ep1_ch1(), ep2_ch1()), 1000, 1000); + EXPECT_THAT( + webrtc::WaitUntil([&]() { return CheckConnected(ep1_ch1(), ep2_ch1()); }, + IsTrue()), + webrtc::IsRtcOk()); // Need to wait to make sure the connections on both networks are writable. EXPECT_THAT(webrtc::WaitUntil( [&] { @@ -3234,9 +3237,13 @@ TEST_F(P2PTransportChannelMultihomedTest, TestPreferWifiOverCellularNetwork) { // Create channels and let them go writable, as usual. CreateChannels(); - EXPECT_TRUE_WAIT_MARGIN(CheckCandidatePairAndConnected(ep1_ch1(), ep2_ch1(), - cellular[0], wifi[1]), - 1000, 1000); + EXPECT_THAT(webrtc::WaitUntil( + [&]() { + return CheckCandidatePairAndConnected(ep1_ch1(), ep2_ch1(), + cellular[0], wifi[1]); + }, + IsTrue()), + webrtc::IsRtcOk()); DestroyChannels(); } @@ -3255,7 +3262,10 @@ TEST_F(P2PTransportChannelMultihomedTest, TestPingBackupConnectionRate) { // Create channels and let them go writable, as usual. CreateChannels(); - EXPECT_TRUE_WAIT_MARGIN(CheckConnected(ep1_ch1(), ep2_ch1()), 1000, 1000); + EXPECT_THAT( + webrtc::WaitUntil([&] { return CheckConnected(ep1_ch1(), ep2_ch1()); }, + IsTrue()), + webrtc::IsRtcOk()); int backup_ping_interval = 2000; ep2_ch1()->SetIceConfig( CreateIceConfig(2000, GATHER_ONCE, backup_ping_interval)); @@ -3300,7 +3310,10 @@ TEST_F(P2PTransportChannelMultihomedTest, TestStableWritableRate) { // Create channels and let them go writable, as usual. CreateChannels(); - EXPECT_TRUE_WAIT_MARGIN(CheckConnected(ep1_ch1(), ep2_ch1()), 1000, 1000); + EXPECT_THAT( + webrtc::WaitUntil([&] { return CheckConnected(ep1_ch1(), ep2_ch1()); }, + IsTrue()), + webrtc::IsRtcOk()); // Set a value larger than the default value of 2500 ms int ping_interval_ms = 3456; IceConfig config = CreateIceConfig(2 * ping_interval_ms, GATHER_ONCE); @@ -3428,8 +3441,11 @@ TEST_F(P2PTransportChannelMultihomedTest, CreateChannels(continual_gathering_config, continual_gathering_config); SetAllocatorFlags(0, kOnlyLocalPorts); SetAllocatorFlags(1, kOnlyLocalPorts); - EXPECT_TRUE_WAIT_MARGIN(CheckConnected(ep1_ch1(), ep2_ch1()), kDefaultTimeout, - kDefaultTimeout); + EXPECT_THAT( + webrtc::WaitUntil( + [&] { return CheckConnected(ep1_ch1(), ep2_ch1()); }, IsTrue(), + {.timeout = webrtc::TimeDelta::Millis(kDefaultTimeout)}), + webrtc::IsRtcOk()); // Add a new wifi interface on end point 2. We should expect a new connection // to be created and the new one will be the best connection. diff --git a/rtc_base/gunit.h b/rtc_base/gunit.h index 759b377aa2..101f07a4e6 100644 --- a/rtc_base/gunit.h +++ b/rtc_base/gunit.h @@ -39,62 +39,6 @@ } \ } while (0) -// The typical EXPECT_XXXX and ASSERT_XXXXs, but done until true or a timeout. -// 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) \ - 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) \ - 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) \ - 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) \ - GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ - if (bool res = true) { \ - WAIT_(ex, timeout, res); \ - if (res) \ - break; \ - RTC_LOG(LS_WARNING) << "Expression " << #ex << " still not true after " \ - << (timeout) << "ms; waiting an additional " << margin \ - << "ms"; \ - WAIT_(ex, margin, res); \ - 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. // TODO(pthatcher): Allow tests to control how many milliseconds to advance. @@ -104,54 +48,4 @@ (clock).AdvanceTime(webrtc::TimeDelta::Millis(1)); \ } -// This returns the result of the test in res, so that we don't re-evaluate -// the expression in the XXXX_WAIT macros below, since that causes problems -// when the expression is only true the first time you check it. -#define SIMULATED_WAIT_(ex, timeout, res, clock) \ - do { \ - int64_t start = rtc::TimeMillis(); \ - res = (ex); \ - while (!res && rtc::TimeMillis() < start + (timeout)) { \ - (clock).AdvanceTime(webrtc::TimeDelta::Millis(1)); \ - res = (ex); \ - } \ - } while (0) - -// The typical EXPECT_XXXX, but done until true or a timeout with a fake clock. -#define EXPECT_TRUE_SIMULATED_WAIT(ex, timeout, clock) \ - do { \ - bool res; \ - SIMULATED_WAIT_(ex, timeout, res, clock); \ - if (!res) { \ - EXPECT_TRUE(ex); \ - } \ - } while (0) - -#define EXPECT_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__) : 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 // RTC_BASE_GUNIT_H_ diff --git a/rtc_tools/network_tester/BUILD.gn b/rtc_tools/network_tester/BUILD.gn index f8ce64e215..2ad4abd328 100644 --- a/rtc_tools/network_tester/BUILD.gn +++ b/rtc_tools/network_tester/BUILD.gn @@ -82,11 +82,13 @@ if (rtc_enable_protobuf) { deps = [ ":network_tester", + "../../api:rtc_error_matchers", "../../rtc_base:gunit_helpers", "../../rtc_base:random", "../../rtc_base:threading", "../../test:fileutils", "../../test:test_support", + "../../test:wait_until", "//testing/gtest", ] diff --git a/rtc_tools/network_tester/network_tester_unittest.cc b/rtc_tools/network_tester/network_tester_unittest.cc index ea0ebf98f8..1596ad5462 100644 --- a/rtc_tools/network_tester/network_tester_unittest.cc +++ b/rtc_tools/network_tester/network_tester_unittest.cc @@ -12,11 +12,13 @@ #include -#include "rtc_base/gunit.h" +#include "api/test/rtc_error_matchers.h" #include "rtc_base/random.h" #include "rtc_tools/network_tester/test_controller.h" +#include "test/gmock.h" #include "test/gtest.h" #include "test/testsupport/file_utils.h" +#include "test/wait_until.h" namespace webrtc { @@ -37,7 +39,10 @@ TEST(NetworkTesterTest, ServerClient) { webrtc::test::ResourcePath("network_tester/server_config", "dat"), webrtc::test::OutputPath() + "server_packet_log.dat"); client.SendConnectTo("127.0.0.1", port); - EXPECT_TRUE_WAIT(server.IsTestDone() && client.IsTestDone(), 2000); + EXPECT_THAT( + WaitUntil([&] { return server.IsTestDone() && client.IsTestDone(); }, + ::testing::IsTrue()), + IsRtcOk()); } } // namespace webrtc diff --git a/sdk/BUILD.gn b/sdk/BUILD.gn index 4a9d1f66a9..e76b68c92e 100644 --- a/sdk/BUILD.gn +++ b/sdk/BUILD.gn @@ -1195,6 +1195,7 @@ if (is_ios || is_mac) { ":videosource_objc", ":videotoolbox_objc", "../api:libjingle_peerconnection_api", + "../api:rtc_error_matchers", "../api:scoped_refptr", "../api/audio:audio_device", "../api/audio:audio_processing", @@ -1215,6 +1216,8 @@ if (is_ios || is_mac) { "../rtc_base:rtc_event", "../rtc_base/system:unused", "../system_wrappers", + "../test:test_support", + "../test:wait_until", "//third_party/libyuv", ] diff --git a/sdk/objc/unittests/RTCFileVideoCapturer_xctest.mm b/sdk/objc/unittests/RTCFileVideoCapturer_xctest.mm index 7eb9efd0f5..9e5f227913 100644 --- a/sdk/objc/unittests/RTCFileVideoCapturer_xctest.mm +++ b/sdk/objc/unittests/RTCFileVideoCapturer_xctest.mm @@ -12,10 +12,12 @@ #import +#include "api/test/rtc_error_matchers.h" #include "rtc_base/gunit.h" +#include "test/gmock.h" +#include "test/wait_until.h" NSString *const kTestFileName = @"foreman.mp4"; -static const int kTestTimeoutMs = 5 * 1000; // 5secs. @interface MockCapturerDelegate : NSObject @@ -66,7 +68,12 @@ NS_CLASS_AVAILABLE_IOS(10) [self.capturer startCapturingFromFileNamed:@"not_in_bundle.mov" onError:errorBlock]; - ASSERT_TRUE_WAIT(errorOccured, kTestTimeoutMs); + EXPECT_THAT(webrtc::WaitUntil( + ^() { + return errorOccured; + }, + ::testing::IsTrue()), + webrtc::IsRtcOk()); } - (void)testSecondStartCaptureCallFails { @@ -87,7 +94,12 @@ NS_CLASS_AVAILABLE_IOS(10) [self.capturer startCapturingFromFileNamed:kTestFileName onError:secondErrorBlock]; - ASSERT_TRUE_WAIT(secondError, kTestTimeoutMs); + EXPECT_THAT(webrtc::WaitUntil( + ^() { + return secondError; + }, + ::testing::IsTrue()), + webrtc::IsRtcOk()); } - (void)testStartStopCapturer { @@ -111,7 +123,12 @@ NS_CLASS_AVAILABLE_IOS(10) [self.capturer stopCapture]; done = YES; }); - WAIT(done, kTestTimeoutMs); + EXPECT_THAT(webrtc::WaitUntil( + ^() { + return done; + }, + ::testing::IsTrue()), + webrtc::IsRtcOk()); capturedFramesAfterStop = self.mockDelegate.capturedFramesCount; ASSERT_TRUE(capturedFrames != -1); diff --git a/test/fuzzers/BUILD.gn b/test/fuzzers/BUILD.gn index b7c7fc621d..773dca56e6 100644 --- a/test/fuzzers/BUILD.gn +++ b/test/fuzzers/BUILD.gn @@ -461,7 +461,9 @@ if (!build_with_chromium) { webrtc_fuzzer_test("sdp_integration_fuzzer") { sources = [ "sdp_integration_fuzzer.cc" ] deps = [ + "..:wait_until", "../../api:libjingle_peerconnection_api", + "../../api:rtc_error_matchers", "../../pc:integration_test_helpers", "../../pc:libjingle_peerconnection", "../../test:test_support", diff --git a/test/fuzzers/sdp_integration_fuzzer.cc b/test/fuzzers/sdp_integration_fuzzer.cc index 9073818de4..81528f4b60 100644 --- a/test/fuzzers/sdp_integration_fuzzer.cc +++ b/test/fuzzers/sdp_integration_fuzzer.cc @@ -12,7 +12,9 @@ #include #include "absl/strings/string_view.h" +#include "api/test/rtc_error_matchers.h" #include "pc/test/integration_test_helpers.h" +#include "test/wait_until.h" namespace webrtc { @@ -35,14 +37,18 @@ class FuzzerTest : public PeerConnectionIntegrationBaseTest { caller()->pc()->SetRemoteDescription(std::move(sdp), srd_observer); // Wait a short time for observer to be called. Timeout is short // because the fuzzer should be trying many branches. - EXPECT_TRUE_WAIT(srd_observer->called(), 100); + EXPECT_THAT( + WaitUntil([&] { return srd_observer->called(); }, ::testing::IsTrue()), + IsRtcOk()); // If set-remote-description was successful, try to answer. auto sld_observer = rtc::make_ref_counted(); if (srd_observer->error().ok()) { caller()->pc()->SetLocalDescription(sld_observer); - EXPECT_TRUE_WAIT(sld_observer->called(), 100); + EXPECT_THAT(WaitUntil([&] { return sld_observer->called(); }, + ::testing::IsTrue()), + IsRtcOk()); } // If there is an EXPECT failure, die here. RTC_CHECK(!HasFailure());