diff --git a/tools-webrtc/valgrind/gtest_exclude/webrtc_nonparallel_tests.gtest-memcheck.txt b/tools-webrtc/valgrind/gtest_exclude/webrtc_nonparallel_tests.gtest-memcheck.txt index 50542cd84d..0f866b780b 100644 --- a/tools-webrtc/valgrind/gtest_exclude/webrtc_nonparallel_tests.gtest-memcheck.txt +++ b/tools-webrtc/valgrind/gtest_exclude/webrtc_nonparallel_tests.gtest-memcheck.txt @@ -3,3 +3,6 @@ PhysicalSocketTest.TestUdpReadyToSendIPv4 PhysicalSocketTest.TestUdpIPv4 PhysicalSocketTest.TestSocketRecvTimestampIPv4 +# Relies on actual CPU timing, so can be flaky on memcheck. +CpuTimeTest.TwoThreads +CpuTimeTest.Sleeping diff --git a/webrtc/base/cpu_time_unittest.cc b/webrtc/base/cpu_time_unittest.cc index b6f6766dcc..bfdd7ef4e6 100644 --- a/webrtc/base/cpu_time_unittest.cc +++ b/webrtc/base/cpu_time_unittest.cc @@ -17,6 +17,15 @@ #include "webrtc/system_wrappers/include/cpu_info.h" #include "webrtc/system_wrappers/include/sleep.h" +// Only run these tests on non-instrumented builds, because timing on +// instrumented builds is unreliable, causing the test to be flaky. +#if defined(THREAD_SANITIZER) || defined(MEMORY_SANITIZER) || \ + defined(ADDRESS_SANITIZER) +#define MAYBE_TEST(test_name) DISABLED_##test_name +#else +#define MAYBE_TEST(test_name) test_name +#endif + namespace { const int kAllowedErrorMillisecs = 30; const int kProcessingTimeMillisecs = 300; @@ -38,7 +47,20 @@ bool WorkingFunction(void* counter_pointer) { namespace rtc { -TEST(CpuTimeTest, TwoThreads) { +// A minimal test which can be run on instrumented builds, so that they're at +// least exercising the code to check for memory leaks/etc. +TEST(CpuTimeTest, BasicTest) { + int64_t process_start_time_nanos = GetProcessCpuTimeNanos(); + int64_t thread_start_time_nanos = GetThreadCpuTimeNanos(); + int64_t process_duration_nanos = + GetProcessCpuTimeNanos() - process_start_time_nanos; + int64_t thread_duration_nanos = + GetThreadCpuTimeNanos() - thread_start_time_nanos; + EXPECT_GE(process_duration_nanos, 0); + EXPECT_GE(thread_duration_nanos, 0); +} + +TEST(CpuTimeTest, MAYBE_TEST(TwoThreads)) { int64_t process_start_time_nanos = GetProcessCpuTimeNanos(); int64_t thread_start_time_nanos = GetThreadCpuTimeNanos(); int64_t counter1; @@ -70,7 +92,7 @@ TEST(CpuTimeTest, TwoThreads) { * kNumNanosecsPerMillisec); } -TEST(CpuTimeTest, Sleeping) { +TEST(CpuTimeTest, MAYBE_TEST(Sleeping)) { int64_t process_start_time_nanos = GetProcessCpuTimeNanos(); webrtc::SleepMs(kProcessingTimeMillisecs); int64_t process_duration_nanos =