PeriodicTaskTests.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
12 /*
13  * $Log$
14  *
15  */
16 
17 #ifndef PeriodicTask_cpp
18 #define PeriodicTask_cpp
19 
20 #include <cppunit/ui/text/TestRunner.h>
21 #include <cppunit/TextOutputter.h>
22 #include <cppunit/extensions/TestFactoryRegistry.h>
23 #include <cppunit/extensions/HelperMacros.h>
24 #include <cppunit/TestAssert.h>
25 
26 #include <coil/Time.h>
27 #include <coil/PeriodicTask.h>
28 
29 #include <sstream>
30 #include <math.h>
31 
32  class Logger
33  {
34  public:
35  void log(const std::string& msg)
36  {
37  m_log.push_back(msg);
38  }
39 
40  int countLog(const std::string& msg)
41  {
42  int count = 0;
43  for (int i = 0; i < (int) m_log.size(); ++i)
44  {
45  if (m_log[i] == msg) ++count;
46  }
47  return count;
48  }
49 
50  void clearLog()
51  {
52  m_log.clear();
53  }
54  private:
55  std::vector<std::string> m_log;
56  };
57 
58 class A
59 {
60 public:
61  int mysvc()
62  {
63  static int cnt;
64  std::cout << "mysvc(): " << cnt << std::endl;
65  ++cnt;
66  coil::sleep(coil::TimeValue(0, 10000));
67  return 0;
68  }
69 
70  int mysvc2()
71  {
72  if (m_logger != NULL)
73  {
74  m_logger->log("mysvc2");
75  }
76  return 0;
77  }
78 
79  int mysvc3()
80  {
81  if (m_logger != NULL)
82  {
83  m_logger->log("mysvc3");
84  }
85 // coil::sleep(coil::TimeValue(0, 30000));
86 //useconds_t delay=30000;
87  coil::usleep(30000);
88  return 0;
89  }
90 
91  static void setLogger(Logger* logger)
92  {
93  m_logger = logger;
94  }
95 
96 private:
97  static Logger* m_logger;
98 };
99  Logger* A::m_logger = NULL;
100 
105 namespace PeriodicTask
106 {
108  : public CppUnit::TestFixture
109  {
110  CPPUNIT_TEST_SUITE(PeriodicTaskTests);
111 // CPPUNIT_TEST(test_case0);
112  CPPUNIT_TEST(test_setTask);
113  CPPUNIT_TEST(test_setPeriodic);
114  CPPUNIT_TEST(test_signal);
115  CPPUNIT_TEST(test_executionMeasure);
116  CPPUNIT_TEST(test_periodicMeasure);
117  CPPUNIT_TEST_SUITE_END();
118 
119  private:
120 
121  public:
122 
127  {
128  }
129 
134  {
135  }
136 
140  virtual void setUp()
141  {
142  }
143 
147  virtual void tearDown()
148  {
149  }
150 
151  /* test case */
152  void test_case0()
153  {
154  A a;
156  p.setTask(&a, &A::mysvc);
157  p.setPeriod(0.05);
158  p.executionMeasure(true);
160  p.periodicMeasure(true);
162  p.activate();
163 
164  for (int i(0); i < 10; ++i)
165  {
166  coil::sleep(1);
167  std::cout << "i: " << i << std::endl;
170 
171  std::cout << "estat max: " << estat.max_interval << std::endl;
172  std::cout << "estat min: " << estat.min_interval << std::endl;
173  std::cout << "estat mean: " << estat.mean_interval << std::endl;
174  std::cout << "estat sdev: " << estat.std_deviation << std::endl;
175 
176  std::cout << "pstat max: " << pstat.max_interval << std::endl;
177  std::cout << "pstat min: " << pstat.min_interval << std::endl;
178  std::cout << "pstat mean: " << pstat.mean_interval << std::endl;
179  std::cout << "pstat sdev: " << pstat.std_deviation << std::endl;
180 
181  if (i == 1)
182  {
183  p.suspend();
184  std::cout << "suspended: " << i << std::endl;
185  }
186  else if (i == 5)
187  {
188  p.resume();
189  std::cout << "resumed: " << i << std::endl;
190  }
191  else if (i == 3)
192  {
193  std::cout << "signal: " << i << std::endl;
194  p.signal();
195  coil::usleep(200000);
196  p.signal();
197  coil::usleep(200000);
198  }
199 
200  }
201 
202  p.finalize();
203 
204 
205 
206  }
212  {
213 
214  A a;
216  CPPUNIT_ASSERT(p.setTask(&a, &A::mysvc2));
217 
218  Logger logger;
219  a.setLogger(&logger);
220 
221  CPPUNIT_ASSERT_EQUAL(0, logger.countLog("mysvc2"));
222  p.activate();
223  coil::usleep(5);
224  p.finalize();
225  coil::usleep(5);
226  CPPUNIT_ASSERT(1<logger.countLog("mysvc2"));
227 
228 
229  }
235  {
236  std::ostringstream ss;
237  A a;
239 
240  Logger logger;
241  a.setLogger(&logger);
242 
243  p.setTask(&a, &A::mysvc2);
244  p.setPeriod(0.05);
245 
246  CPPUNIT_ASSERT_EQUAL(0, logger.countLog("mysvc2"));
247  p.activate();
248  coil::usleep(100000);
249  p.suspend();
250  coil::usleep(50000);
251 
252 // std::cout<<logger.countLog("mysvc2")<<std::endl;
253  CPPUNIT_ASSERT(4>logger.countLog("mysvc2"));
254  CPPUNIT_ASSERT(0<logger.countLog("mysvc2"));
255 
256  logger.clearLog();
257 
258  p.setPeriod(0.01);
259  CPPUNIT_ASSERT_EQUAL(0, logger.countLog("mysvc2"));
260  p.resume();
261  coil::usleep(100000);
262  p.suspend();
263  coil::usleep(10000);
264 // std::cout<<logger.countLog("mysvc2")<<std::endl;
265  CPPUNIT_ASSERT(12>logger.countLog("mysvc2"));
266  CPPUNIT_ASSERT(8<logger.countLog("mysvc2"));
267 
268  logger.clearLog();
269 
270  coil::TimeValue tm(0,50000);
271  p.setPeriod(tm);
272  CPPUNIT_ASSERT_EQUAL(0, logger.countLog("mysvc2"));
273  p.resume();
274  coil::usleep(100000);
275  p.finalize();
276  coil::usleep(50000);
277 // std::cout<<logger.countLog("mysvc2")<<std::endl;
278  CPPUNIT_ASSERT(4>logger.countLog("mysvc2"));
279  CPPUNIT_ASSERT(0<logger.countLog("mysvc2"));
280 
281  }
286  void test_signal()
287  {
288  A a;
290 
291  Logger logger;
292  a.setLogger(&logger);
293 
294  p.setTask(&a, &A::mysvc2);
295  p.setPeriod(0.05);
296 
297  CPPUNIT_ASSERT_EQUAL(0, logger.countLog("mysvc2"));
298  p.activate();
299  coil::usleep(200000);
300  p.suspend();
301 
302  int count;
303  count = logger.countLog("mysvc2");
304  coil::usleep(200000);
305  CPPUNIT_ASSERT(count == logger.countLog("mysvc2"));
306 
307  p.signal();
308  coil::usleep(200000);
309  CPPUNIT_ASSERT(count+1 == logger.countLog("mysvc2"));
310  p.signal();
311  coil::usleep(200000);
312  CPPUNIT_ASSERT(count+2 == logger.countLog("mysvc2"));
313 
314  logger.clearLog();
315  p.resume();
316  coil::usleep(200000);
317  p.suspend();
318  coil::usleep(200000);
319  CPPUNIT_ASSERT(6>logger.countLog("mysvc2"));
320  CPPUNIT_ASSERT(2<logger.countLog("mysvc2"));
321 
322  p.finalize();
323  coil::usleep(200000);
324 
325  }
331  {
332 std::cout<<"IN test_executionMeasure()"<<std::endl;
333  const double wait(0.03); // [s]
334  A a;
336  p.setTask(&a, &A::mysvc3);
337 
338  Logger logger;
339  a.setLogger(&logger);
340 
341  p.setPeriod(0.05);
342  p.executionMeasure(true);
343 
344  /* 回数(executionMeasureConut)がデフォルト(10)の場合 */
345  p.activate();
346 
347  coil::usleep(600000);
348 
349  p.suspend();
350  coil::usleep(50000);
352  /*
353  std::cout << "estat max: " << estat.max_interval << std::endl;
354  std::cout << "estat min: " << estat.min_interval << std::endl;
355  std::cout << "estat mean: " << estat.mean_interval << std::endl;
356  std::cout << "estat sdev: " << estat.std_deviation << std::endl;
357  */
358  std::ostringstream ss;
359  ss << "wait: " << wait << std::endl;
360  ss << "estat max: " << estat.max_interval << std::endl;
361  ss << "estat min: " << estat.min_interval << std::endl;
362  ss << "estat mean: " << estat.mean_interval << std::endl;
363  ss << "estat sdev: " << estat.std_deviation << std::endl;
364 
365  CPPUNIT_ASSERT_MESSAGE(ss.str(),estat.max_interval < (wait + 0.030));
366  CPPUNIT_ASSERT_MESSAGE(ss.str(),estat.min_interval > (wait - 0.015));
367  CPPUNIT_ASSERT_MESSAGE(ss.str(),fabs(estat.mean_interval - wait) < 0.03);
368  CPPUNIT_ASSERT_MESSAGE(ss.str(),estat.std_deviation < (wait / 5.0));
369 
370 
371 
372  /* 回数(executionMeasureConut)5の場合 */
374  p.resume();
375  coil::usleep(300000);
376  p.suspend();
377  coil::usleep(50000);
378  estat = p.getExecStat();
379  /*
380  std::cout << "estat max: " << estat.max_interval << std::endl;
381  std::cout << "estat min: " << estat.min_interval << std::endl;
382  std::cout << "estat mean: " << estat.mean_interval << std::endl;
383  std::cout << "estat sdev: " << estat.std_deviation << std::endl;
384  */
385  ss.str("");;
386  ss << "wait: " << wait << std::endl;
387  ss << "estat max: " << estat.max_interval << std::endl;
388  ss << "estat min: " << estat.min_interval << std::endl;
389  ss << "estat mean: " << estat.mean_interval << std::endl;
390  ss << "estat sdev: " << estat.std_deviation << std::endl;
391 
392  CPPUNIT_ASSERT_MESSAGE(ss.str(),estat.max_interval < (wait + 0.030));
393  CPPUNIT_ASSERT_MESSAGE(ss.str(),estat.min_interval > (wait - 0.015));
394  CPPUNIT_ASSERT_MESSAGE(ss.str(),fabs(estat.mean_interval - wait) < 0.03);
395  CPPUNIT_ASSERT_MESSAGE(ss.str(),estat.std_deviation < (wait / 5.0));
396 
397  /* 実行回数が回数(executionMeasureConut)に満たない場合 */
398  p.executionMeasureCount(10);
399  p.resume();
400  coil::usleep(300000);
401  p.suspend();
402  coil::usleep(50000);
403  p.finalize();
405  /* 回数(periodicMeasureConut)に満たないため、前回と同じ値を返す。*/
406  CPPUNIT_ASSERT(estat.max_interval == estat2.max_interval);
407  CPPUNIT_ASSERT(estat.min_interval == estat2.min_interval);
408  CPPUNIT_ASSERT(estat.mean_interval == estat2.mean_interval);
409  CPPUNIT_ASSERT(estat.std_deviation == estat2.std_deviation);
410  }
416  {
417 std::cout<<"IN test_periodicMeasure()"<<std::endl;
418  const double wait(0.05); // [s]
419  A a;
421  p.setTask(&a, &A::mysvc3);
422 
423  Logger logger;
424  a.setLogger(&logger);
425 
426  p.setPeriod(0.05);
427  p.periodicMeasure(true);
428 
429  /* 回数(periodicMeasureConut)がデフォルト(10)の場合 */
430  p.activate();
431 
432  coil::usleep(600000);
433 
434  p.suspend();
435  coil::usleep(50000);
437  /*
438  std::cout << "pstat max: " << pstat.max_interval << std::endl;
439  std::cout << "pstat min: " << pstat.min_interval << std::endl;
440  std::cout << "pstat mean: " << pstat.mean_interval << std::endl;
441  std::cout << "pstat sdev: " << pstat.std_deviation << std::endl;
442  */
443  CPPUNIT_ASSERT(pstat.max_interval < (wait + 0.030));
444  CPPUNIT_ASSERT(pstat.min_interval > (wait - 0.010));
445  CPPUNIT_ASSERT(fabs(pstat.mean_interval - wait) < 0.03);
446  CPPUNIT_ASSERT(pstat.std_deviation < (wait / 5.0));
447 
448 
449 
450  /* 回数(periodicMeasureConut)5の場合 */
452  p.resume();
453  coil::usleep(300000);
454  p.suspend();
455  coil::usleep(50000);
456  pstat = p.getPeriodStat();
457  /*
458  std::cout << "pstat max: " << pstat.max_interval << std::endl;
459  std::cout << "pstat min: " << pstat.min_interval << std::endl;
460  std::cout << "pstat mean: " << pstat.mean_interval << std::endl;
461  std::cout << "pstat sdev: " << pstat.std_deviation << std::endl;
462  */
463  CPPUNIT_ASSERT(pstat.max_interval < (wait + 0.030));
464  CPPUNIT_ASSERT(pstat.min_interval > (wait - 0.010));
465  CPPUNIT_ASSERT(fabs(pstat.mean_interval - wait) < 0.03);
466  CPPUNIT_ASSERT(pstat.std_deviation < (wait / 5.0));
467 
468 
469  /* 実行回数が回数(periodicMeasureConut)に満たない場合 */
470  p.periodicMeasureCount(10);
471  p.resume();
472  coil::usleep(300000);
473  p.suspend();
474  coil::usleep(50000);
475  p.finalize();
477  /* 回数(periodicMeasureConut)に満たないため、前回と同じ値を返す。*/
478  CPPUNIT_ASSERT(pstat.max_interval == pstat2.max_interval);
479  CPPUNIT_ASSERT(pstat.min_interval == pstat2.min_interval);
480  CPPUNIT_ASSERT(pstat.mean_interval == pstat2.mean_interval);
481  CPPUNIT_ASSERT(pstat.std_deviation == pstat2.std_deviation);
482  }
483  };
484 }; // namespace PeriodicTask
485 
486 /*
487  * Register test suite
488  */
490 
491 #ifdef LOCAL_MAIN
492 int main(int argc, char* argv[])
493 {
494  CppUnit::TextUi::TestRunner runner;
495  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
496  CppUnit::Outputter* outputter =
497  new CppUnit::TextOutputter(&runner.result(), std::cout);
498  runner.setOutputter(outputter);
499  bool retcode = runner.run();
500  return !retcode;
501 }
502 #endif // MAIN
503 #endif // PeriodicTask_cpp
int main(int argc, char **argv)
Structure for time statistics.
Definition: TimeMeasure.h:63
virtual void periodicMeasure(bool value)
Validate a Task period time measurement.
unsigned int sleep(unsigned int seconds)
Stop a processing at specified second time.
Definition: ace/coil/Time.h:40
void clearLog()
virtual void executionMeasure(bool value)
Validate a Task execute time measurement.
void log(const std::string &msg)
virtual int resume(void)
Resuming the suspended task.
virtual void activate()
Starting the task.
virtual void periodicMeasureCount(int n)
Task period time measurement count.
int mysvc3()
void test_periodicMeasure()
periodicMeasureテスト 実行周期は50ms,svcの実行時間は30ms。
static void setLogger(Logger *logger)
TimeValue class.
Definition: TimeValue.h:40
virtual void signal()
Executing the suspended task one tick.
virtual TimeMeasure::Statistics getExecStat()
Get a result in task execute time measurement.
void test_executionMeasure()
executionMeasureテスト 実行周期は50ms,svcの実行時間は30ms。
int countLog(const std::string &msg)
static Logger * m_logger
void test_setTask()
setTaskテスト
virtual bool setTask(TaskFuncBase *func, bool delete_in_dtor=true)
Setting task execution function.
virtual void setPeriod(double period)
Setting task execution period.
virtual void executionMeasureCount(int n)
Task execute time measurement period.
virtual void tearDown()
Test finalization.
void test_signal()
signal,suspend,resume,finalizeテスト
::OutPortBase::Logger logger
virtual void finalize()
Finalizing the task.
CPPUNIT_TEST_SUITE_REGISTRATION(PeriodicTask::PeriodicTaskTests)
std::vector< std::string > m_log
virtual TimeMeasure::Statistics getPeriodStat()
Get a result in task period time measurement.
virtual int suspend(void)
Suspending the task.
PeriodicTask class.
Definition: PeriodicTask.h:61
virtual void setUp()
Test initialization.
int mysvc()
int usleep(useconds_t usec)
Stop a processing at specified micro second time.
Definition: ace/coil/Time.h:51
void test_setPeriodic()
setPeriodicテスト
int mysvc2()


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Feb 28 2022 23:00:44