2019-01-20 12:21:20 +01:00
|
|
|
#include <cpp-utils/thread/debugging.h>
|
|
|
|
#include <cpp-utils/assert/assert.h>
|
|
|
|
#include <cpp-utils/lock/ConditionBarrier.h>
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
using namespace cpputils;
|
|
|
|
using std::string;
|
|
|
|
|
|
|
|
TEST(ThreadDebuggingTest_ThreadName, givenMainThread_whenSettingAndGetting_thenDoesntCrash) {
|
|
|
|
set_thread_name("my_thread_name");
|
|
|
|
get_thread_name();
|
|
|
|
}
|
|
|
|
|
2019-03-18 00:53:20 +01:00
|
|
|
TEST(ThreadDebuggingTest_ThreadName, givenMainThread_whenGettingFromInside_thenIsCorrect) {
|
|
|
|
set_thread_name("my_thread_name");
|
|
|
|
string name = get_thread_name();
|
|
|
|
EXPECT_EQ("my_thread_name", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ThreadDebuggingTest_ThreadName, givenChildThread_whenGettingFromInside_thenIsCorrect) {
|
|
|
|
std::thread child([] {
|
|
|
|
set_thread_name("my_thread_name");
|
|
|
|
string name = get_thread_name();
|
|
|
|
EXPECT_EQ("my_thread_name", name);
|
|
|
|
});
|
|
|
|
child.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(__GLIBC__) || defined(__APPLE__) || defined(_MSC_VER)
|
|
|
|
// disabled on musl because getting the thread name for a child thread doesn't work there
|
2019-01-20 12:21:20 +01:00
|
|
|
TEST(ThreadDebuggingTest_ThreadName, givenChildThread_whenSettingAndGetting_thenDoesntCrash) {
|
|
|
|
ConditionBarrier nameIsChecked;
|
|
|
|
|
|
|
|
bool child_didnt_crash = false;
|
|
|
|
std::thread child([&] {
|
|
|
|
set_thread_name("my_thread_name");
|
|
|
|
get_thread_name();
|
|
|
|
child_didnt_crash = true;
|
|
|
|
nameIsChecked.wait();
|
|
|
|
});
|
|
|
|
get_thread_name(&child);
|
|
|
|
nameIsChecked.release(); // getting the name of a not-running thread would cause errors, so let's make sure we only exit after getting the name
|
|
|
|
child.join();
|
|
|
|
EXPECT_TRUE(child_didnt_crash);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ThreadDebuggingTest_ThreadName, givenChildThread_whenGettingFromOutside_thenIsCorrect) {
|
2019-03-18 00:53:20 +01:00
|
|
|
ConditionBarrier nameIsSet;
|
|
|
|
ConditionBarrier nameIsChecked;
|
2019-01-20 12:21:20 +01:00
|
|
|
|
2019-03-18 00:53:20 +01:00
|
|
|
std::thread child([&] {
|
|
|
|
set_thread_name("my_thread_name");
|
|
|
|
nameIsSet.release();
|
|
|
|
nameIsChecked.wait();
|
|
|
|
});
|
2019-01-20 12:21:20 +01:00
|
|
|
|
2019-03-18 00:53:20 +01:00
|
|
|
nameIsSet.wait();
|
|
|
|
set_thread_name("outer_thread_name"); // just to make sure the next line doesn't read the outer thread name
|
|
|
|
string name = get_thread_name(&child);
|
|
|
|
EXPECT_EQ("my_thread_name", name);
|
2019-01-20 12:21:20 +01:00
|
|
|
|
2019-03-18 00:53:20 +01:00
|
|
|
nameIsChecked.release();
|
|
|
|
child.join();
|
2019-01-20 12:21:20 +01:00
|
|
|
}
|
2019-03-18 00:53:20 +01:00
|
|
|
#endif
|