Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "unit.hpp"
00020
00021 #include <iostream>
00022
00023 #include <TaskContext.hpp>
00024 #include <extras/SlaveActivity.hpp>
00025 #include <extras/SequentialActivity.hpp>
00026 #include <extras/SimulationActivity.hpp>
00027 #include <extras/SimulationThread.hpp>
00028
00029 #include <boost/function_types/function_type.hpp>
00030 #include <OperationCaller.hpp>
00031
00032 using namespace std;
00033 using namespace RTT;
00034 using namespace RTT::detail;
00035
00036 struct A {};
00037
00038
00039
00040 class StatesTC
00041 : public RTT::TaskContext
00042 {
00043 public:
00044 StatesTC()
00045 : TaskContext("TC", PreOperational)
00046 {
00047 BOOST_CHECK( this->getTaskState() == TaskContext::PreOperational );
00048 BOOST_CHECK( this->getTargetState() == TaskContext::PreOperational );
00049
00050 this->resetFlags();
00051 validconfig = true;
00052 validstart = true;
00053 do_error = false;
00054 do_fatal = false;
00055 do_throw=false;
00056 do_throw2=false;
00057 do_throw3=false;
00058 do_trigger=false;
00059 do_breakUH=false;
00060 do_block=false;
00061 do_checks=true;
00062 do_stop=false;
00063 }
00064
00065 void resetFlags()
00066 {
00067 didconfig = false;
00068 didstart = false;
00069 didstop = false;
00070 didcleanup = false;
00071 didupdate = false;
00072 diderror = false;
00073 didexcept = false;
00074 didbreakUH = false;
00075 updatecount=0;
00076 do_stop=false;
00077 }
00078
00079 bool configureHook() {
00080 if (do_checks) {
00081 BOOST_CHECK( mTaskState <= Stopped );
00082 BOOST_CHECK( getTargetState() == Stopped );
00083 }
00084 didconfig = true;
00085 return validconfig;
00086 }
00087
00088 bool startHook() {
00089 if (do_checks) {
00090 BOOST_CHECK( mTaskState == Stopped);
00091 BOOST_CHECK( getTargetState() == Running );
00092 }
00093 didstart = true;
00094 return validstart;
00095 }
00096
00097 void stopHook() {
00098 if (do_checks) {
00099 BOOST_CHECK( mTaskState >= Running || mTaskState == Exception);
00100 BOOST_CHECK( getTargetState() == Stopped || getTargetState() == Exception );
00101 }
00102 didstop = true;
00103 }
00104
00105 void cleanupHook() {
00106 if (do_checks) {
00107 BOOST_CHECK( mTaskState == Stopped || mTaskState == Exception);
00108 BOOST_CHECK( getTargetState() == PreOperational || getTargetState() == Exception );
00109 }
00110 didcleanup = true;
00111 }
00112
00113 void exceptionHook() {
00114 if (do_checks) {
00115 BOOST_CHECK( mTaskState == Exception);
00116 BOOST_CHECK( getTargetState() == Exception );
00117 }
00118 didexcept = true;
00119 if (do_throw3)
00120 throw A();
00121 }
00122
00123 void updateHook() {
00124 if (do_checks) {
00125 BOOST_CHECK( mTaskState == Running );
00126 BOOST_CHECK( getTargetState() == Running );
00127 }
00128 didupdate = true;
00129 updatecount++;
00130 if (do_fatal)
00131 this->fatal();
00132 if (do_error)
00133 this->error();
00134 if (do_throw)
00135 throw A();
00136 if (do_trigger) {
00137 this->trigger();
00138 do_trigger = false;
00139 }
00140 if (do_block)
00141 usleep(1*1000*1000);
00142 if (do_stop)
00143 this->stop();
00144 }
00145
00146 bool breakUpdateHook() {
00147 didbreakUH = true;
00148 return do_breakUH;
00149 }
00150
00151 void errorHook() {
00152 if (do_checks) {
00153 BOOST_CHECK( mTaskState == RunTimeError );
00154 BOOST_CHECK( getTargetState() == RunTimeError );
00155 }
00156 diderror = true;
00157 if (do_fatal)
00158 this->fatal();
00159 if (do_throw2)
00160 throw A();
00161 }
00162
00163 bool validconfig, didconfig;
00164 bool validstart, didstart;
00165 bool didstop;
00166 bool didcleanup;
00167 bool didupdate,diderror,didexcept, didbreakUH;
00168 bool do_fatal, do_error, do_throw,do_throw2,do_throw3, do_trigger, do_breakUH, do_block, do_checks, do_stop;
00169 int updatecount;
00170 };
00171
00175 class TaskStates_Test
00176 {
00177 public:
00178 TaskContext* tc;
00179 StatesTC* stc;
00180 ActivityInterface* tsim;
00181 ActivityInterface* stsim;
00182
00183 public:
00184 TaskStates_Test()
00185 {
00186 tc = new TaskContext( "root", TaskContext::Stopped );
00187 stc = new StatesTC();
00188 tc->setActivity( new SimulationActivity(0.001) );
00189 stc->setActivity( new SimulationActivity(0.001) );
00190 tsim = tc->getActivity();
00191 stsim = stc->getActivity();
00192 SimulationThread::Instance()->stop();
00193 }
00194
00195 ~TaskStates_Test()
00196 {
00197 tsim->stop();
00198 stsim->stop();
00199 delete tc;
00200 delete stc;
00201 }
00202 };
00203
00204
00205
00206 BOOST_FIXTURE_TEST_SUITE( TaskStatesSuite, TaskStates_Test )
00207
00208
00209
00212 BOOST_AUTO_TEST_CASE( testPeriod)
00213 {
00214
00215 TaskContext pertc("PerTC");
00216 BOOST_CHECK( pertc.getPeriod() == 0.0 );
00217 BOOST_CHECK( pertc.isActive() );
00218
00219
00220 pertc.setActivity( new SlaveActivity(1.0) );
00221 BOOST_CHECK( pertc.engine()->getActivity()->getPeriod() == 1.0 );
00222 BOOST_CHECK( pertc.getPeriod() == 1.0 );
00223
00224
00225 pertc.setActivity( new SlaveActivity(0.0) );
00226 BOOST_CHECK( pertc.engine()->getActivity()->getPeriod() == 0.0 );
00227 BOOST_CHECK( pertc.getPeriod() == 0.0 );
00228 }
00229
00233 BOOST_AUTO_TEST_CASE( testTrigger )
00234 {
00235
00236 StatesTC trigtc;
00237 trigtc.do_checks = false;
00238 BOOST_CHECK( trigtc.getPeriod() == 0.0 );
00239 BOOST_CHECK( trigtc.isActive() );
00240 BOOST_CHECK( trigtc.trigger() == true );
00241 usleep(100*1000);
00242
00243 BOOST_CHECK_EQUAL( trigtc.updatecount, 0 );
00244
00245 BOOST_CHECK( trigtc.configure() );
00246 BOOST_CHECK( trigtc.trigger() == true );
00247 usleep(100*1000);
00248
00249 BOOST_CHECK_EQUAL( trigtc.updatecount, 0 );
00250 BOOST_CHECK( trigtc.start() );
00251 BOOST_CHECK( trigtc.didstart == true );
00252 usleep(100*1000);
00253
00254
00255 BOOST_CHECK_EQUAL( trigtc.updatecount, 1 );
00256
00257 trigtc.resetFlags();
00258
00259
00260 BOOST_CHECK( trigtc.trigger() );
00261 usleep(100*1000);
00262 BOOST_CHECK( trigtc.didstart == false );
00263 BOOST_CHECK_EQUAL( trigtc.updatecount, 1 );
00264
00265
00266 BOOST_CHECK( trigtc.trigger() );
00267 usleep(100*1000);
00268 BOOST_CHECK_EQUAL( trigtc.updatecount, 2 );
00269
00270
00271
00272 trigtc.do_trigger = true;
00273 BOOST_CHECK( trigtc.trigger() );
00274 usleep(100*1000);
00275 BOOST_CHECK_EQUAL( trigtc.updatecount, 4 );
00276 BOOST_CHECK( trigtc.do_trigger == false );
00277
00278
00279 TaskContext pertc("test");
00280 pertc.setPeriod( 0.1 );
00281 BOOST_CHECK_EQUAL( pertc.getPeriod(), 0.1 );
00282 BOOST_CHECK( pertc.trigger() == false );
00283 BOOST_CHECK( pertc.configure() == true );
00284 BOOST_CHECK( pertc.trigger() == false );
00285 BOOST_CHECK( pertc.start() == true );
00286 BOOST_CHECK( pertc.trigger() == false );
00287 }
00288
00292 BOOST_AUTO_TEST_CASE( testBreakUpdateHook )
00293 {
00294
00295 StatesTC breaktc;
00296 breaktc.do_checks = false;
00297 BOOST_CHECK( breaktc.getPeriod() == 0.0 );
00298 BOOST_CHECK( breaktc.isActive() );
00299 breaktc.do_block = true;
00300 BOOST_CHECK( breaktc.configure() );
00301 BOOST_CHECK( breaktc.start() );
00302 usleep(100*1000);
00303
00304 BOOST_CHECK_EQUAL( breaktc.didupdate, true );
00305 BOOST_CHECK_EQUAL( breaktc.stop(), false );
00306 BOOST_CHECK_EQUAL( breaktc.didbreakUH, true);
00307 BOOST_CHECK_EQUAL( breaktc.didstop, false);
00308 breaktc.didbreakUH = false;
00309 breaktc.do_breakUH = true;
00310 breaktc.do_trigger = true;
00311
00312 BOOST_CHECK_EQUAL( breaktc.stop(), true );
00313 BOOST_CHECK_EQUAL( breaktc.didbreakUH, true);
00314 BOOST_CHECK_EQUAL( breaktc.didstop, true);
00315 }
00316
00320 BOOST_AUTO_TEST_CASE( testStopInUpdateHook )
00321 {
00322
00323 StatesTC breaktc;
00324 breaktc.do_checks = false;
00325 breaktc.do_stop = true;
00326 BOOST_CHECK( breaktc.getPeriod() == 0.0 );
00327 BOOST_CHECK( breaktc.isActive() );
00328 BOOST_CHECK( breaktc.configure() );
00329 BOOST_CHECK( breaktc.start() );
00330 usleep(100*1000);
00331
00332 breaktc.do_breakUH = false;
00333 BOOST_CHECK_EQUAL( breaktc.didupdate, true );
00334 BOOST_CHECK_EQUAL( breaktc.didbreakUH, true);
00335 BOOST_CHECK_EQUAL( breaktc.didstop, false);
00336 BOOST_CHECK_EQUAL( breaktc.isRunning(), true);
00337
00338
00339 breaktc.do_breakUH = true;
00340 breaktc.didbreakUH = false;
00341 breaktc.didstop = false;
00342 breaktc.didupdate = false;
00343 BOOST_CHECK( breaktc.trigger() );
00344 usleep(100*1000);
00345 BOOST_CHECK_EQUAL( breaktc.didupdate, true );
00346 BOOST_CHECK_EQUAL( breaktc.didbreakUH, true);
00347 BOOST_CHECK_EQUAL( breaktc.didstop, true);
00348 BOOST_CHECK_EQUAL( breaktc.isRunning(), false);
00349 }
00350
00354 BOOST_AUTO_TEST_CASE( testSetPeriod )
00355 {
00356 TaskContext pertc("test");
00357 BOOST_CHECK( pertc.setPeriod( 0.1 ) );
00358 BOOST_CHECK_EQUAL( pertc.getPeriod(), 0.1 );
00359 BOOST_CHECK( pertc.configure() == true);
00360 BOOST_CHECK( pertc.setPeriod( 0.2 ) );
00361 BOOST_CHECK_EQUAL( pertc.getPeriod(), 0.2 );
00362 BOOST_CHECK( pertc.start() == true );
00363
00364 BOOST_CHECK( pertc.setPeriod( 0.5 ) );
00365 BOOST_CHECK_EQUAL( pertc.getPeriod(), 0.5 );
00366 }
00367
00371 BOOST_AUTO_TEST_CASE( testTCStates)
00372 {
00373
00374 BOOST_CHECK( tc->getTaskState() == TaskContext::Stopped );
00375
00376
00377 BOOST_CHECK( tc->isConfigured() == true );
00378 BOOST_CHECK( tc->isRunning() == false );
00379 BOOST_CHECK( tc->isActive() == true );
00380 BOOST_CHECK( tc->configure() == true );
00381
00382
00383 BOOST_CHECK( tc->start() == true );
00384 BOOST_CHECK( tc->isRunning() == true );
00385 BOOST_CHECK( tc->isActive() == true );
00386 BOOST_CHECK( tc->configure() == false );
00387 BOOST_CHECK( tc->start() == false );
00388 BOOST_CHECK( tc->cleanup() == false );
00389
00390
00391 BOOST_CHECK( tc->stop() == true );
00392 BOOST_CHECK( tc->isRunning() == false);
00393 BOOST_CHECK( tc->isActive() == true );
00394 BOOST_CHECK( tc->stop() == false );
00395 BOOST_CHECK( tc->configure() == true );
00396 BOOST_CHECK( tc->isConfigured() == true );
00397
00398
00399 BOOST_CHECK( tc->engine()->getActivity()->stop() == true);
00400 BOOST_CHECK( tc->isActive() == false);
00401 BOOST_CHECK( tc->activate() == true );
00402 BOOST_CHECK( tc->isActive() == true);
00403 BOOST_CHECK( tc->isRunning() == false );
00404 BOOST_CHECK( tc->configure() == true );
00405 BOOST_CHECK( tc->isConfigured() == true );
00406
00407
00408 BOOST_CHECK( tc->cleanup() == true );
00409 BOOST_CHECK( tc->isConfigured() == false);
00410 BOOST_CHECK( tc->isRunning() == false);
00411
00412
00413 BOOST_CHECK( tc->configure() == true );
00414 BOOST_CHECK( tc->isConfigured() == true);
00415 BOOST_CHECK( tc->isRunning() == false);
00416 BOOST_CHECK( tc->isActive() == true );
00417
00418 }
00419
00423 BOOST_AUTO_TEST_CASE( testSpecialTCStates)
00424 {
00425
00426 BOOST_CHECK( stc->getTaskState() == TaskContext::PreOperational );
00427
00428
00429 BOOST_CHECK( stc->configure() == true );
00430 BOOST_CHECK( stc->didconfig == true );
00431 BOOST_CHECK( stc->isConfigured() == true );
00432
00433 BOOST_CHECK( stc->didstart == false );
00434 BOOST_CHECK( stc->didupdate == false );
00435 BOOST_CHECK( stc->diderror == false );
00436 BOOST_CHECK( stc->didexcept == false );
00437 BOOST_CHECK( stc->didstop == false );
00438 BOOST_CHECK( stc->didcleanup == false );
00439 stc->resetFlags();
00440
00441
00442 BOOST_CHECK( stc->start() == true );
00443 BOOST_CHECK( stc->didstart == true );
00444 BOOST_CHECK( stc->isRunning() == true );
00445
00446 BOOST_CHECK( stc->didconfig == false );
00447 BOOST_CHECK( stc->didupdate == false );
00448 BOOST_CHECK( stc->diderror == false );
00449 BOOST_CHECK( stc->didexcept == false );
00450 BOOST_CHECK( stc->didstop == false );
00451 BOOST_CHECK( stc->didcleanup == false );
00452 stc->resetFlags();
00453
00454
00455
00456 SimulationThread::Instance()->run(1);
00457
00458 BOOST_CHECK( stc->didconfig == false );
00459 BOOST_CHECK( stc->didstart == false );
00460 BOOST_CHECK( stc->didupdate == true );
00461 BOOST_CHECK( stc->diderror == false );
00462 BOOST_CHECK( stc->didexcept == false );
00463 BOOST_CHECK( stc->didstop == false );
00464 BOOST_CHECK( stc->didcleanup == false );
00465 stc->resetFlags();
00466
00467
00468 BOOST_CHECK( stc->stop() == true );
00469 BOOST_CHECK( stc->didstop == true );
00470 BOOST_CHECK( stc->isRunning() == false );
00471
00472 BOOST_CHECK( stc->didconfig == false );
00473 BOOST_CHECK( stc->didstart == false );
00474 BOOST_CHECK( stc->didupdate == false );
00475 BOOST_CHECK( stc->diderror == false );
00476 BOOST_CHECK( stc->didexcept == false );
00477 BOOST_CHECK( stc->didcleanup == false );
00478 stc->resetFlags();
00479
00480
00481 BOOST_CHECK( stc->configure() == true );
00482 BOOST_CHECK( stc->didconfig == true );
00483 BOOST_CHECK( stc->isConfigured() == true );
00484
00485 BOOST_CHECK( stc->didstart == false );
00486 BOOST_CHECK( stc->didupdate == false );
00487 BOOST_CHECK( stc->diderror == false );
00488 BOOST_CHECK( stc->didexcept == false );
00489 BOOST_CHECK( stc->didstop == false );
00490 BOOST_CHECK( stc->didcleanup == false );
00491 stc->resetFlags();
00492
00493
00494 BOOST_CHECK( stc->cleanup() == true );
00495 BOOST_CHECK( stc->didcleanup == true );
00496 BOOST_CHECK( stc->isConfigured() == false);
00497
00498 BOOST_CHECK( stc->didconfig == false );
00499 BOOST_CHECK( stc->didstart == false );
00500 BOOST_CHECK( stc->didupdate == false );
00501 BOOST_CHECK( stc->diderror == false );
00502 BOOST_CHECK( stc->didexcept == false );
00503 BOOST_CHECK( stc->didstop == false );
00504 stc->resetFlags();
00505 }
00506
00510 BOOST_AUTO_TEST_CASE( testFailingTCStates)
00511 {
00512
00513 stc->validconfig = false;
00514 stc->validstart = false;
00515
00516
00517 BOOST_CHECK( stc->isConfigured() == false );
00518 BOOST_CHECK( stc->isRunning() == false );
00519 BOOST_CHECK( stc->configure() == false );
00520 BOOST_CHECK( stc->didconfig == true );
00521 BOOST_CHECK( stc->isConfigured() == false );
00522 BOOST_CHECK( stc->isRunning() == false );
00523 BOOST_CHECK( stc->isActive() == true );
00524 stc->resetFlags();
00525
00526
00527 stc->validconfig = true;
00528 BOOST_CHECK( stc->configure() == true );
00529 BOOST_CHECK( stc->didconfig == true );
00530 BOOST_CHECK( stc->isConfigured() == true );
00531 BOOST_CHECK( stc->isRunning() == false );
00532 BOOST_CHECK( stc->isActive() == true );
00533 stc->resetFlags();
00534
00535
00536 BOOST_CHECK( stc->start() == false );
00537 BOOST_CHECK( stc->didstart == true );
00538 BOOST_CHECK( stc->isRunning() == false );
00539 BOOST_CHECK( stc->isActive() == true );
00540 BOOST_CHECK( stc->isConfigured() == true );
00541 stc->resetFlags();
00542
00543
00544 stc->validstart = true;
00545 BOOST_CHECK( stc->start() == true );
00546 BOOST_CHECK( stc->didstart == true );
00547 BOOST_CHECK( stc->isConfigured() == true );
00548 BOOST_CHECK( stc->isRunning() == true );
00549 BOOST_CHECK( stc->isActive() == true );
00550 stc->resetFlags();
00551
00552
00553 stc->do_error = true;
00554
00555 SimulationThread::Instance()->run(1);
00556 BOOST_CHECK( stc->inRunTimeError() == true );
00557 BOOST_CHECK( stc->diderror == true );
00558 BOOST_CHECK( stc->didexcept == false );
00559 BOOST_CHECK( stc->isActive() == true );
00560 stc->resetFlags();
00561 stc->do_error = false;
00562 stc->recover();
00563 SimulationThread::Instance()->run(1);
00564 BOOST_CHECK( stc->isRunning() == true );
00565 BOOST_CHECK( stc->diderror == false );
00566 BOOST_CHECK( stc->didupdate == true );
00567
00568
00569 stc->do_throw = true;
00570
00571 SimulationThread::Instance()->run(1);
00572 BOOST_CHECK( stc->inRunTimeError() == false );
00573 BOOST_CHECK( stc->inException() == true );
00574 BOOST_CHECK( stc->didexcept == true );
00575 BOOST_CHECK( stc->isActive() == true );
00576 stc->resetFlags();
00577 stc->do_throw = false;
00578 stc->recover();
00579 SimulationThread::Instance()->run(1);
00580 BOOST_CHECK( stc->isConfigured() == false );
00581 BOOST_CHECK( stc->diderror == false );
00582 BOOST_CHECK( stc->didupdate == false );
00583 stc->resetFlags();
00584 stc->configure();
00585 stc->start();
00586
00587
00588 stc->do_error = false;
00589 stc->do_throw = true;
00590 stc->do_throw3 = true;
00591
00592 SimulationThread::Instance()->run(1);
00593 BOOST_CHECK( stc->inRunTimeError() == false );
00594 BOOST_CHECK( stc->inFatalError() == true );
00595 BOOST_CHECK( stc->diderror == false );
00596 BOOST_CHECK( stc->didexcept == true );
00597 BOOST_CHECK( stc->didstop == true );
00598 BOOST_CHECK( stc->didcleanup == true );
00599 BOOST_CHECK( stc->isActive() == false );
00600 BOOST_CHECK( stc->isRunning() == false );
00601 BOOST_CHECK( stc->isConfigured() == false );
00602 stc->resetFlags();
00603
00604
00605 BOOST_CHECK( stc->configure() == false );
00606 BOOST_CHECK( stc->start() == false );
00607 }
00608
00609 BOOST_AUTO_TEST_CASE( testExecutionEngine)
00610 {
00611
00612 ExecutionEngine ee1(0);
00613 ExecutionEngine ee2(0);
00614
00615
00616 BOOST_CHECK( tsim->stop() );
00617 BOOST_CHECK( tsim->run(&ee1) );
00618 BOOST_CHECK(tsim->start() );
00619 BOOST_CHECK( SimulationThread::Instance()->run(5) );
00620
00621
00622 BOOST_CHECK( tsim->stop() );
00623 BOOST_CHECK( tsim->run(&ee2) );
00624 BOOST_CHECK(tsim->start() );
00625 BOOST_CHECK( SimulationThread::Instance()->run(5) );
00626
00627 {
00628 TaskCore tc1(&ee2);
00629 TaskCore tc2(&ee2);
00630
00631
00632 BOOST_CHECK( SimulationThread::Instance()->run(5) );
00633 }
00634
00635 BOOST_CHECK( SimulationThread::Instance()->run(5) );
00636 BOOST_CHECK(tsim->stop() );
00637 tsim->run(0);
00638 }
00639
00640 BOOST_AUTO_TEST_SUITE_END()
00641