2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <folly/Baton.h>
20 #include <folly/ScopeGuard.h>
21 #include <folly/portability/GTest.h>
22 #include <folly/system/ThreadName.h>
25 using namespace folly;
29 const bool expectedSetOtherThreadNameResult = folly::canSetOtherThreadName();
30 const bool expectedSetSelfThreadNameResult = folly::canSetCurrentThreadName();
31 constexpr StringPiece kThreadName{"rockin-thread"};
35 TEST(ThreadName, getCurrentThreadName) {
37 EXPECT_EQ(expectedSetSelfThreadNameResult, setThreadName(kThreadName));
38 if (expectedSetSelfThreadNameResult) {
39 EXPECT_EQ(kThreadName.toString(), *getCurrentThreadName());
42 SCOPE_EXIT { th.join(); };
45 TEST(ThreadName, setThreadName_other_pthread) {
47 Baton<> let_thread_end;
50 handle = pthread_self();
52 let_thread_end.wait();
54 SCOPE_EXIT { th.join(); };
56 SCOPE_EXIT { let_thread_end.post(); };
58 expectedSetOtherThreadNameResult, setThreadName(handle, kThreadName));
61 TEST(ThreadName, setThreadName_other_id) {
62 Baton<> let_thread_end;
64 let_thread_end.wait();
66 SCOPE_EXIT { th.join(); };
67 SCOPE_EXIT { let_thread_end.post(); };
69 expectedSetOtherThreadNameResult,
70 setThreadName(th.get_id(), kThreadName));
71 if (expectedSetOtherThreadNameResult) {
72 EXPECT_EQ(*getThreadName(th.get_id()), kThreadName);