-
Notifications
You must be signed in to change notification settings - Fork 865
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] Refactor ThreadName implementation
* Add support for macOS and iOS * Add test_threadname
- Loading branch information
1 parent
1a85c02
commit 73cad8d
Showing
3 changed files
with
192 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include <algorithm> | ||
#include <string> | ||
|
||
#include "gtest/gtest.h" | ||
#include "threadname.h" | ||
|
||
TEST(ThreadName, GetSet) | ||
{ | ||
std::string name("getset"); | ||
char buf[ThreadName::BUFSIZE * 2]; | ||
|
||
memset(buf, 'a', sizeof(buf)); | ||
ASSERT_EQ(ThreadName::get(buf), true); | ||
// ensure doesn't write out-of-range | ||
size_t max = ThreadName::BUFSIZE - 1; | ||
ASSERT_LE(strlen(buf), max); | ||
|
||
if (ThreadName::DUMMY_IMPL) | ||
return; | ||
|
||
ASSERT_EQ(ThreadName::set(name), true); | ||
memset(buf, 'a', sizeof(buf)); | ||
ASSERT_EQ(ThreadName::get(buf), true); | ||
ASSERT_EQ(buf, name); | ||
} | ||
|
||
TEST(ThreadName, AutoReset) | ||
{ | ||
std::string old_name("old"); | ||
std::string new_name("new-name"); | ||
if (ThreadName::DUMMY_IMPL) | ||
{ | ||
// just make sure the API is correct | ||
ThreadName t("test"); | ||
return; | ||
} | ||
|
||
ASSERT_EQ(ThreadName::set(old_name), true); | ||
std::string name; | ||
ASSERT_EQ(ThreadName::get(name), true); | ||
ASSERT_EQ(name, old_name); | ||
|
||
{ | ||
ThreadName threadName(new_name); | ||
ASSERT_EQ(ThreadName::get(name), true); | ||
ASSERT_EQ(name, new_name); | ||
} | ||
|
||
ASSERT_EQ(ThreadName::get(name), true); | ||
ASSERT_EQ(name, old_name); | ||
|
||
{ | ||
new_name.resize(std::max<size_t>(512, ThreadName::BUFSIZE * 2), 'z'); | ||
ThreadName threadName(new_name); | ||
ASSERT_EQ(ThreadName::get(name), true); | ||
ASSERT_EQ(new_name.compare(0, name.size(), name), 0); | ||
} | ||
|
||
ASSERT_EQ(ThreadName::get(name), true); | ||
ASSERT_EQ(name, old_name); | ||
} |