Skip to content

Commit 5959160

Browse files
committed
added test_thread
1 parent 96278fb commit 5959160

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,14 @@ if(BUILD_TESTING)
562562
test/test_macros.cpp
563563
)
564564

565+
ament_add_gtest(test_thread
566+
test/test_thread.cpp
567+
src/thread.c # to test RCUTILS_LOCAL functions
568+
)
569+
if(TARGET test_thread)
570+
target_link_libraries(test_thread ${PROJECT_NAME})
571+
endif()
572+
565573
add_performance_test(benchmark_logging test/benchmark/benchmark_logging.cpp)
566574
if(TARGET benchmark_logging)
567575
target_link_libraries(benchmark_logging ${PROJECT_NAME})

test/test_thread.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2020 Open Source Robotics Foundation, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <gtest/gtest.h>
16+
17+
#include "rcutils/thread.h"
18+
#include "rcutils/error_handling.h"
19+
20+
#ifdef __linux__
21+
#include <pthread.h>
22+
#endif // __linux__
23+
24+
TEST(test_thread, config_rt_thread) {
25+
#ifdef __linux__
26+
const unsigned cpu_id = 0;
27+
const unsigned long cpu_bitmask = 1 << cpu_id;
28+
const int priority = THREAD_PRIORITY_MEDIUM;
29+
if (configure_native_realtime_thread(pthread_self(), priority, cpu_bitmask) != RCUTILS_RET_OK) {
30+
GTEST_SKIP() << "Unable to set realtime thread priority.";
31+
return;
32+
}
33+
34+
struct sched_param params_self;
35+
int policy_self;
36+
EXPECT_EQ(0, pthread_getschedparam(pthread_self(), &policy_self, &params_self));
37+
int prio_calculated;
38+
EXPECT_EQ(RCUTILS_RET_OK, calculate_os_fifo_thread_priority(priority, &prio_calculated));
39+
EXPECT_EQ(prio_calculated, params_self.sched_priority);
40+
EXPECT_EQ(SCHED_FIFO, policy_self);
41+
42+
cpu_set_t cpuset_self;
43+
EXPECT_EQ(0, pthread_getaffinity_np(pthread_self(), sizeof(cpuset_self), &cpuset_self));
44+
cpu_set_t cpuset;
45+
CPU_ZERO(&cpuset);
46+
CPU_SET(cpu_id, &cpuset);
47+
EXPECT_EQ(0, memcmp(&cpuset, &cpuset_self, sizeof(cpu_set_t)));
48+
#else
49+
GTEST_SKIP() << "Testcase not implemented for this platform."
50+
#endif // __linux__
51+
}
52+
53+
TEST(test_thread, calculate_rt_priorities) {
54+
#ifdef __linux__
55+
int prio = -1;
56+
const int max_prio = sched_get_priority_max(SCHED_FIFO);
57+
const int min_prio = sched_get_priority_min(SCHED_FIFO);
58+
EXPECT_EQ(RCUTILS_RET_OK, calculate_os_fifo_thread_priority(THREAD_PRIORITY_LOWEST, &prio));
59+
EXPECT_EQ(min_prio, prio);
60+
EXPECT_EQ(RCUTILS_RET_OK, calculate_os_fifo_thread_priority(THREAD_PRIORITY_MEDIUM, &prio));
61+
EXPECT_EQ((max_prio + min_prio) / 2 - 1, prio);
62+
EXPECT_EQ(RCUTILS_RET_OK, calculate_os_fifo_thread_priority(THREAD_PRIORITY_HIGHEST, &prio));
63+
EXPECT_EQ(max_prio, prio);
64+
#elif
65+
GTEST_SKIP() << "Testcase not implemented for this platform."
66+
#endif // __linux__
67+
}

0 commit comments

Comments
 (0)