diff --git a/googletest/test/googletest-port-test.cc index 9f05a01..9ee4061 100644 --- a/googletest/test/googletest-port-test.cc +++ b/googletest/test/googletest-port-test.cc @@ -304,7 +304,12 @@ TEST(GetThreadCountTest, ReturnsCorrectValue) { // some point. for (int attempt = 0; attempt < 20; ++attempt) { starting_count = GetThreadCount(); + if (starting_count == 0) { + GTEST_SKIP() << "GetThreadCount() is unavailable on this system."; + } + pthread_t thread_id; + bool thread_count_matches = false; internal::Mutex mutex; { @@ -317,7 +322,22 @@ TEST(GetThreadCountTest, ReturnsCorrectValue) { ASSERT_EQ(0, pthread_attr_destroy(&attr)); ASSERT_EQ(0, status); - thread_count_after_create = GetThreadCount(); + // The OS may not immediately report the updated thread count after + // creating a thread, causing flakiness in this test. To counter that, + // wait for up to .5 seconds for the OS to report the correct value + // while keeping the worker thread blocked on the mutex. + for (int i = 0; i < 5; ++i) { + thread_count_after_create = GetThreadCount(); + if (thread_count_after_create == 0) { + GTEST_SKIP() << "GetThreadCount() is unavailable on this system."; + } + if (thread_count_after_create == starting_count + 1) { + thread_count_matches = true; + break; + } + + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + } } void* dummy; @@ -325,14 +345,17 @@ TEST(GetThreadCountTest, ReturnsCorrectValue) { // Join before we decide whether we need to retry the test. Retry if an // arbitrary other thread was created or destroyed in the meantime. - if (thread_count_after_create != starting_count + 1) continue; + if (!thread_count_matches) continue; // The OS may not immediately report the updated thread count after // joining a thread, causing flakiness in this test. To counter that, we // wait for up to .5 seconds for the OS to report the correct value. - bool thread_count_matches = false; + thread_count_matches = false; for (int i = 0; i < 5; ++i) { thread_count_after_join = GetThreadCount(); + if (thread_count_after_join == 0) { + GTEST_SKIP() << "GetThreadCount() is unavailable on this system."; + } if (thread_count_after_join == starting_count) { thread_count_matches = true; break;