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 
26  Binder bindA() { return Binder(this, &TimerCallCounter::callA); }
27  Binder bindB() { return Binder(this, &TimerCallCounter::callB); }
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());
83  ASSERT_EQ(uavcan::MonotonicDuration::getInfinite(), a.getPeriod());
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());
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::MethodBinder< TimerCallCounter *, void(TimerCallCounter::*)(const uavcan::TimerEvent &)> Binder
Definition: scheduler.cpp:24
Binder bindB()
Definition: scheduler.cpp:27
virtual uavcan::MonotonicTime getMonotonic() const
void startPeriodic(MonotonicDuration period)
Definition: uc_timer.cpp:42
void callA(const uavcan::TimerEvent &ev)
Definition: scheduler.cpp:21
unsigned getNumHandlers() const
Definition: scheduler.hpp:54
static MonotonicDuration getInfinite()
Definition: time.hpp:33
class UAVCAN_EXPORT Scheduler
Definition: scheduler.hpp:15
void callB(const uavcan::TimerEvent &ev)
Definition: scheduler.cpp:22
int spin(MonotonicTime deadline)
static bool areTimestampsClose(const T &a, const T &b, int64_t precision_usec=100000)
TEST(Scheduler, Timers)
Definition: scheduler.cpp:33
virtual uavcan::Scheduler & getScheduler()
Definition: test_node.hpp:47
std::vector< uavcan::TimerEvent > events_a
Definition: scheduler.cpp:18
uavcan::MonotonicDuration durMono(int64_t usec)
static MonotonicDuration fromMSec(int64_t ms)
Definition: time.hpp:41
std::vector< uavcan::TimerEvent > events_b
Definition: scheduler.cpp:19
Binder bindA()
Definition: scheduler.cpp:26
DeadlineScheduler & getDeadlineScheduler()
Definition: scheduler.hpp:117


uavcan_communicator
Author(s):
autogenerated on Wed Jan 11 2023 03:59:39