GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: tests/real-time-logger.cpp Lines: 32 32 100.0 %
Date: 2023-03-15 12:04:10 Branches: 140 272 51.5 %

Line Branch Exec Source
1
/*
2
 * Copyright 2018,
3
 * Joseph Mirabel
4
 *
5
 * LAAS-CNRS
6
 *
7
 */
8
9
#include <iostream>
10
11
#define ENABLE_RT_LOG
12
#include <dynamic-graph/real-time-logger.h>
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
















4
BOOST_AUTO_TEST_CASE(monothread) {
28
4
  RealTimeLogger rtl(10);
29

2
  rtl.addOutputStream(LoggerStreamPtr_t(new LoggerIOStream(std::cout)));
30


20
  for (int i = 0; i < 9; ++i) rtl.front() << "Call number " << i << '\n';
31



2
  BOOST_CHECK(rtl.full());
32

2
  rtl.front() << "This call should not appear in the output" << '\n';
33
34
2
  rtl.spinOnce();
35



2
  BOOST_CHECK(!rtl.full());
36

2
  rtl.front() << "This call should appear in the output" << '\n';
37
38
2
  int spinNb = 0;
39

20
  while (rtl.spinOnce()) {
40
18
    spinNb++;
41
  }
42


2
  BOOST_CHECK_EQUAL(spinNb, 9);
43
44

2
  rtl.front() << "This msg should be short." << '\n';
45
2
  rtl.spinOnce();
46
2
}
47
48
















4
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
2
  if (pthread_getschedparam(pthread_self(), &threadPolicy, &threadParam) == 0) {
57
2
    threadPolicy = SCHED_RR;
58
2
    threadParam.sched_priority = 50;
59
2
    pthread_setschedparam(pthread_self(), threadPolicy, &threadParam);
60
  }
61
62
2
  RealTimeLogger &rtl = RealTimeLogger::instance();
63


2
  dgADD_OSTREAM_TO_RTLOG(std::cout);
64
65
2000
  for (std::size_t i = 0; i < rtl.getBufferSize() - 1; ++i)
66


1998
    dgRTLOG() << "Call number " << i << '\n';
67
26
  for (std::size_t i = 0; i < 12; ++i) {
68

24
    boost::this_thread::sleep(boost::posix_time::milliseconds(20));
69


24
    dgRTLOG() << "Call number " << i << std::endl;
70



24
    BOOST_CHECK(!rtl.full());
71
  }
72
73


2
  dgRTLOG() << "This call should appear in the output" << '\n';
74
75
2
  RealTimeLogger::destroy();
76
2
}