1818
1919#include " rclcpp/rate.hpp"
2020
21+ <<<<<<< HEAD
2122/*
2223 Basic tests for the Rate and WallRate classes.
2324 */
2425TEST (TestRate, rate_basics) {
26+ =======
27+ #include " ../utils/rclcpp_gtest_macros.hpp"
28+
29+ class TestRate : public ::testing::Test
30+ {
31+ public:
32+ void SetUp ()
33+ {
34+ rclcpp::init (0 , nullptr );
35+ }
36+
37+ void TearDown ()
38+ {
39+ rclcpp::shutdown ();
40+ }
41+ };
42+
43+ /*
44+ Basic tests for the Rate and WallRate classes.
45+ */
46+ TEST_F (TestRate, rate_basics) {
47+ >>>>>>> fcbe64cf (address rate related flaky tests. (#2329 ))
2548 auto period = std::chrono::milliseconds (1000 );
2649 auto offset = std::chrono::milliseconds (500 );
2750 auto epsilon = std::chrono::milliseconds (100 );
@@ -61,7 +84,11 @@ TEST(TestRate, rate_basics) {
6184 ASSERT_TRUE (epsilon > delta);
6285}
6386
87+ <<<<<<< HEAD
6488TEST (TestRate, wall_rate_basics) {
89+ =======
90+ TEST_F (TestRate, wall_rate_basics) {
91+ >>>>>>> fcbe64cf (address rate related flaky tests. (#2329 ))
6592 auto period = std::chrono::milliseconds (100 );
6693 auto offset = std::chrono::milliseconds (50 );
6794 auto epsilon = std::chrono::milliseconds (1 );
@@ -99,9 +126,130 @@ TEST(TestRate, wall_rate_basics) {
99126 auto five = std::chrono::steady_clock::now ();
100127 delta = five - four;
101128 EXPECT_GT (epsilon, delta);
129+ <<<<<<< HEAD
130+ =======
102131}
103132
104- TEST (TestRate, from_double) {
133+ /*
134+ Basic test for the deprecated GenericRate class.
135+ */
136+ TEST_F (TestRate, generic_rate) {
137+ auto period = std::chrono::milliseconds (100 );
138+ auto offset = std::chrono::milliseconds (50 );
139+ auto epsilon = std::chrono::milliseconds (1 );
140+ double overrun_ratio = 1.5 ;
141+
142+ {
143+ auto start = std::chrono::system_clock::now ();
144+
145+ // suppress deprecated warnings
146+ #if !defined(_WIN32)
147+ # pragma GCC diagnostic push
148+ # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
149+ #else // !defined(_WIN32)
150+ # pragma warning(push)
151+ # pragma warning(disable: 4996)
152+ #endif
153+
154+ rclcpp::GenericRate<std::chrono::system_clock> gr (period);
155+ ASSERT_FALSE (gr.is_steady ());
156+
157+ // remove warning suppression
158+ #if !defined(_WIN32)
159+ # pragma GCC diagnostic pop
160+ #else // !defined(_WIN32)
161+ # pragma warning(pop)
162+ #endif
163+
164+ ASSERT_EQ (gr.get_type (), RCL_SYSTEM_TIME);
165+ EXPECT_EQ (period, gr.period ());
166+ ASSERT_TRUE (gr.sleep ());
167+ auto one = std::chrono::system_clock::now ();
168+ auto delta = one - start;
169+ EXPECT_LT (period, delta + epsilon);
170+ EXPECT_GT (period * overrun_ratio, delta);
171+
172+ rclcpp::sleep_for (offset);
173+ ASSERT_TRUE (gr.sleep ());
174+ auto two = std::chrono::system_clock::now ();
175+ delta = two - start;
176+ EXPECT_LT (2 * period, delta);
177+ EXPECT_GT (2 * period * overrun_ratio, delta);
178+
179+ rclcpp::sleep_for (offset);
180+ auto two_offset = std::chrono::system_clock::now ();
181+ gr.reset ();
182+ ASSERT_TRUE (gr.sleep ());
183+ auto three = std::chrono::system_clock::now ();
184+ delta = three - two_offset;
185+ EXPECT_LT (period, delta + epsilon);
186+ EXPECT_GT (period * overrun_ratio, delta);
187+
188+ rclcpp::sleep_for (offset + period);
189+ auto four = std::chrono::system_clock::now ();
190+ ASSERT_FALSE (gr.sleep ());
191+ auto five = std::chrono::system_clock::now ();
192+ delta = five - four;
193+ ASSERT_TRUE (epsilon > delta);
194+ }
195+
196+ {
197+ auto start = std::chrono::steady_clock::now ();
198+
199+ // suppress deprecated warnings
200+ #if !defined(_WIN32)
201+ # pragma GCC diagnostic push
202+ # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
203+ #else // !defined(_WIN32)
204+ # pragma warning(push)
205+ # pragma warning(disable: 4996)
206+ #endif
207+
208+ rclcpp::GenericRate<std::chrono::steady_clock> gr (period);
209+ ASSERT_TRUE (gr.is_steady ());
210+
211+ // remove warning suppression
212+ #if !defined(_WIN32)
213+ # pragma GCC diagnostic pop
214+ #else // !defined(_WIN32)
215+ # pragma warning(pop)
216+ #endif
217+
218+ ASSERT_EQ (gr.get_type (), RCL_STEADY_TIME);
219+ EXPECT_EQ (period, gr.period ());
220+ ASSERT_TRUE (gr.sleep ());
221+ auto one = std::chrono::steady_clock::now ();
222+ auto delta = one - start;
223+ EXPECT_LT (period, delta);
224+ EXPECT_GT (period * overrun_ratio, delta);
225+
226+ rclcpp::sleep_for (offset);
227+ ASSERT_TRUE (gr.sleep ());
228+ auto two = std::chrono::steady_clock::now ();
229+ delta = two - start;
230+ EXPECT_LT (2 * period, delta + epsilon);
231+ EXPECT_GT (2 * period * overrun_ratio, delta);
232+
233+ rclcpp::sleep_for (offset);
234+ auto two_offset = std::chrono::steady_clock::now ();
235+ gr.reset ();
236+ ASSERT_TRUE (gr.sleep ());
237+ auto three = std::chrono::steady_clock::now ();
238+ delta = three - two_offset;
239+ EXPECT_LT (period, delta);
240+ EXPECT_GT (period * overrun_ratio, delta);
241+
242+ rclcpp::sleep_for (offset + period);
243+ auto four = std::chrono::steady_clock::now ();
244+ ASSERT_FALSE (gr.sleep ());
245+ auto five = std::chrono::steady_clock::now ();
246+ delta = five - four;
247+ EXPECT_GT (epsilon, delta);
248+ }
249+ >>>>>>> fcbe64cf (address rate related flaky tests. (#2329 ))
250+ }
251+
252+ TEST_F (TestRate, from_double) {
105253 {
106254 rclcpp::WallRate rate (1.0 );
107255 EXPECT_EQ (std::chrono::seconds (1 ), rate.period ());
@@ -119,3 +267,88 @@ TEST(TestRate, from_double) {
119267 EXPECT_EQ (std::chrono::milliseconds (250 ), rate.period ());
120268 }
121269}
270+ <<<<<<< HEAD
271+ =======
272+
273+ TEST_F (TestRate, clock_types) {
274+ {
275+ rclcpp::Rate rate (1.0 , std::make_shared<rclcpp::Clock>(RCL_SYSTEM_TIME));
276+ // suppress deprecated warnings
277+ #if !defined(_WIN32)
278+ # pragma GCC diagnostic push
279+ # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
280+ #else // !defined(_WIN32)
281+ # pragma warning(push)
282+ # pragma warning(disable: 4996)
283+ #endif
284+ EXPECT_FALSE (rate.is_steady ());
285+ // remove warning suppression
286+ #if !defined(_WIN32)
287+ # pragma GCC diagnostic pop
288+ #else // !defined(_WIN32)
289+ # pragma warning(pop)
290+ #endif
291+ EXPECT_EQ (RCL_SYSTEM_TIME, rate.get_type ());
292+ }
293+ {
294+ rclcpp::Rate rate (1.0 , std::make_shared<rclcpp::Clock>(RCL_STEADY_TIME));
295+ // suppress deprecated warnings
296+ #if !defined(_WIN32)
297+ # pragma GCC diagnostic push
298+ # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
299+ #else // !defined(_WIN32)
300+ # pragma warning(push)
301+ # pragma warning(disable: 4996)
302+ #endif
303+ EXPECT_TRUE (rate.is_steady ());
304+ // remove warning suppression
305+ #if !defined(_WIN32)
306+ # pragma GCC diagnostic pop
307+ #else // !defined(_WIN32)
308+ # pragma warning(pop)
309+ #endif
310+ EXPECT_EQ (RCL_STEADY_TIME, rate.get_type ());
311+ }
312+ {
313+ rclcpp::Rate rate (1.0 , std::make_shared<rclcpp::Clock>(RCL_ROS_TIME));
314+ // suppress deprecated warnings
315+ #if !defined(_WIN32)
316+ # pragma GCC diagnostic push
317+ # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
318+ #else // !defined(_WIN32)
319+ # pragma warning(push)
320+ # pragma warning(disable: 4996)
321+ #endif
322+ EXPECT_FALSE (rate.is_steady ());
323+ // remove warning suppression
324+ #if !defined(_WIN32)
325+ # pragma GCC diagnostic pop
326+ #else // !defined(_WIN32)
327+ # pragma warning(pop)
328+ #endif
329+ EXPECT_EQ (RCL_ROS_TIME, rate.get_type ());
330+ }
331+ }
332+
333+ TEST_F (TestRate, incorrect_constuctor) {
334+ // Constructor with 0-frequency
335+ RCLCPP_EXPECT_THROW_EQ (
336+ rclcpp::Rate rate (0.0 ),
337+ std::invalid_argument (" rate must be greater than 0" ));
338+
339+ // Constructor with negative frequency
340+ RCLCPP_EXPECT_THROW_EQ (
341+ rclcpp::Rate rate (-1.0 ),
342+ std::invalid_argument (" rate must be greater than 0" ));
343+
344+ // Constructor with 0-duration
345+ RCLCPP_EXPECT_THROW_EQ (
346+ rclcpp::Rate rate (rclcpp::Duration (0 , 0 )),
347+ std::invalid_argument (" period must be greater than 0" ));
348+
349+ // Constructor with negative duration
350+ RCLCPP_EXPECT_THROW_EQ (
351+ rclcpp::Rate rate (rclcpp::Duration (-1 , 0 )),
352+ std::invalid_argument (" period must be greater than 0" ));
353+ }
354+ >>>>>>> fcbe64cf (address rate related flaky tests. (#2329 ))
0 commit comments