summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorkosak@google.com <kosak@google.com@861a406c-534a-0410-8894-cb66d6ee9925>2015-07-17 22:53:00 +0000
committerkosak@google.com <kosak@google.com@861a406c-534a-0410-8894-cb66d6ee9925>2015-07-17 22:53:00 +0000
commit9ee09fed2fcd57093fc83dc8e53301270e7227c9 (patch)
tree2ea4cec130f05cc618d89d056304a7d15419318c /test
parent9e1d563866472949308083821242032a0891b88e (diff)
downloadgoogletest-9ee09fed2fcd57093fc83dc8e53301270e7227c9.tar.gz
Implement GetThreadCount for Linux.
git-svn-id: http://googletest.googlecode.com/svn/trunk@726 861a406c-534a-0410-8894-cb66d6ee9925
Diffstat (limited to 'test')
-rw-r--r--test/gtest-port_test.cc49
1 files changed, 21 insertions, 28 deletions
diff --git a/test/gtest-port_test.cc b/test/gtest-port_test.cc
index c904f1e..7647859 100644
--- a/test/gtest-port_test.cc
+++ b/test/gtest-port_test.cc
@@ -304,58 +304,51 @@ TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownFileAndLine) {
EXPECT_EQ("unknown file", FormatCompilerIndependentFileLocation(NULL, -1));
}
-#if GTEST_OS_MAC || GTEST_OS_QNX
+#if GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_QNX
void* ThreadFunc(void* data) {
- pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data);
- pthread_mutex_lock(mutex);
- pthread_mutex_unlock(mutex);
+ internal::Mutex* mutex = static_cast<internal::Mutex*>(data);
+ mutex->Lock();
+ mutex->Unlock();
return NULL;
}
TEST(GetThreadCountTest, ReturnsCorrectValue) {
- EXPECT_EQ(1U, GetThreadCount());
- pthread_mutex_t mutex;
- pthread_attr_t attr;
+ const size_t starting_count = GetThreadCount();
pthread_t thread_id;
- // TODO(vladl@google.com): turn mutex into internal::Mutex for automatic
- // destruction.
- pthread_mutex_init(&mutex, NULL);
- pthread_mutex_lock(&mutex);
- ASSERT_EQ(0, pthread_attr_init(&attr));
- ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
-
- const int status = pthread_create(&thread_id, &attr, &ThreadFunc, &mutex);
- ASSERT_EQ(0, pthread_attr_destroy(&attr));
- ASSERT_EQ(0, status);
- EXPECT_EQ(2U, GetThreadCount());
- pthread_mutex_unlock(&mutex);
+ internal::Mutex mutex;
+ {
+ internal::MutexLock lock(&mutex);
+ pthread_attr_t attr;
+ ASSERT_EQ(0, pthread_attr_init(&attr));
+ ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
+
+ const int status = pthread_create(&thread_id, &attr, &ThreadFunc, &mutex);
+ ASSERT_EQ(0, pthread_attr_destroy(&attr));
+ ASSERT_EQ(0, status);
+ EXPECT_EQ(starting_count + 1, GetThreadCount());
+ }
void* dummy;
ASSERT_EQ(0, pthread_join(thread_id, &dummy));
-# if GTEST_OS_MAC
-
- // MacOS X 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
// wait for up to .5 seconds for the OS to report the correct value.
for (int i = 0; i < 5; ++i) {
- if (GetThreadCount() == 1)
+ if (GetThreadCount() == starting_count)
break;
SleepMilliseconds(100);
}
-# endif // GTEST_OS_MAC
-
- EXPECT_EQ(1U, GetThreadCount());
- pthread_mutex_destroy(&mutex);
+ EXPECT_EQ(starting_count, GetThreadCount());
}
#else
TEST(GetThreadCountTest, ReturnsZeroWhenUnableToCountThreads) {
EXPECT_EQ(0U, GetThreadCount());
}
-#endif // GTEST_OS_MAC || GTEST_OS_QNX
+#endif // GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_QNX
TEST(GtestCheckDeathTest, DiesWithCorrectOutputOnFailure) {
const bool a_false_condition = false;