timer_callbacks.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /* Author: Josh Faust */
31 
32 /*
33  * Test timer callbacks
34  */
35 
36 #include <string>
37 #include <sstream>
38 #include <fstream>
39 
40 #include <gtest/gtest.h>
41 
42 #include <time.h>
43 #include <stdlib.h>
44 
45 #include "ros/ros.h"
46 #include "ros/callback_queue.h"
47 #include <test_roscpp/TestArray.h>
48 #include <test_roscpp/TestStringString.h>
49 
50 #include <boost/scoped_ptr.hpp>
51 
52 using namespace ros;
53 using namespace test_roscpp;
54 
55 std::string g_node_name = "test_timer_callbacks";
56 
57 
58 /************************* SteadyTimer tests **********************/
59 
61 {
62  public:
63  SteadyTimerHelper(float period, bool oneshot = false)
64  : expected_period_(period)
65  , failed_(false)
66  , total_calls_(0)
67  , testing_period_(false)
68  , calls_before_testing_period_(0)
69  {
70  NodeHandle n;
71  timer_ = n.createSteadyTimer(expected_period_, &SteadyTimerHelper::callback, this, oneshot);
72  }
73 
74  void callback(const SteadyTimerEvent& e)
75  {
76  bool first = last_call_.isZero();
77  last_call_ = e.current_expired;
78 
79  if (!first)
80  {
81  double time_error = e.current_expired.toSec() - e.current_expected.toSec();
82  // Strict check if called early, loose check if called late.
83  // Yes, this is very loose, but must pass in high-load, containerized/virtualized, contentious environments.
84  if (time_error > 5.0 || time_error < -0.01)
85  {
86  ROS_ERROR("Call came at wrong time (expected: %f, expired: %f, callback: %f)",
88  failed_ = true;
89  }
90  }
91 
92  if(testing_period_)
93  {
94 
95  // Inside callback, less than current period, reset=false
96  if(total_calls_ == calls_before_testing_period_)
97  {
98  WallDuration p(0.5);
99  pretendWork(0.15);
100  setPeriod(p);
101  }
102 
103  // Inside callback, greater than current period, reset=false
104  else if(total_calls_ == (calls_before_testing_period_+1))
105  {
106  WallDuration p(0.25);
107  pretendWork(0.15);
108  setPeriod(p);
109  }
110 
111  // Inside callback, less than current period, reset=true
112  else if(total_calls_ == (calls_before_testing_period_+2))
113  {
114  WallDuration p(0.5);
115  pretendWork(0.15);
116  setPeriod(p, true);
117  }
118 
119  // Inside callback, greater than current period, reset=true
120  else if(total_calls_ == (calls_before_testing_period_+3))
121  {
122  WallDuration p(0.25);
123  pretendWork(0.15);
124  setPeriod(p, true);
125  }
126  }
127 
128  ++total_calls_;
129  }
130 
131  void setPeriod(const WallDuration p, bool reset=false)
132  {
133  timer_.setPeriod(p, reset);
134  expected_period_ = p;
135  }
136 
137 
138  void pretendWork(const float t)
139  {
140  ros::Rate r(1. / t);
141  r.sleep();
142  }
143 
146 
147  bool failed_;
148 
150  int32_t total_calls_;
151 
154 };
155 
156 TEST(RoscppTimerCallbacks, singleSteadyTimeCallback)
157 {
158  NodeHandle n;
159  SteadyTimerHelper helper1(0.01);
160 
161  WallDuration d(0.001f);
162  for (int32_t i = 0; i < 1000 && n.ok(); ++i)
163  {
164  spinOnce();
165  d.sleep();
166  }
167 
168  if (helper1.failed_)
169  {
170  FAIL();
171  }
172 
173  if (helper1.total_calls_ < 99)
174  {
175  ROS_ERROR("Total calls: %d (expected at least 100)", helper1.total_calls_);
176  FAIL();
177  }
178 }
179 
180 TEST(RoscppTimerCallbacks, multipleSteadyTimeCallbacks)
181 {
182  NodeHandle n;
183  const int count = 100;
184  typedef boost::scoped_ptr<SteadyTimerHelper> HelperPtr;
185  HelperPtr helpers[count];
186  for (int i = 0; i < count; ++i)
187  {
188  helpers[i].reset(new SteadyTimerHelper((float)(i + 1) * 0.1f));
189  }
190 
191  WallDuration d(0.01f);
192  const int spin_count = 1000;
193  for (int32_t i = 0; i < spin_count && n.ok(); ++i)
194  {
195  spinOnce();
196  d.sleep();
197  }
198 
199  for (int i = 0; i < count; ++i)
200  {
201  if (helpers[i]->failed_)
202  {
203  ROS_ERROR("Helper %d failed", i);
204  FAIL();
205  }
206 
207  int32_t expected_count = (spin_count * d.toSec()) / helpers[i]->expected_period_.toSec();
208  if (helpers[i]->total_calls_ < (expected_count - 1))
209  {
210  ROS_ERROR("Helper %d total calls: %d (at least %d expected)", i, helpers[i]->total_calls_, expected_count);
211  FAIL();
212  }
213  }
214 }
215 
216 TEST(RoscppTimerCallbacks, steadySetPeriod)
217 {
218  NodeHandle n;
219  WallDuration period(0.5);
220  SteadyTimerHelper helper(period.toSec());
221  Rate r(100);
222 
223  // Let the callback occur once before getting started
224  while(helper.total_calls_ < 1)
225  {
226  spinOnce();
227  r.sleep();
228  }
229 
230  helper.pretendWork(0.1);
231 
232  // outside callback, new period < old period, reset = false
233  Duration wait(0.5);
234  WallDuration p(0.25);
235  helper.setPeriod(p);
236  while(helper.total_calls_ < 2)
237  {
238  spinOnce();
239  r.sleep();
240  }
241 
242  helper.pretendWork(0.1);
243 
244  // outside callback, new period > old period, reset = false
245  WallDuration p2(0.5);
246  helper.setPeriod(p);
247  while(helper.total_calls_ < 3)
248  {
249  spinOnce();
250  r.sleep();
251  }
252 
253  helper.pretendWork(0.1);
254 
255  // outside callback, new period < old period, reset = true
256  WallDuration p3(0.25);
257  helper.setPeriod(p, true);
258  while(helper.total_calls_ < 4)
259  {
260  spinOnce();
261  r.sleep();
262  }
263 
264  helper.pretendWork(0.1);
265 
266  // outside callback, new period > old period, reset = true
267  WallDuration p4(0.5);
268  helper.setPeriod(p, true);
269  while(helper.total_calls_ < 5)
270  {
271  spinOnce();
272  r.sleep();
273  }
274 
275  // Test calling setPeriod inside callback
277  int total = helper.total_calls_ + 5;
278  helper.testing_period_ = true;
279  while(helper.total_calls_ < total)
280  {
281  spinOnce();
282  r.sleep();
283  }
284  helper.testing_period_ = false;
285 
286 
287  if(helper.failed_)
288  {
289  ROS_ERROR("Helper failed in setPeriod");
290  FAIL();
291  }
292 }
293 
294 TEST(RoscppTimerCallbacks, stopSteadyTimer)
295 {
296  NodeHandle n;
297  SteadyTimerHelper helper(0.001);
298 
299  for (int32_t i = 0; i < 1000 && n.ok(); ++i)
300  {
301  WallDuration(0.001).sleep();
302  spinOnce();
303  }
304 
305  ASSERT_GT(helper.total_calls_, 0);
306  int32_t last_count = helper.total_calls_;
307  helper.timer_.stop();
308 
309  for (int32_t i = 0; i < 1000 && n.ok(); ++i)
310  {
311  WallDuration(0.001).sleep();
312  spinOnce();
313  }
314 
315  ASSERT_EQ(last_count, helper.total_calls_);
316 }
317 
318 int32_t g_steady_count = 0;
320 {
321  ++g_steady_count;
322 }
323 
324 TEST(RoscppTimerCallbacks, steadyStopThenSpin)
325 {
326  g_steady_count = 0;
327  NodeHandle n;
329 
330  WallDuration(0.1).sleep();
331  timer.stop();
332 
333  spinOnce();
334 
335  ASSERT_EQ(g_steady_count, 0);
336 }
337 
338 TEST(RoscppTimerCallbacks, oneShotSteadyTimer)
339 {
340  NodeHandle n;
341  SteadyTimerHelper helper(0.001, true);
342 
343  for (int32_t i = 0; i < 1000 && n.ok(); ++i)
344  {
345  WallDuration(0.001).sleep();
346  spinOnce();
347  }
348 
349  ASSERT_EQ(helper.total_calls_, 1);
350 }
351 
352 /************************* WallTimer tests **********************/
353 
355 {
356 public:
357  WallTimerHelper(float period, bool oneshot = false)
358  : expected_period_(period)
359  , failed_(false)
360  , total_calls_(0)
361  , testing_period_(false)
362  , calls_before_testing_period_(0)
363  {
364  NodeHandle n;
365  timer_ = n.createWallTimer(expected_period_, &WallTimerHelper::callback, this, oneshot);
366  }
367 
368  void callback(const WallTimerEvent& e)
369  {
370  bool first = last_call_.isZero();
371  last_call_ = e.current_expired;
372 
373  if (!first)
374  {
375  double time_error = e.current_expired.toSec() - e.current_expected.toSec();
376  // Strict check if called early, loose check if called late.
377  // Yes, this is very loose, but must pass in high-load, containerized/virtualized, contentious environments.
378  if (time_error > 5.0 || time_error < -0.01)
379  {
380  ROS_ERROR("Call came at wrong time (expected: %f, expired: %f, callback: %f)",
382  failed_ = true;
383  }
384  }
385 
386  if(testing_period_)
387  {
388 
389  // Inside callback, less than current period, reset=false
390  if(total_calls_ == calls_before_testing_period_)
391  {
392  WallDuration p(0.5);
393  pretendWork(0.15);
394  setPeriod(p);
395  }
396 
397  // Inside callback, greater than current period, reset=false
398  else if(total_calls_ == (calls_before_testing_period_+1))
399  {
400  WallDuration p(0.25);
401  pretendWork(0.15);
402  setPeriod(p);
403  }
404 
405  // Inside callback, less than current period, reset=true
406  else if(total_calls_ == (calls_before_testing_period_+2))
407  {
408  WallDuration p(0.5);
409  pretendWork(0.15);
410  setPeriod(p, true);
411  }
412 
413  // Inside callback, greater than current period, reset=true
414  else if(total_calls_ == (calls_before_testing_period_+3))
415  {
416  WallDuration p(0.25);
417  pretendWork(0.15);
418  setPeriod(p, true);
419  }
420  }
421 
422  ++total_calls_;
423  }
424 
425  void setPeriod(const WallDuration p, bool reset=false)
426  {
427  timer_.setPeriod(p, reset);
428  expected_period_ = p;
429  }
430 
431 
432  void pretendWork(const float t)
433  {
434  ros::Rate r(1. / t);
435  r.sleep();
436  }
437 
440 
441  bool failed_;
442 
444  int32_t total_calls_;
445 
448 };
449 
450 TEST(RoscppTimerCallbacks, singleWallTimeCallback)
451 {
452  NodeHandle n;
453  WallTimerHelper helper1(0.01);
454 
455  WallDuration d(0.001f);
456  for (int32_t i = 0; i < 1000 && n.ok(); ++i)
457  {
458  spinOnce();
459  d.sleep();
460  }
461 
462  if (helper1.failed_)
463  {
464  FAIL();
465  }
466 
467  if (helper1.total_calls_ < 99)
468  {
469  ROS_ERROR("Total calls: %d (expected at least 100)", helper1.total_calls_);
470  FAIL();
471  }
472 }
473 
474 TEST(RoscppTimerCallbacks, multipleWallTimeCallbacks)
475 {
476  NodeHandle n;
477  const int count = 100;
478  typedef boost::scoped_ptr<WallTimerHelper> HelperPtr;
479  HelperPtr helpers[count];
480  for (int i = 0; i < count; ++i)
481  {
482  helpers[i].reset(new WallTimerHelper((float)(i + 1) * 0.1f));
483  }
484 
485  WallDuration d(0.01f);
486  const int spin_count = 1000;
487  for (int32_t i = 0; i < spin_count && n.ok(); ++i)
488  {
489  spinOnce();
490  d.sleep();
491  }
492 
493  for (int i = 0; i < count; ++i)
494  {
495  if (helpers[i]->failed_)
496  {
497  ROS_ERROR("Helper %d failed", i);
498  FAIL();
499  }
500 
501  int32_t expected_count = (spin_count * d.toSec()) / helpers[i]->expected_period_.toSec();
502  if (helpers[i]->total_calls_ < (expected_count - 1))
503  {
504  ROS_ERROR("Helper %d total calls: %d (at least %d expected)", i, helpers[i]->total_calls_, expected_count);
505  FAIL();
506  }
507  }
508 }
509 
510 TEST(RoscppTimerCallbacks, setPeriod)
511 {
512  NodeHandle n;
513  WallDuration period(0.5);
514  WallTimerHelper helper(period.toSec());
515  Rate r(100);
516 
517  // Let the callback occur once before getting started
518  while(helper.total_calls_ < 1)
519  {
520  spinOnce();
521  r.sleep();
522  }
523 
524  helper.pretendWork(0.1);
525 
526  // outside callback, new period < old period, reset = false
527  Time start = Time::now();
528  Duration wait(0.5);
529  WallDuration p(0.25);
530  helper.setPeriod(p);
531  while(helper.total_calls_ < 2)
532  {
533  spinOnce();
534  r.sleep();
535  }
536 
537  helper.pretendWork(0.1);
538 
539  // outside callback, new period > old period, reset = false
540  WallDuration p2(0.5);
541  start = Time::now();
542  helper.setPeriod(p);
543  while(helper.total_calls_ < 3)
544  {
545  spinOnce();
546  r.sleep();
547  }
548 
549  helper.pretendWork(0.1);
550 
551  // outside callback, new period < old period, reset = true
552  WallDuration p3(0.25);
553  start = Time::now();
554  helper.setPeriod(p, true);
555  while(helper.total_calls_ < 4)
556  {
557  spinOnce();
558  r.sleep();
559  }
560 
561  helper.pretendWork(0.1);
562 
563  // outside callback, new period > old period, reset = true
564  WallDuration p4(0.5);
565  start = Time::now();
566  helper.setPeriod(p, true);
567  while(helper.total_calls_ < 5)
568  {
569  spinOnce();
570  r.sleep();
571  }
572 
573  // Test calling setPeriod inside callback
575  int total = helper.total_calls_ + 5;
576  helper.testing_period_ = true;
577  while(helper.total_calls_ < total)
578  {
579  spinOnce();
580  r.sleep();
581  }
582  helper.testing_period_ = false;
583 
584 
585  if(helper.failed_)
586  {
587  ROS_ERROR("Helper failed in setPeriod");
588  FAIL();
589  }
590 }
591 
592 TEST(RoscppTimerCallbacks, stopWallTimer)
593 {
594  NodeHandle n;
595  WallTimerHelper helper(0.001);
596 
597  for (int32_t i = 0; i < 1000 && n.ok(); ++i)
598  {
599  WallDuration(0.001).sleep();
600  spinOnce();
601  }
602 
603  ASSERT_GT(helper.total_calls_, 0);
604  int32_t last_count = helper.total_calls_;
605  helper.timer_.stop();
606 
607  for (int32_t i = 0; i < 1000 && n.ok(); ++i)
608  {
609  WallDuration(0.001).sleep();
610  spinOnce();
611  }
612 
613  ASSERT_EQ(last_count, helper.total_calls_);
614 }
615 
616 int32_t g_count = 0;
618 {
619  ++g_count;
620 }
621 
622 TEST(RoscppTimerCallbacks, stopThenSpin)
623 {
624  g_count = 0;
625  NodeHandle n;
627 
628  WallDuration(0.1).sleep();
629  timer.stop();
630 
631  spinOnce();
632 
633  ASSERT_EQ(g_count, 0);
634 }
635 
636 TEST(RoscppTimerCallbacks, oneShotWallTimer)
637 {
638  NodeHandle n;
639  WallTimerHelper helper(0.001, true);
640 
641  for (int32_t i = 0; i < 1000 && n.ok(); ++i)
642  {
643  WallDuration(0.001).sleep();
644  spinOnce();
645  }
646 
647  ASSERT_EQ(helper.total_calls_, 1);
648 }
649 
651 {
652 public:
653  TimerHelper(Duration period, bool oneshot = false)
654  : failed_(false)
655  , expected_period_(period)
656  , total_calls_(0)
657  {
658  NodeHandle n;
659  timer_ = n.createTimer(expected_period_, &TimerHelper::callback, this, oneshot);
660  }
661 
662  TimerHelper(Rate r, bool oneshot = false)
663  : failed_(false)
664  , expected_period_(r.expectedCycleTime())
665  , total_calls_(0)
666  {
667  NodeHandle n;
668  timer_ = n.createTimer(r, &TimerHelper::callback, this, oneshot);
669  }
670 
671  void callback(const TimerEvent&)
672  {
673  ++total_calls_;
674  }
675 
676  bool failed_;
677 
679 
681  int32_t total_calls_;
682 };
683 
684 TEST(RoscppTimerCallbacks, singleROSTimeCallback)
685 {
686  NodeHandle n;
687 
688  Time now(1, 0);
689  Time::setNow(now);
690 
691  TimerHelper helper(Duration(0, 10000000));
692 
693  Duration d(0, 1000000);
694  for (int32_t i = 0; i < 1000 && n.ok(); ++i)
695  {
696  now += d;
697  Time::setNow(now);
698 
699  while (helper.timer_.hasPending())
700  {
701  WallDuration(0.001).sleep();
702  spinOnce();
703  }
704  }
705 
706  if (helper.failed_)
707  {
708  FAIL();
709  }
710 
711  if (helper.total_calls_ != 100)
712  {
713  ROS_ERROR("Total calls: %d (expected 100)", helper.total_calls_);
714  FAIL();
715  }
716 }
717 
718 TEST(RoscppTimerCallbacks, singleROSTimeCallbackFromRate)
719 {
720  NodeHandle n;
721 
722  Time now(1, 0);
723  Time::setNow(now);
724 
725  TimerHelper helper(Rate(100));
726 
727  Duration d(0, 1000000);
728  for (int32_t i = 0; i < 1000 && n.ok(); ++i)
729  {
730  now += d;
731  Time::setNow(now);
732 
733  while (helper.timer_.hasPending())
734  {
735  WallDuration(0.00025).sleep();
736  spinOnce();
737  }
738  }
739 
740  if (helper.failed_)
741  {
742  FAIL();
743  }
744 
745  if (helper.total_calls_ != 100)
746  {
747  ROS_ERROR("Total calls: %d (expected 100)", helper.total_calls_);
748  FAIL();
749  }
750 }
751 
752 TEST(RoscppTimerCallbacks, oneshotROSTimer)
753 {
754  NodeHandle n;
755 
756  Time now(1, 0);
757  Time::setNow(now);
758 
759  TimerHelper helper(Duration(0, 10000000), true);
760 
761  Duration d(0, 1000000);
762  for (int32_t i = 0; i < 1000 && n.ok(); ++i)
763  {
764  now += d;
765  Time::setNow(now);
766 
767  while (helper.timer_.hasPending())
768  {
769  WallDuration(0.001).sleep();
770  spinOnce();
771  }
772  }
773 
774  if (helper.failed_)
775  {
776  FAIL();
777  }
778 
779  ASSERT_EQ(helper.total_calls_, 1);
780 }
781 
782 TEST(RoscppTimerCallbacks, singleROSTimeCallbackLargeTimestep)
783 {
784  NodeHandle n;
785 
786  Time now(1, 0);
787  Time::setNow(now);
788 
789  TimerHelper helper(Duration(0, 10000000));
790 
791  Duration d(0, 100000000);
792  for (int32_t i = 0; i < 100 && n.ok(); ++i)
793  {
794  now += d;
795  Time::setNow(now);
796 
797  while (helper.timer_.hasPending())
798  {
799  WallDuration(0.001).sleep();
800  spinOnce();
801  }
802  }
803 
804  if (helper.failed_)
805  {
806  FAIL();
807  }
808 
809  if (helper.total_calls_ != 200)
810  {
811  ROS_ERROR("Total calls: %d (expected 200)", helper.total_calls_);
812  FAIL();
813  }
814 }
815 
816 TEST(RoscppTimerCallbacks, multipleROSTimeCallbacks)
817 {
818  NodeHandle n;
819 
820  Time now(1, 0);
821  Time::setNow(now);
822 
823  const int count = 100;
824  typedef boost::scoped_ptr<TimerHelper> HelperPtr;
825  HelperPtr helpers[count];
826  for (int i = 0; i < count; ++i)
827  {
828  helpers[i].reset(new TimerHelper(Duration((float)(i + 1) * 0.01f)));
829  }
830 
831  Duration d(0.001f);
832  const int spin_count = 1000;
833  for (int32_t i = 0; i < spin_count && n.ok(); ++i)
834  {
835  now += d;
836  Time::setNow(now);
837 
838  bool pending = false;
839 
840  do
841  {
842  pending = false;
843  for (int i = 0; i < count; ++i)
844  {
845  pending |= helpers[i]->timer_.hasPending();
846  }
847 
848  WallDuration(0.001).sleep();
849  spinOnce();
850  } while (pending);
851  }
852 
853  for (int i = 0; i < count; ++i)
854  {
855  if (helpers[i]->failed_)
856  {
857  ROS_ERROR("Helper %d failed", i);
858  FAIL();
859  }
860 
861  int32_t expected_count = (spin_count * d.toSec()) / helpers[i]->expected_period_.toSec();
862  if (helpers[i]->total_calls_ < (expected_count - 1) || helpers[i]->total_calls_ > (expected_count + 1))
863  {
864  ROS_ERROR("Helper %d total calls: %d (%d expected)", i, helpers[i]->total_calls_, expected_count);
865  FAIL();
866  }
867  }
868 }
869 
870 class Tracked
871 {
872 public:
874  {
875  g_count = 0;
876  }
877 
878  void callback(const TimerEvent&)
879  {
880  ++g_count;
881  }
882 };
883 
884 TEST(RoscppTimerCallbacks, trackedObject)
885 {
886  NodeHandle n;
887  Time now(1, 0);
888  Time::setNow(now);
889 
890  boost::shared_ptr<Tracked> tracked(boost::make_shared<Tracked>());
891  Timer timer = n.createTimer(Duration(0.001), &Tracked::callback, tracked);
892 
893  now += Duration(0.1);
894  Time::setNow(now);
895 
896  while (timer.hasPending())
897  {
898  WallDuration(0.001).sleep();
899  spinOnce();
900  }
901 
902  ASSERT_GT(g_count, 0);
903  int32_t last_count = g_count;
904  tracked.reset();
905 
906  now += Duration(0.1);
907  Time::setNow(now);
908 
909  while (timer.hasPending())
910  {
911  WallDuration(0.001).sleep();
912  spinOnce();
913  }
914 
915  ASSERT_EQ(last_count, g_count);
916 }
917 
918 TEST(RoscppTimerCallbacks, stopROSTimer)
919 {
920  NodeHandle n;
921  Time now(1, 0);
922  Time::setNow(now);
923 
924  TimerHelper helper(Duration(0.001));
925 
926  now += Duration(0.1);
927  Time::setNow(now);
928 
929  while (helper.timer_.hasPending())
930  {
931  WallDuration(0.001).sleep();
932  spinOnce();
933  }
934 
935  ASSERT_GT(helper.total_calls_, 0);
936  int32_t last_count = helper.total_calls_;
937  helper.timer_.stop();
938 
939  now += Duration(0.1);
940  Time::setNow(now);
941 
942  while (helper.timer_.hasPending())
943  {
944  WallDuration(0.001).sleep();
945  spinOnce();
946  }
947 
948  ASSERT_EQ(last_count, helper.total_calls_);
949 }
950 
951 int main(int argc, char** argv)
952 {
953  testing::InitGoogleTest(&argc, argv);
954  ros::init(argc, argv, g_node_name);
955 
956  return RUN_ALL_TESTS();
957 }
958 
ros::SteadyTimerEvent::current_expected
SteadyTime current_expected
t
ros::WallTime t
Definition: pointcloud_serdes.cpp:41
ros::WallDuration::sleep
bool sleep() const
WallTimerHelper::callback
void callback(const WallTimerEvent &e)
Definition: timer_callbacks.cpp:368
ros::SteadyTimerEvent::current_expired
SteadyTime current_expired
boost::shared_ptr
WallTimerHelper
Definition: timer_callbacks.cpp:354
TimerHelper::total_calls_
int32_t total_calls_
Definition: timer_callbacks.cpp:681
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
test_roscpp
Definition: generated_messages.cpp:58
WallTimerHelper::calls_before_testing_period_
int calls_before_testing_period_
Definition: timer_callbacks.cpp:447
SteadyTimerHelper::pretendWork
void pretendWork(const float t)
Definition: timer_callbacks.cpp:138
WallTimerHelper::last_call_
WallTime last_call_
Definition: timer_callbacks.cpp:438
main
int main(int argc, char **argv)
Definition: timer_callbacks.cpp:951
ros
ros.h
SteadyTimerHelper::setPeriod
void setPeriod(const WallDuration p, bool reset=false)
Definition: timer_callbacks.cpp:131
time.h
SteadyTimerHelper::timer_
SteadyTimer timer_
Definition: timer_callbacks.cpp:149
ros::Timer::stop
void stop()
ros::WallTimer
ros::spinOnce
ROSCPP_DECL void spinOnce()
TimerHelper::TimerHelper
TimerHelper(Rate r, bool oneshot=false)
Definition: timer_callbacks.cpp:662
TimerHelper
Definition: timer_callbacks.cpp:650
TimeBase< SteadyTime, WallDuration >::toSec
double toSec() const
WallTimerHelper::timer_
WallTimer timer_
Definition: timer_callbacks.cpp:443
ros::Time::setNow
static void setNow(const Time &new_now)
WallTimerHelper::setPeriod
void setPeriod(const WallDuration p, bool reset=false)
Definition: timer_callbacks.cpp:425
TimerHelper::callback
void callback(const TimerEvent &)
Definition: timer_callbacks.cpp:671
ros::WallTimerEvent::current_expected
WallTime current_expected
WallTimerHelper::expected_period_
WallDuration expected_period_
Definition: timer_callbacks.cpp:439
WallTimerHelper::failed_
bool failed_
Definition: timer_callbacks.cpp:441
f
f
ros::Timer
TEST
TEST(RoscppTimerCallbacks, singleSteadyTimeCallback)
Definition: timer_callbacks.cpp:156
ros::WallTimer
TimerHelper::failed_
bool failed_
Definition: timer_callbacks.cpp:676
SteadyTimerHelper::testing_period_
bool testing_period_
Definition: timer_callbacks.cpp:152
ros::SteadyTimer::stop
void stop()
steadyTimerCallback
void steadyTimerCallback(const ros::SteadyTimerEvent &)
Definition: timer_callbacks.cpp:319
ros::SteadyTimer
WallTimerHelper::total_calls_
int32_t total_calls_
Definition: timer_callbacks.cpp:444
TimerHelper::TimerHelper
TimerHelper(Duration period, bool oneshot=false)
Definition: timer_callbacks.cpp:653
Tracked::Tracked
Tracked()
Definition: timer_callbacks.cpp:873
SteadyTimerHelper::callback
void callback(const SteadyTimerEvent &e)
Definition: timer_callbacks.cpp:74
ros::SteadyTime
Tracked::callback
void callback(const TimerEvent &)
Definition: timer_callbacks.cpp:878
WallTimerHelper::WallTimerHelper
WallTimerHelper(float period, bool oneshot=false)
Definition: timer_callbacks.cpp:357
WallTimerHelper::pretendWork
void pretendWork(const float t)
Definition: timer_callbacks.cpp:432
d
d
TimerHelper::expected_period_
Duration expected_period_
Definition: timer_callbacks.cpp:678
ros::SteadyTimerEvent
g_count
int32_t g_count
Definition: timer_callbacks.cpp:616
ros::NodeHandle
Definition: test_callback_queue.cpp:397
ros::TimerEvent
SteadyTimerHelper::expected_period_
WallDuration expected_period_
Definition: timer_callbacks.cpp:145
g_steady_count
int32_t g_steady_count
Definition: timer_callbacks.cpp:318
ros::WallTime
SteadyTimerHelper::last_call_
SteadyTime last_call_
Definition: timer_callbacks.cpp:144
SteadyTimerHelper::calls_before_testing_period_
int calls_before_testing_period_
Definition: timer_callbacks.cpp:153
Tracked
Definition: timer_callbacks.cpp:870
ros::Rate::sleep
bool sleep()
ros::WallTimerEvent::current_real
WallTime current_real
callback_queue.h
ros::Timer::hasPending
bool hasPending()
ros::start
ROSCPP_DECL void start()
ros::NodeHandle::ok
bool ok() const
ros::WallTimerEvent
WallTimerHelper::testing_period_
bool testing_period_
Definition: timer_callbacks.cpp:446
ros::Time
ros::NodeHandle::createSteadyTimer
SteadyTimer createSteadyTimer(SteadyTimerOptions &ops) const
ROS_ERROR
#define ROS_ERROR(...)
ros::WallTimerEvent::current_expired
WallTime current_expired
SteadyTimerHelper
Definition: timer_callbacks.cpp:60
ros::Rate
g_node_name
std::string g_node_name
Definition: timer_callbacks.cpp:55
DurationBase< WallDuration >::toSec
double toSec() const
ros::WallDuration
SteadyTimerHelper::total_calls_
int32_t total_calls_
Definition: timer_callbacks.cpp:150
ros::NodeHandle::createTimer
Timer createTimer(Duration period, const TimerCallback &callback, bool oneshot=false, bool autostart=true) const
SteadyTimerHelper::failed_
bool failed_
Definition: timer_callbacks.cpp:147
ros::Duration
timerCallback
void timerCallback(const ros::WallTimerEvent &)
Definition: timer_callbacks.cpp:617
ros::NodeHandle::createWallTimer
WallTimer createWallTimer(WallDuration period, const WallTimerCallback &callback, bool oneshot=false, bool autostart=true) const
SteadyTimerHelper::SteadyTimerHelper
SteadyTimerHelper(float period, bool oneshot=false)
Definition: timer_callbacks.cpp:63
ros::SteadyTimer
ros::WallTimer::stop
void stop()
ros::SteadyTimerEvent::current_real
SteadyTime current_real
TimerHelper::timer_
Timer timer_
Definition: timer_callbacks.cpp:680
ros::Time::now
static Time now()


test_roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim, Dirk Thomas , Jacob Perron
autogenerated on Thu Nov 23 2023 04:02:02