JayBeams  0.1
Another project to have fun coding.
ut_thread_config.cpp
Go to the documentation of this file.
1 #include <jb/thread_config.hpp>
2 
3 #include <boost/test/unit_test.hpp>
4 
5 #include <sched.h>
6 
7 /**
8  * @test Verify that basic functionality works as expected.
9  */
10 BOOST_AUTO_TEST_CASE(thread_config_basic) {
11  jb::thread_config tested;
12 
13  BOOST_CHECK_EQUAL(tested.name(), "");
14  BOOST_CHECK_NO_THROW(tested.native_scheduling_policy());
15  BOOST_CHECK_NO_THROW(tested.native_priority());
16 
17  tested.scheduler("FIFO").priority("MAX");
18  BOOST_CHECK_EQUAL(tested.native_scheduling_policy(), SCHED_FIFO);
19  BOOST_CHECK_EQUAL(
20  tested.native_priority(), sched_get_priority_max(SCHED_FIFO));
21 }
22 
23 /**
24  * @test Verify that setting the scheduling policy works as expected
25  */
26 BOOST_AUTO_TEST_CASE(thread_config_scheduling_policy) {
27  jb::thread_config tested;
28  tested.scheduler("RR");
29  BOOST_CHECK_EQUAL(tested.native_scheduling_policy(), SCHED_RR);
30  tested.scheduler("BATCH");
31  BOOST_CHECK_EQUAL(tested.native_scheduling_policy(), SCHED_BATCH);
32  tested.scheduler("IDLE");
33  BOOST_CHECK_EQUAL(tested.native_scheduling_policy(), SCHED_IDLE);
34  tested.scheduler("__not_a_scheduler__");
35  BOOST_CHECK_THROW(tested.native_scheduling_policy(), std::exception);
36 }
37 
38 /**
39  * @test Verify that setting the scheduling priority works as expected
40  */
41 BOOST_AUTO_TEST_CASE(thread_config_scheduling_priority) {
42  jb::thread_config tested;
43 
44  tested.scheduler("FIFO").priority("MIN");
45  BOOST_CHECK_EQUAL(
46  tested.native_priority(), sched_get_priority_min(SCHED_FIFO));
47  tested.priority("MID");
48  BOOST_CHECK_LE(tested.native_priority(), sched_get_priority_max(SCHED_FIFO));
49  BOOST_CHECK_GE(tested.native_priority(), sched_get_priority_min(SCHED_FIFO));
50  tested.priority("75");
51  BOOST_CHECK_EQUAL(tested.native_priority(), 75);
52  tested.priority("__not_a_number__");
53  BOOST_CHECK_THROW(tested.native_priority(), std::exception);
54 }
55 
56 /**
57  * @test Verify that parsing YAML overrides works as expected.
58  */
59 BOOST_AUTO_TEST_CASE(thread_config_overrides) {
60  jb::thread_config tested;
61 
62  char const contents[] = R"""(# YAML overrides
63 name: foo
64 scheduler: FIFO
65 priority: MAX
66 affinity: 1-3,7
67 )""";
68 
69  std::istringstream is(contents);
70 
71  int argc = 0;
72  tested.load_overrides(argc, nullptr, is);
73  BOOST_CHECK_EQUAL(tested.native_scheduling_policy(), SCHED_FIFO);
74  BOOST_CHECK_EQUAL(
75  tested.native_priority(), sched_get_priority_max(SCHED_FIFO));
76 
77  jb::cpu_set cpus;
78  cpus.set(1, 3).set(7);
79  BOOST_CHECK_EQUAL(tested.affinity(), cpus);
80 }
int native_priority() const
void load_overrides(int &argc, char *argv[], std::string const &filename, char const *environment_variable_name)
Read the configuration file and load the overrides defined therein.
jb::config_attribute< thread_config, jb::cpu_set > affinity
Hold the configuration to initialize threads.
int native_scheduling_policy() const
jb::config_attribute< thread_config, std::string > scheduler
BOOST_AUTO_TEST_CASE(thread_config_basic)
cpu_set & set(int cpu)
Add cpu to the cpu set.
Definition: cpu_set.hpp:66
jb::config_attribute< thread_config, std::string > name
A wrapper for the Linux CPU_SET data structure.
Definition: cpu_set.hpp:37
jb::config_attribute< thread_config, std::string > priority