rippled
Loading...
Searching...
No Matches
beast_CurrentThreadName_test.cpp
1#include <xrpl/beast/core/CurrentThreadName.h>
2#include <xrpl/beast/unit_test.h>
3
4#include <boost/predef/os.h>
5
6#include <thread>
7
8namespace xrpl {
9namespace test {
10
12{
13private:
14 static void
16 {
17 // Verify that upon creation a thread has no name.
18 auto const initialThreadName = beast::getCurrentThreadName();
19
20 // Set the new name.
22
23 // Indicate to caller that the name is set.
24 *state = 1;
25
26 // If there is an initial thread name then we failed.
27 if (!initialThreadName.empty())
28 return;
29
30 // Wait until all threads have their names.
31 while (!*stop)
32 ;
33
34 // Make sure the thread name that we set before is still there
35 // (not overwritten by, for instance, another thread).
36 if (beast::getCurrentThreadName() == myName)
37 *state = 2;
38 }
39#if BOOST_OS_LINUX
40 // Helper function to test a specific name.
41 // It creates a thread, sets the name, and checks if the OS-level
42 // name matches the expected (potentially truncated) name.
43 void
44 testName(std::string const& nameToSet, std::string const& expectedName)
45 {
46 std::thread t([&] {
48
49 // Initialize buffer to be safe.
50 char actualName[beast::maxThreadNameLength + 1] = {};
51 pthread_getname_np(pthread_self(), actualName, sizeof(actualName));
52
53 BEAST_EXPECT(std::string(actualName) == expectedName);
54 });
55 t.join();
56 }
57#endif
58
59public:
60 void
61 run() override
62 {
63 // Make two different threads with two different names.
64 // Make sure that the expected thread names are still there
65 // when the thread exits.
66 {
67 std::atomic<bool> stop{false};
68
69 std::atomic<int> stateA{0};
70 std::thread tA(exerciseName, "tA", &stop, &stateA);
71
72 std::atomic<int> stateB{0};
73 std::thread tB(exerciseName, "tB", &stop, &stateB);
74
75 // Wait until both threads have set their names.
76 while (stateA == 0 || stateB == 0)
77 ;
78
79 stop = true;
80 tA.join();
81 tB.join();
82
83 // Both threads should still have the expected name when they exit.
84 BEAST_EXPECT(stateA == 2);
85 BEAST_EXPECT(stateB == 2);
86 }
87#if BOOST_OS_LINUX
88 // On Linux, verify that thread names within the 15 character limit
89 // are set correctly (the 16th character is reserved for the null
90 // terminator).
91 {
92 testName("123456789012345",
93 "123456789012345"); // 15 chars, maximum allowed
94 testName("", ""); // empty name
95 testName("short", "short"); // short name
96 }
97#endif
98 }
99};
100
101BEAST_DEFINE_TESTSUITE(CurrentThreadName, beast, beast);
102
103} // namespace test
104} // namespace xrpl
A testsuite class.
Definition suite.h:51
static void exerciseName(std::string myName, std::atomic< bool > *stop, std::atomic< int > *state)
T join(T... args)
void setCurrentThreadName(std::string_view newThreadName)
Changes the name of the caller thread.
std::string getCurrentThreadName()
Returns the name of the caller thread.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5