tests/real-time-logger.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018,
3  * Joseph Mirabel
4  *
5  * LAAS-CNRS
6  *
7  */
8 
9 #include <iostream>
10 
11 #define ENABLE_RT_LOG
13 
14 #define BOOST_TEST_MODULE real_time_logger
15 
16 #if BOOST_VERSION >= 105900
17 #include <boost/test/tools/output_test_stream.hpp>
18 #else
19 #include <boost/test/output_test_stream.hpp>
20 #endif
21 #include <boost/date_time/posix_time/posix_time.hpp>
22 #include <boost/test/unit_test.hpp>
23 #include <boost/thread/thread.hpp>
24 
25 using namespace dynamicgraph;
26 
27 BOOST_AUTO_TEST_CASE(monothread) {
28  RealTimeLogger rtl(10);
30  for (int i = 0; i < 9; ++i) rtl.front() << "Call number " << i << '\n';
31  BOOST_CHECK(rtl.full());
32  rtl.front() << "This call should not appear in the output" << '\n';
33 
34  rtl.spinOnce();
35  BOOST_CHECK(!rtl.full());
36  rtl.front() << "This call should appear in the output" << '\n';
37 
38  int spinNb = 0;
39  while (rtl.spinOnce()) {
40  spinNb++;
41  }
42  BOOST_CHECK_EQUAL(spinNb, 9);
43 
44  rtl.front() << "This msg should be short." << '\n';
45  rtl.spinOnce();
46 }
47 
48 BOOST_AUTO_TEST_CASE(multithread) {
49  // The part of the code changing priority will only be effective
50  // if this test is run as root. Otherwise it behaves like a classical thread.
51  // Test confirms that in this case, it runs with a priority -51
52  // and that the thread for logging is running on SCHED_OTHER
53  // with a nice priority (20).
54  int threadPolicy;
55  struct sched_param threadParam;
56  if (pthread_getschedparam(pthread_self(), &threadPolicy, &threadParam) == 0) {
57  threadPolicy = SCHED_RR;
58  threadParam.sched_priority = 50;
59  pthread_setschedparam(pthread_self(), threadPolicy, &threadParam);
60  }
61 
63  dgADD_OSTREAM_TO_RTLOG(std::cout);
64 
65  for (std::size_t i = 0; i < rtl.getBufferSize() - 1; ++i)
66  dgRTLOG() << "Call number " << i << '\n';
67  for (std::size_t i = 0; i < 12; ++i) {
68  boost::this_thread::sleep(boost::posix_time::milliseconds(20));
69  dgRTLOG() << "Call number " << i << std::endl;
70  BOOST_CHECK(!rtl.full());
71  }
72 
73  dgRTLOG() << "This call should appear in the output" << '\n';
74 
76 }
#define dgADD_OSTREAM_TO_RTLOG(ostr)
BOOST_AUTO_TEST_CASE(monothread)
boost::shared_ptr< LoggerStream > LoggerStreamPtr_t
Main class of the real-time logger.
void addOutputStream(const LoggerStreamPtr_t &os)
#define dgRTLOG()


dynamic-graph
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Sun Jun 25 2023 02:06:03