33 #include <boost/scoped_ptr.hpp> 36 using namespace boost;
40 #define BOOST_CHECK_EQUAL_MESSAGE(v1, v2, M) BOOST_CHECK_MESSAGE( v1==v2, M) 41 #define BOOST_REQUIRE_EQUAL_MESSAGE(v1, v2, M) BOOST_REQUIRE_MESSAGE( v1==v2, M) 44 :
public RunnableInterface
53 : sleep_time(
Seconds_to_nsecs(0.2)), fini(false), wakeup_time(3), additional_sleep_time(), cycle(0)
60 wakeup_time.assign(wakeup_time.size(), 0);
66 if (cycle < wakeup_time.size())
68 wakeup_time[cycle] = os::TimeService::Instance()->getNSecs();
70 if (cycle < additional_sleep_time.size() && additional_sleep_time[cycle] > 0) {
71 usleep(additional_sleep_time[cycle]/1000);
93 :
public RunnableInterface
104 : overfail(0), underfail(0), succ(0), stepped(false), cpu(0)
113 if (stepped ==
false ) {
114 ts = TimeService::Instance()->getTicks();
118 if ( s < this->getThread()->getPeriod() *0.9 ) {
122 else if ( s > this->getThread()->getPeriod() *1.1 ) {
130 ts = TimeService::Instance()->getTicks();
132 #if defined( OROCOS_TARGET_GNULINUX ) 133 cpu = sched_getcpu();
134 BOOST_REQUIRE_NE(ENOSYS, cpu);
138 if (overfail || underfail)
139 cerr <<
"overfail is:"<<overfail<<
", underfail is:"<<underfail<<
" success is: "<<succ<<
endl;
152 :
public RunnableInterface
175 while (breakl ==
false) {
195 :
public RunnableInterface
197 std::vector<std::string>
v;
207 v.resize( 1025, std::string(
"Goodbye Memory") );
224 :
public RunnableInterface
239 this->getActivity()->stop();
244 this->getActivity()->stop();
274 delete t_run_int_prio;
275 delete t_run_int_act;
276 delete t_run_int_fail;
278 delete t_run_allocate;
279 delete t_run_allocate_act;
281 delete t_self_remove;
288 periodic_act->run( t_run_int_fail );
290 BOOST_CHECK( !periodic_act->start() );
291 BOOST_CHECK( !periodic_act->stop() );
293 BOOST_CHECK( t_act->run( t_run_int_fail ) );
294 t_run_int_fail->reset(
false);
296 BOOST_CHECK( !t_act->start() );
297 BOOST_CHECK( !t_act->stop() );
304 boost::scoped_ptr<TestOverrun> run(
new TestOverrun() );
305 boost::scoped_ptr<Activity> t(
new Activity(25, 0.1, 0,
"ORThread") );
306 BOOST_REQUIRE_EQUAL(0.1,t->getPeriod() );
308 t->thread()->setMaxOverrun(1);
323 BOOST_REQUIRE_MESSAGE( !t->isRunning(),
"Failed to detect step overrun in Thread");
324 BOOST_CHECK_MESSAGE( run->fini,
"Failed to execute finalize in emergencyStop" );
332 boost::scoped_ptr<TestOverrun> run(
new TestOverrun() );
333 boost::scoped_ptr<Activity> t(
new Activity(25, 0.3, 0,
"ORThread") );
335 run->additional_sleep_time.resize(1);
340 BOOST_REQUIRE_MESSAGE( t->start(),
"start thread");
345 BOOST_REQUIRE_MESSAGE( t->stop(),
"stop thread");
353 boost::scoped_ptr<TestOverrun> run(
new TestOverrun() );
354 boost::scoped_ptr<Activity> t(
new Activity(25, 0.3, 0,
"ORThread") );
356 run->additional_sleep_time.resize(1);
362 BOOST_REQUIRE_MESSAGE( t->start(),
"start thread");
367 BOOST_REQUIRE_MESSAGE( t->stop(),
"stop thread");
375 boost::scoped_ptr<TestOverrun> run(
new TestOverrun() );
376 boost::scoped_ptr<Activity> t(
new Activity(25, 0.3, 0,
"ORThread") );
378 run->additional_sleep_time.resize(1);
384 BOOST_REQUIRE_MESSAGE( t->start(),
"start thread");
389 BOOST_REQUIRE_MESSAGE( t->stop(),
"stop thread");
398 boost::scoped_ptr<TestPeriodic> run(
new TestPeriodic() );
404 BOOST_CHECK_MESSAGE( r,
"Failed to start Thread");
407 BOOST_CHECK_MESSAGE( r,
"Failed to stop Thread" );
408 BOOST_CHECK_MESSAGE( run->stepped ==
true,
"Step not executed" );
413 #if defined( OROCOS_TARGET_GNULINUX ) 415 void testAffinity2(boost::scoped_ptr<TestPeriodic>& run,
416 boost::scoped_ptr<Activity>& t,
423 BOOST_CHECK(t->setCpuAffinity(1 << targetCPU));
424 BOOST_CHECK_EQUAL((1 << targetCPU), t->getCpuAffinity());
426 BOOST_CHECK_EQUAL(0, run->cpu);
428 BOOST_CHECK_MESSAGE( r,
"Failed to start Thread");
431 BOOST_CHECK_MESSAGE( r,
"Failed to stop Thread" );
432 BOOST_CHECK_MESSAGE( run->stepped ==
true,
"Step not executed" );
433 BOOST_CHECK_EQUAL(targetCPU, run->cpu);
434 BOOST_CHECK_LT(0, run->succ);
442 if(std::getenv(
"CI") != NULL) {
443 BOOST_TEST_MESSAGE(
"Skipping testAffinity because it can fail on integration servers.");
447 int numCPU = sysconf( _SC_NPROCESSORS_ONLN );
450 boost::scoped_ptr<TestPeriodic> run(
new TestPeriodic() );
454 for (
int i=0; i<numCPU; ++i)
458 BOOST_CHECK_EQUAL(mask, t->getCpuAffinity());
461 testAffinity2(run, t, 0);
462 testAffinity2(run, t, numCPU-1);
471 scoped_ptr<TestRunnableInterface> t_run_int_nonper
475 scoped_ptr<Activity> t_task_nonper
478 BOOST_CHECK( t_task_nonper->run( t_run_int_nonper.get() ) );
479 BOOST_CHECK( t_task_nonper->start() );
481 BOOST_CHECK( t_run_int_nonper->looped );
482 BOOST_CHECK( !t_run_int_nonper->stepped );
483 BOOST_CHECK( t_run_int_nonper->init );
484 BOOST_CHECK( t_task_nonper->stop() );
485 BOOST_CHECK( t_run_int_nonper->fini );
486 BOOST_CHECK( !t_task_nonper->isRunning() );
487 BOOST_CHECK( t_task_nonper->run( 0 ) );
488 BOOST_CHECK( t_task_nonper->start() );
489 BOOST_CHECK( t_task_nonper->stop() );
491 BOOST_CHECK( !t_task_nonper->isRunning() );
497 scoped_ptr<TestRunnableInterface> t_run_int_nonper
501 scoped_ptr<Activity> t_task_nonper
504 BOOST_CHECK( t_task_nonper->run( t_run_int_nonper.get() ) );
505 BOOST_CHECK( t_task_nonper->start() );
508 BOOST_CHECK( t_run_int_nonper->looped );
509 BOOST_CHECK( t_run_int_nonper->init );
510 BOOST_CHECK( t_task_nonper->stop() );
511 BOOST_CHECK( t_run_int_nonper->fini );
512 BOOST_CHECK( !t_task_nonper->isRunning() );
513 BOOST_CHECK( t_task_nonper->run( 0 ) );
514 BOOST_CHECK( t_task_nonper->start() );
515 BOOST_CHECK( t_task_nonper->stop() );
517 BOOST_CHECK( !t_task_nonper->isRunning() );
523 scoped_ptr<TestRunnableInterface> t_run_int_nonper
527 scoped_ptr<Activity> t_task_nonper
530 BOOST_CHECK( t_task_nonper->run( t_run_int_nonper.get() ) );
531 BOOST_CHECK( t_task_nonper->start() );
534 t_run_int_nonper->breakl =
false;
535 BOOST_CHECK( t_task_nonper->start() );
537 BOOST_CHECK( t_task_nonper->stop() == false );
539 t_run_int_nonper->breakl =
true;
540 BOOST_CHECK( t_task_nonper->stop() );
541 BOOST_CHECK( t_run_int_nonper->fini );
542 BOOST_CHECK( !t_task_nonper->isRunning() );
548 scoped_ptr<TestSelfRemove> t_run_int_nonper
550 scoped_ptr<Activity> t_task_nonper
552 BOOST_CHECK( t_task_nonper->run( t_run_int_nonper.get() ) );
553 BOOST_CHECK( t_task_nonper->start() );
554 BOOST_CHECK( periodic_act->run(t_self_remove) );
555 BOOST_CHECK( periodic_act->start() );
557 BOOST_CHECK( !periodic_act->isRunning() );
558 BOOST_CHECK( t_self_remove->fini );
559 BOOST_CHECK( !t_task_nonper->isRunning() );
560 BOOST_CHECK( t_run_int_nonper->fini );
565 scoped_ptr<TestSelfRemove> t_run_int_nonper
567 scoped_ptr<Activity> t_task_nonper
569 BOOST_CHECK( t_task_nonper->run( t_run_int_nonper.get() ) );
570 BOOST_CHECK( t_task_nonper->start() );
572 BOOST_CHECK( !t_task_nonper->isRunning() );
573 BOOST_CHECK( t_run_int_nonper->fini );
578 scoped_ptr<TestSelfRemove> t_run_int_per
580 scoped_ptr<Activity> t_task_per
582 BOOST_CHECK( t_task_per->run( t_run_int_per.get() ) );
583 BOOST_CHECK( t_task_per->start() );
586 BOOST_CHECK( !t_task_per->isRunning() );
587 BOOST_CHECK( t_run_int_per->fini );
600 testAddRunnableInterface();
602 testRunnableInterfaceInit();
604 testRunnableInterfaceExecution();
606 testRemoveRunnableInterface();
615 testRemoveAllocate();
620 #if defined( OROCOS_TARGET_GNULINUX ) && defined( ORO_HAVE_PTHREAD_SETNAME_NP ) 623 Activity activity(0, 0,
"hello-world");
626 pthread_getname_np(thread->
getTask()->
thread, buffer,
sizeof(buffer));
627 BOOST_CHECK_EQUAL(std::string(buffer), std::string(
"hello-world"));
631 Activity activity(0, 0,
"abcdefgXXXXX1234567");
634 pthread_getname_np(thread->
getTask()->
thread, buffer,
sizeof(buffer));
635 BOOST_CHECK_EQUAL(std::string(buffer), std::string(
"abcdefg~1234567"));
641 bool adding_prio = periodic_act->run( t_run_int_prio );
642 BOOST_CHECK( adding_prio );
644 bool adding_act = t_act->run( t_run_int_act );
645 BOOST_CHECK( adding_act );
650 BOOST_CHECK( t_run_int_prio->fini );
651 BOOST_CHECK( periodic_act->run( 0 ) );
653 BOOST_CHECK( t_run_int_act->fini );
654 BOOST_CHECK( t_act->run( 0 ) );
659 BOOST_CHECK( periodic_act->start());
660 BOOST_CHECK( periodic_act->isRunning() );
662 BOOST_CHECK( t_act->start());
663 BOOST_CHECK( t_act->isRunning() );
671 BOOST_CHECK( (rv =
usleep(100000)) == 0);
683 BOOST_CHECK( t_run_int_prio->init );
684 BOOST_CHECK( t_run_int_act->init );
689 BOOST_CHECK( t_run_int_prio->stepped );
690 BOOST_CHECK( t_run_int_act->stepped );
695 BOOST_CHECK( periodic_act->stop());
696 BOOST_CHECK( !periodic_act->isRunning() );
698 BOOST_CHECK( t_act->stop());
699 BOOST_CHECK( !t_act->isRunning() );
704 BOOST_CHECK( periodic_act->run( t_run_allocate ) );
706 BOOST_CHECK( t_act->run( t_run_allocate_act ) );
711 BOOST_CHECK( periodic_act->run( 0 ) );
713 BOOST_CHECK( t_act->run( 0 ) );
#define BOOST_FIXTURE_TEST_SUITE(suite_name, F)
static const nsecs WaitPeriodPolicyTolerance
LogLevel getLogLevel() const
virtual os::ThreadInterface * thread()
void testRemoveRunnableInterface()
#define BOOST_AUTO_TEST_SUITE_END()
TestRunnableInterface(bool res)
static int rtos_nanosleep(const TIME_SPEC *rqtp, TIME_SPEC *rmtp)
int usleep(unsigned int us)
virtual RTOS_TASK * getTask()=0
#define BOOST_CHECK_EQUAL_MESSAGE(v1, v2, M)
std::vector< std::string > v
const int HighestPriority
void testRunnableInterfaceExecution()
basic_ostreams & endl(basic_ostreams &s)
static TIME_SPEC ticks2timespec(TICK_TIME hrt)
std::vector< nsecs > wakeup_time
An Activity executes a RunnableInterface object in a (periodic) thread.
unsigned int sleep(unsigned int s)
nsecs Seconds_to_nsecs(const Seconds s)
TICK_TIME nano2ticks(NANO_TIME nano)
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
void testRemoveAllocate()
struct timespec TIME_SPEC
std::vector< nsecs > additional_sleep_time
void testRunnableInterfaceInit()
void testAddRunnableInterface()
void setLogLevel(LogLevel ll)
BOOST_AUTO_TEST_CASE(testFailInit)