JayBeams  0.1
Another project to have fun coding.
ut_launch_thread.cpp
Go to the documentation of this file.
1 #include <jb/launch_thread.hpp>
2 
3 #include <boost/test/unit_test.hpp>
4 #include <mutex>
5 #include <thread>
6 
7 /**
8  * Helper classes to test jb::launch_thread.
9  */
10 namespace {
11 
12 class fixture {
13 public:
14  fixture()
15  : mu()
16  , id()
17  , value()
18  , msg() {
19  }
20 
21  void run(int x, std::string const& y) {
22  std::unique_lock<std::mutex> lk(mu);
23  id = std::this_thread::get_id();
24  value = x;
25  msg = y;
26  std::this_thread::sleep_for(std::chrono::seconds(1));
27  }
28 
29  void run(int x) {
30  std::unique_lock<std::mutex> lk(mu);
31  id = std::this_thread::get_id();
32  value = x;
33  msg = "no msg";
34  std::this_thread::sleep_for(std::chrono::seconds(1));
35  }
36 
37  int run_simple(int x, std::string const& y) {
38  run(x, y);
39  return 0;
40  }
41 
42  std::mutex mu;
43  std::thread::id id;
44  int value;
45  std::string msg;
46 };
47 
48 fixture g;
49 
50 void test_with_g(int x, std::string const& y) {
51  g.run(x, y);
52 }
53 
54 } // anonymous namespace
55 
56 /**
57  * @test Verify that jb::launch_thread<> compiles and works.
58  */
59 BOOST_AUTO_TEST_CASE(launch_thread_basic) {
61  cfg.name("test-thread");
62 
63  std::thread t;
64  jb::launch_thread(t, cfg, test_with_g, 42, "42");
65 
66  std::thread t0;
67  auto f0 = std::make_shared<fixture>();
68  auto fu0 = [f0](int x, std::string const& y) { f0->run(x, y); };
69 
70  jb::launch_thread(t0, cfg, fu0, 47, std::string("47"));
71 
72  std::thread t1;
73  fixture f1;
74  jb::launch_thread(t1, cfg, &fixture::run_simple, &f1, 1, "t1");
75 
76  t1.join();
77  BOOST_CHECK_NE(f1.id, std::thread::id());
78  BOOST_CHECK_EQUAL(f1.value, 1);
79  BOOST_CHECK_EQUAL(f1.msg, "t1");
80 
81  t0.join();
82  BOOST_CHECK_NE(f0->id, std::thread::id());
83  BOOST_CHECK_EQUAL(f0->value, 47);
84  BOOST_CHECK_EQUAL(f0->msg, "47");
85 
86  t.join();
87  BOOST_CHECK_NE(g.id, std::thread::id());
88  BOOST_CHECK_EQUAL(g.value, 42);
89  BOOST_CHECK_EQUAL(g.msg, "42");
90 }
91 
92 /**
93  * @test Verify that jb::launch_thread<> detects OS errors.
94  */
95 BOOST_AUTO_TEST_CASE(launch_thread_errors) {
97  cfg.ignore_setup_errors(false);
98  cfg.name("name_too_long_should_fail__1234567890ABCDEF__");
99 
100  BOOST_TEST_MESSAGE("main id=" << std::this_thread::get_id());
101  int cnt = 0;
102  std::thread t;
103  jb::launch_thread(t, cfg, [&cnt]() {
104  BOOST_TEST_MESSAGE("id=" << std::this_thread::get_id());
105  ++cnt;
106  });
107  t.join();
108  BOOST_CHECK_EQUAL(cnt, 0);
109 
110  cfg.name("").affinity(jb::cpu_set::parse("512"));
111  jb::launch_thread(t, cfg, [&cnt]() {
112  BOOST_TEST_MESSAGE("id=" << std::this_thread::get_id());
113  ++cnt;
114  });
115  t.join();
116  BOOST_CHECK_EQUAL(cnt, 0);
117 
118  cfg.affinity(jb::cpu_set::parse("")).priority("1000000");
119  jb::launch_thread(t, cfg, [&cnt]() {
120  BOOST_TEST_MESSAGE("id=" << std::this_thread::get_id());
121  ++cnt;
122  });
123  t.join();
124  BOOST_CHECK_EQUAL(cnt, 0);
125 }
126 
127 /**
128  * @test Improve coverage, handle unknown exceptions.
129  */
130 BOOST_AUTO_TEST_CASE(launch_thread_unknown_exception) {
131  jb::thread_config cfg;
132 
133  BOOST_TEST_MESSAGE("main id=" << std::this_thread::get_id());
134  int cnt = 0;
135  std::thread t;
136  jb::launch_thread(t, cfg, [&cnt]() {
137  BOOST_TEST_MESSAGE("id=" << std::this_thread::get_id());
138  ++cnt;
139  throw "not a std::exception";
140  });
141  t.join();
142 }
jb::config_attribute< thread_config, jb::cpu_set > affinity
jb::config_attribute< thread_config, bool > ignore_setup_errors
Hold the configuration to initialize threads.
static cpu_set parse(std::string const &value)
Interpret value as a cpu set in list format.
Definition: cpu_set.cpp:24
jb::config_attribute< thread_config, std::string > name
BOOST_AUTO_TEST_CASE(launch_thread_basic)
void launch_thread(std::thread &t, thread_config const &config, Function &&f, A &&... a)
Create a new thread, configure it as desired, and then call a user-defined function.