Googletest export
Make multiple attempts to verify GetThreadCount() Testing GetThreadCount() is inheritently noisy, as other threads can be started or destroyed between two calls to GetThreadCount(). This is especially true under certain analyzer configurations, such as TSAN. PiperOrigin-RevId: 381951799
This commit is contained in:
parent
255323cf09
commit
22e6055c75
@ -289,7 +289,16 @@ void* ThreadFunc(void* data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(GetThreadCountTest, ReturnsCorrectValue) {
|
TEST(GetThreadCountTest, ReturnsCorrectValue) {
|
||||||
const size_t starting_count = GetThreadCount();
|
size_t starting_count;
|
||||||
|
size_t thread_count_after_create;
|
||||||
|
size_t thread_count_after_join;
|
||||||
|
|
||||||
|
// We can't guarantee that no other thread was created or destroyed between
|
||||||
|
// any two calls to GetThreadCount(). We make multiple attempts, hoping that
|
||||||
|
// background noise is not constant and we would see the "right" values at
|
||||||
|
// some point.
|
||||||
|
for (int attempt = 0; attempt < 20; ++attempt) {
|
||||||
|
starting_count = GetThreadCount();
|
||||||
pthread_t thread_id;
|
pthread_t thread_id;
|
||||||
|
|
||||||
internal::Mutex mutex;
|
internal::Mutex mutex;
|
||||||
@ -302,23 +311,39 @@ TEST(GetThreadCountTest, ReturnsCorrectValue) {
|
|||||||
const int status = pthread_create(&thread_id, &attr, &ThreadFunc, &mutex);
|
const int status = pthread_create(&thread_id, &attr, &ThreadFunc, &mutex);
|
||||||
ASSERT_EQ(0, pthread_attr_destroy(&attr));
|
ASSERT_EQ(0, pthread_attr_destroy(&attr));
|
||||||
ASSERT_EQ(0, status);
|
ASSERT_EQ(0, status);
|
||||||
EXPECT_EQ(starting_count + 1, GetThreadCount());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
thread_count_after_create = GetThreadCount();
|
||||||
|
|
||||||
void* dummy;
|
void* dummy;
|
||||||
ASSERT_EQ(0, pthread_join(thread_id, &dummy));
|
ASSERT_EQ(0, pthread_join(thread_id, &dummy));
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
|
||||||
// The OS may not immediately report the updated thread count after
|
// The OS may not immediately report the updated thread count after
|
||||||
// joining a thread, causing flakiness in this test. To counter that, we
|
// 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.
|
// wait for up to .5 seconds for the OS to report the correct value.
|
||||||
|
bool thread_count_matches = false;
|
||||||
for (int i = 0; i < 5; ++i) {
|
for (int i = 0; i < 5; ++i) {
|
||||||
if (GetThreadCount() == starting_count)
|
thread_count_after_join = GetThreadCount();
|
||||||
|
if (thread_count_after_join == starting_count) {
|
||||||
|
thread_count_matches = true;
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
SleepMilliseconds(100);
|
SleepMilliseconds(100);
|
||||||
}
|
}
|
||||||
|
|
||||||
EXPECT_EQ(starting_count, GetThreadCount());
|
// Retry if an arbitrary other thread was created or destroyed.
|
||||||
|
if (!thread_count_matches) continue;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
EXPECT_EQ(thread_count_after_create, starting_count + 1);
|
||||||
|
EXPECT_EQ(thread_count_after_join, starting_count);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
TEST(GetThreadCountTest, ReturnsZeroWhenUnableToCountThreads) {
|
TEST(GetThreadCountTest, ReturnsZeroWhenUnableToCountThreads) {
|
||||||
|
Loading…
Reference in New Issue
Block a user