proxygen
ThreadNameTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <thread>
18 
19 #include <folly/ScopeGuard.h>
23 
24 using namespace std;
25 using namespace folly;
26 
27 namespace {
28 
29 const bool expectedSetOtherThreadNameResult = folly::canSetOtherThreadName();
30 const bool expectedSetSelfThreadNameResult = folly::canSetCurrentThreadName();
31 constexpr StringPiece kThreadName{"rockin-thread"};
32 
33 } // namespace
34 
35 TEST(ThreadName, getCurrentThreadName) {
36  thread th([] {
37  EXPECT_EQ(expectedSetSelfThreadNameResult, setThreadName(kThreadName));
38  if (expectedSetSelfThreadNameResult) {
39  EXPECT_EQ(kThreadName.toString(), *getCurrentThreadName());
40  }
41  });
42  SCOPE_EXIT {
43  th.join();
44  };
45 }
46 
47 #if FOLLY_HAVE_PTHREAD
48 TEST(ThreadName, setThreadName_other_pthread) {
49  Baton<> handle_set;
50  Baton<> let_thread_end;
51  pthread_t handle;
52  thread th([&] {
53  handle = pthread_self();
54  handle_set.post();
55  let_thread_end.wait();
56  });
57  SCOPE_EXIT {
58  th.join();
59  };
60  handle_set.wait();
61  SCOPE_EXIT {
62  let_thread_end.post();
63  };
64  EXPECT_EQ(
65  expectedSetOtherThreadNameResult, setThreadName(handle, kThreadName));
66 }
67 #endif
68 
69 TEST(ThreadName, setThreadName_other_id) {
70  Baton<> let_thread_end;
71  thread th([&] { let_thread_end.wait(); });
72  SCOPE_EXIT {
73  th.join();
74  };
75  SCOPE_EXIT {
76  let_thread_end.post();
77  };
78  EXPECT_EQ(
79  expectedSetOtherThreadNameResult,
80  setThreadName(th.get_id(), kThreadName));
81  if (expectedSetOtherThreadNameResult) {
82  EXPECT_EQ(*getThreadName(th.get_id()), kThreadName);
83  }
84 }
Optional< std::string > getThreadName(std::thread::id id)
Definition: ThreadName.cpp:90
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
bool canSetOtherThreadName()
Definition: ThreadName.cpp:80
STL namespace.
#define SCOPE_EXIT
Definition: ScopeGuard.h:274
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
FOLLY_ALWAYS_INLINE void wait(const WaitOptions &opt=wait_options()) noexcept
Definition: Baton.h:170
bool canSetCurrentThreadName()
Definition: ThreadName.cpp:71
void post() noexcept
Definition: Baton.h:123
bool setThreadName(std::thread::id tid, StringPiece name)
Definition: ThreadName.cpp:109
TEST(ThreadName, getCurrentThreadName)
Optional< std::string > getCurrentThreadName()
Definition: ThreadName.cpp:105