scheduler.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
3  */
4 
5 #include <gtest/gtest.h>
6 #include <uavcan/node/timer.hpp>
8 #include "../clock.hpp"
9 #include "../transport/can/can.hpp"
10 #include "test_node.hpp"
11 
12 #if !defined(UAVCAN_CPP11) || !defined(UAVCAN_CPP_VERSION)
13 # error UAVCAN_CPP_VERSION
14 #endif
15 
17 {
18  std::vector<uavcan::TimerEvent> events_a;
19  std::vector<uavcan::TimerEvent> events_b;
20 
21  void callA(const uavcan::TimerEvent& ev) { events_a.push_back(ev); }
22  void callB(const uavcan::TimerEvent& ev) { events_b.push_back(ev); }
23 
25 
28 };
29 
30 /*
31  * This test can fail on a non real time system. That's kinda sad but okay.
32  */
33 TEST(Scheduler, Timers)
34 {
35  SystemClockDriver clock_driver;
36  CanDriverMock can_driver(2, clock_driver);
37  TestNode node(can_driver, clock_driver, 1);
38 
39  /*
40  * Registration
41  */
42  {
43  TimerCallCounter tcc;
46 
47  ASSERT_EQ(0, node.getScheduler().getDeadlineScheduler().getNumHandlers());
48 
49  const uavcan::MonotonicTime start_ts = clock_driver.getMonotonic();
50 
51  a.startOneShotWithDeadline(start_ts + durMono(100000));
52  b.startPeriodic(durMono(1000));
53 
54  ASSERT_EQ(2, node.getScheduler().getDeadlineScheduler().getNumHandlers());
55 
56  /*
57  * Spinning
58  */
59  ASSERT_EQ(0, node.spin(start_ts + durMono(1000000)));
60 
61  ASSERT_EQ(1, tcc.events_a.size());
62  ASSERT_TRUE(areTimestampsClose(tcc.events_a[0].scheduled_time, start_ts + durMono(100000)));
63  ASSERT_TRUE(areTimestampsClose(tcc.events_a[0].scheduled_time, tcc.events_a[0].real_time));
64 
65  ASSERT_LT(900, tcc.events_b.size());
66  ASSERT_GT(1100, tcc.events_b.size());
67  {
68  uavcan::MonotonicTime next_expected_deadline = start_ts + durMono(1000);
69  for (unsigned i = 0; i < tcc.events_b.size(); i++)
70  {
71  ASSERT_TRUE(areTimestampsClose(tcc.events_b[i].scheduled_time, next_expected_deadline));
72  ASSERT_TRUE(areTimestampsClose(tcc.events_b[i].scheduled_time, tcc.events_b[i].real_time));
73  next_expected_deadline += durMono(1000);
74  }
75  }
76 
77  /*
78  * Deinitialization
79  */
80  ASSERT_EQ(1, node.getScheduler().getDeadlineScheduler().getNumHandlers());
81 
82  ASSERT_FALSE(a.isRunning());
84 
85  ASSERT_TRUE(b.isRunning());
86  ASSERT_EQ(1000, b.getPeriod().toUSec());
87  }
88 
89  ASSERT_EQ(0, node.getScheduler().getDeadlineScheduler().getNumHandlers()); // Both timers were destroyed by now
90  ASSERT_EQ(0, node.spin(durMono(1000))); // Spin some more without timers
91 }
92 
93 #if UAVCAN_CPP_VERSION >= UAVCAN_CPP11
94 
95 TEST(Scheduler, TimerCpp11)
96 {
97  SystemClockDriver clock_driver;
98  CanDriverMock can_driver(2, clock_driver);
99  TestNode node(can_driver, clock_driver, 1);
100 
101  int count = 0;
102 
103  uavcan::Timer tm(node, [&count](const uavcan::TimerEvent&) { count++; });
104 
105  ASSERT_EQ(0, node.getScheduler().getDeadlineScheduler().getNumHandlers());
106  tm.startPeriodic(uavcan::MonotonicDuration::fromMSec(10));
107  ASSERT_EQ(1, node.getScheduler().getDeadlineScheduler().getNumHandlers());
108 
109  ASSERT_EQ(0, node.spin(uavcan::MonotonicDuration::fromMSec(100)));
110 
111  std::cout << count << std::endl;
112  ASSERT_LE(5, count);
113  ASSERT_GE(15, count);
114 }
115 
116 #endif
uavcan::Scheduler
class UAVCAN_EXPORT Scheduler
Definition: scheduler.hpp:15
uavcan::TimerBase::getPeriod
MonotonicDuration getPeriod() const
Definition: timer.hpp:77
TimerCallCounter::callA
void callA(const uavcan::TimerEvent &ev)
Definition: scheduler.cpp:21
TimerCallCounter::Binder
uavcan::MethodBinder< TimerCallCounter *, void(TimerCallCounter::*)(const uavcan::TimerEvent &)> Binder
Definition: scheduler.cpp:24
SystemClockDriver::getMonotonic
virtual uavcan::MonotonicTime getMonotonic() const
Definition: libuavcan/libuavcan/test/clock.hpp:67
uavcan::DurationBase< MonotonicDuration >::fromMSec
static MonotonicDuration fromMSec(int64_t ms)
Definition: time.hpp:41
TimerCallCounter::bindB
Binder bindB()
Definition: scheduler.cpp:27
timer.hpp
uavcan::TimerBase::startPeriodic
void startPeriodic(MonotonicDuration period)
Definition: uc_timer.cpp:42
TestNode
Definition: test_node.hpp:20
TimerCallCounter::callB
void callB(const uavcan::TimerEvent &ev)
Definition: scheduler.cpp:22
areTimestampsClose
static bool areTimestampsClose(const T &a, const T &b, int64_t precision_usec=100000)
Definition: libuavcan/libuavcan/test/clock.hpp:103
TEST
TEST(Scheduler, Timers)
Definition: scheduler.cpp:33
method_binder.hpp
test_node.hpp
uavcan::TimerBase::startOneShotWithDeadline
void startOneShotWithDeadline(MonotonicTime deadline)
Definition: uc_timer.cpp:28
uavcan::MethodBinder
Definition: method_binder.hpp:20
CanDriverMock
Definition: libuavcan/libuavcan/test/transport/can/can.hpp:190
TimerCallCounter::bindA
Binder bindA()
Definition: scheduler.cpp:26
durMono
uavcan::MonotonicDuration durMono(int64_t usec)
Definition: libuavcan/libuavcan/test/clock.hpp:100
uavcan::DurationBase< MonotonicDuration >::getInfinite
static MonotonicDuration getInfinite()
Definition: time.hpp:33
pyuavcan_v0.introspect.node
node
Definition: introspect.py:398
SystemClockDriver
Definition: libuavcan/libuavcan/test/clock.hpp:62
uavcan::MonotonicTime
Definition: time.hpp:184
TimerCallCounter::events_a
std::vector< uavcan::TimerEvent > events_a
Definition: scheduler.cpp:18
TimerCallCounter::events_b
std::vector< uavcan::TimerEvent > events_b
Definition: scheduler.cpp:19
uavcan::TimerEventForwarder
Definition: timer.hpp:96
uavcan::TimerBase::isRunning
bool isRunning() const
Definition: uc_scheduler.cpp:32
uavcan::TimerEvent
Definition: timer.hpp:32
TimerCallCounter
Definition: scheduler.cpp:16


uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:03