00001
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef PeriodicExecutionContext_cpp
00033 #define PeriodicExecutionContext_cpp
00034
00035 #include <cppunit/ui/text/TestRunner.h>
00036 #include <cppunit/TextOutputter.h>
00037 #include <cppunit/extensions/TestFactoryRegistry.h>
00038 #include <cppunit/extensions/HelperMacros.h>
00039 #include <cppunit/TestAssert.h>
00040
00041 #include <rtm/idl/RTCSkel.h>
00042 #include <rtm/PeriodicExecutionContext.h>
00043 #include <rtm/CORBA_SeqUtil.h>
00044 #include <rtm/RTObject.h>
00045
00050 namespace PeriodicExecutionContext
00051 {
00052 class LightweightRTObjectMock
00053 : public virtual POA_RTC::LightweightRTObject,
00054 public virtual PortableServer::RefCountServantBase
00055 {
00056 protected:
00057 typedef std::map<RTC::UniqueId, RTC::ExecutionContext_ptr> ExecContexts;
00058 CORBA::Long m_nextUniqueId;
00059 ExecContexts m_execContexts;
00060 std::vector<std::string> m_log;
00061 bool m_alive;
00062 bool m_error;
00063
00064 public:
00065 LightweightRTObjectMock()
00066 : m_alive(true), m_error(false)
00067 {
00068 }
00069
00070
00071 virtual RTC::UniqueId attach_context(RTC::ExecutionContext_ptr exec_context)
00072 {
00073 m_log.push_back("attach_executioncontext");
00074 m_execContexts.insert(
00075 std::pair<RTC::UniqueId, RTC::ExecutionContext_ptr>(m_nextUniqueId++, exec_context));
00076 return m_nextUniqueId;
00077 }
00078 virtual RTC::ReturnCode_t detach_context(RTC::UniqueId ec_id)
00079 {
00080 m_log.push_back("detach_executioncontext");
00081 m_execContexts.erase(ec_id);
00082 return RTC::RTC_OK;
00083 }
00084 virtual RTC::ReturnCode_t on_initialize()
00085 {
00086 m_log.push_back("on_initialize");
00087 return RTC::RTC_OK;
00088 }
00089 virtual RTC::ReturnCode_t on_finalize()
00090 {
00091 m_log.push_back("on_finalize");
00092 return RTC::RTC_OK;
00093 }
00094 virtual RTC::ReturnCode_t on_startup(RTC::UniqueId ec_id)
00095 {
00096 m_log.push_back("on_startup");
00097 return RTC::RTC_OK;
00098 }
00099 virtual RTC::ReturnCode_t on_shutdown(RTC::UniqueId ec_id)
00100 {
00101 m_log.push_back("on_shutdown");
00102 return RTC::RTC_OK;
00103 }
00104 virtual RTC::ReturnCode_t on_activated(RTC::UniqueId ec_id)
00105 {
00106 m_log.push_back("on_activated");
00107 return returnCode(RTC::RTC_OK);
00108 }
00109 virtual RTC::ReturnCode_t on_deactivated(RTC::UniqueId ec_id)
00110 {
00111 m_log.push_back("on_deactivated");
00112 return RTC::RTC_OK;
00113 }
00114 virtual RTC::ReturnCode_t on_aborting(RTC::UniqueId ec_id)
00115 {
00116 m_log.push_back("on_aborting");
00117 return RTC::RTC_OK;
00118 }
00119 virtual RTC::ReturnCode_t on_error(RTC::UniqueId ec_id)
00120 {
00121 m_log.push_back("on_error");
00122 return RTC::RTC_OK;
00123 }
00124 virtual RTC::ReturnCode_t on_reset(RTC::UniqueId ec_id)
00125 {
00126 m_log.push_back("on_reset");
00127 return RTC::RTC_OK;
00128 }
00129
00130
00131 virtual RTC::ReturnCode_t initialize()
00132 {
00133 m_log.push_back("initialize");
00134 return RTC::RTC_OK;
00135 }
00136 virtual RTC::ReturnCode_t finalize()
00137 {
00138 m_log.push_back("finalize");
00139 return RTC::RTC_OK;
00140 }
00141 virtual RTC::ReturnCode_t exit()
00142 {
00143 m_log.push_back("exit");
00144 return RTC::RTC_OK;
00145 }
00146 virtual CORBA::Boolean is_alive(RTC::_objref_ExecutionContext* exec_context)
00147 {
00148 m_log.push_back("is_alive");
00149 return CORBA::Boolean(m_alive);
00150 }
00151 virtual RTC::ExecutionContextList* get_owned_contexts()
00152 {
00153 m_log.push_back("get_contexts");
00154 return 0;
00155 }
00156 virtual RTC::ExecutionContextList* get_participating_contexts()
00157 {
00158 m_log.push_back("get_context");
00159 return 0;
00160
00161 }
00162
00163 virtual RTC::_objref_ExecutionContext* get_context(RTC::ExecutionContextHandle_t)
00164 {
00165 return 0;
00166 }
00167 virtual RTC::ExecutionContextHandle_t get_context_handle(RTC::_objref_ExecutionContext*)
00168 {
00169 return 0;
00170 }
00171 public:
00172 int countLog(std::string line)
00173 {
00174 int count = 0;
00175 for (int i = 0; i < (int) m_log.size(); ++i)
00176 {
00177 if (m_log[i] == line) ++count;
00178 }
00179 return count;
00180 }
00181
00182 void setAlive(bool alive)
00183 {
00184 m_alive = alive;
00185 }
00186
00187 void setError(bool error)
00188 {
00189 m_error = error;
00190 }
00191
00192 private:
00193 RTC::ReturnCode_t returnCode(RTC::ReturnCode_t rc)
00194 {
00195 return m_error ? RTC::RTC_ERROR : rc;
00196 }
00197 };
00198
00199 class DataFlowComponentMock
00200
00201 : public virtual POA_OpenRTM::DataFlowComponent,
00202 public virtual LightweightRTObjectMock
00203 {
00204 public:
00205
00206 virtual SDOPackage::OrganizationList* get_owned_organizations()
00207 throw (SDOPackage::NotAvailable)
00208 {
00209 m_log.push_back("get_owned_organizations");
00210 return 0;
00211 }
00212 virtual char* get_sdo_id()
00213 throw (SDOPackage::NotAvailable, SDOPackage::InternalError)
00214 {
00215 m_log.push_back("get_sdo_id");
00216 return 0;
00217 }
00218 virtual char* get_sdo_type()
00219 throw (SDOPackage::NotAvailable, SDOPackage::InternalError)
00220 {
00221 m_log.push_back("get_sdo_type");
00222 return 0;
00223 }
00224 virtual SDOPackage::DeviceProfile* get_device_profile()
00225 throw (SDOPackage::NotAvailable, SDOPackage::InternalError)
00226 {
00227 m_log.push_back("get_device_profile");
00228 return 0;
00229 }
00230 virtual SDOPackage::ServiceProfileList* get_service_profiles()
00231 throw (SDOPackage::InvalidParameter, SDOPackage::NotAvailable, SDOPackage::InternalError)
00232 {
00233 m_log.push_back("get_service_profiles");
00234 return 0;
00235 }
00236 virtual SDOPackage::ServiceProfile* get_service_profile(const char* id)
00237 throw (SDOPackage::InvalidParameter, SDOPackage::NotAvailable, SDOPackage::InternalError)
00238 {
00239 m_log.push_back("get_service_profile");
00240 return 0;
00241 }
00242 virtual SDOPackage::SDOService_ptr get_sdo_service(const char* id)
00243 throw (SDOPackage::InvalidParameter, SDOPackage::NotAvailable, SDOPackage::InternalError)
00244 {
00245 m_log.push_back("get_sdo_service");
00246 return SDOPackage::SDOService::_nil();
00247 }
00248 virtual SDOPackage::Configuration_ptr get_configuration()
00249 throw (SDOPackage::InterfaceNotImplemented, SDOPackage::NotAvailable, SDOPackage::InternalError)
00250 {
00251 m_log.push_back("get_configuration");
00252 return SDOPackage::Configuration::_nil();
00253 }
00254 virtual SDOPackage::Monitoring_ptr get_monitoring()
00255 throw (SDOPackage::InterfaceNotImplemented, SDOPackage::NotAvailable, SDOPackage::InternalError)
00256 {
00257 m_log.push_back("get_monitoring");
00258 return SDOPackage::Monitoring::_nil();
00259 }
00260 virtual SDOPackage::OrganizationList* get_organizations()
00261 throw (SDOPackage::NotAvailable, SDOPackage::InternalError)
00262 {
00263 m_log.push_back("get_organizations");
00264 return 0;
00265 }
00266 virtual SDOPackage::NVList* get_status_list()
00267 throw (SDOPackage::NotAvailable, SDOPackage::InternalError)
00268 {
00269 m_log.push_back("get_status_list");
00270 return 0;
00271 }
00272 virtual CORBA::Any* get_status(const char* name)
00273 throw (SDOPackage::InvalidParameter, SDOPackage::NotAvailable, SDOPackage::InternalError)
00274 {
00275 m_log.push_back("get_status");
00276 return 0;
00277 }
00278
00279
00280 virtual RTC::ComponentProfile* get_component_profile()
00281 {
00282 m_log.push_back("get_component_profile");
00283
00284 RTC::ComponentProfile_var prof(new RTC::ComponentProfile());
00285 return prof._retn();
00286 }
00287 virtual RTC::PortServiceList* get_ports()
00288 {
00289 m_log.push_back("get_ports");
00290
00291 RTC::PortServiceList_var ports(new RTC::PortServiceList());
00292 ports->length(0);
00293 return ports._retn();
00294 }
00295 virtual RTC::ExecutionContextServiceList* get_execution_context_services()
00296 {
00297 m_log.push_back("get_execution_context_services");
00298
00299 RTC::ExecutionContextServiceList_var ec = new RTC::ExecutionContextServiceList();
00300 ec->length(0);
00301 return ec._retn();
00302 }
00303
00304
00305 virtual RTC::ReturnCode_t on_execute(RTC::UniqueId ec_id)
00306 {
00307 m_log.push_back("on_execute");
00308 return RTC::RTC_OK;
00309 }
00310 virtual RTC::ReturnCode_t on_state_update(RTC::UniqueId ec_id)
00311 {
00312 m_log.push_back("on_state_update");
00313 return RTC::RTC_OK;
00314 }
00315 virtual RTC::ReturnCode_t on_rate_changed(RTC::UniqueId ec_id)
00316 {
00317 m_log.push_back("on_rate_changed");
00318 return RTC::RTC_OK;
00319 }
00320 };
00321
00322 class RTObjectMock
00323 : public RTC::RTObject_impl
00324 {
00325 public:
00326 RTObjectMock(CORBA::ORB_ptr orb, PortableServer::POA_ptr poa)
00327 : RTC::RTObject_impl(orb, poa)
00328 {
00329 }
00330 virtual ~RTObjectMock(void){}
00331 };
00332
00333 class PeriodicExecutionContextMock
00334 : public RTC::PeriodicExecutionContext
00335 {
00336 public:
00337 PeriodicExecutionContextMock() : RTC::PeriodicExecutionContext() {}
00338 virtual ~PeriodicExecutionContextMock(void){}
00339
00340
00341 void set_m_ref()
00342 {
00343 RTC::PeriodicExecutionContext::m_ref = m_refmock;
00344 }
00345 void clear_m_comps()
00346 {
00347 if (!RTC::PeriodicExecutionContext::m_comps.empty())
00348 {
00349 RTC::PeriodicExecutionContext::m_comps.clear();
00350 }
00351 }
00352 RTC::ExecutionContextService_var m_refmock;
00353 };
00354
00355 class PeriodicExecutionContextTests
00356 : public CppUnit::TestFixture
00357 {
00358 CPPUNIT_TEST_SUITE(PeriodicExecutionContextTests);
00359
00360 CPPUNIT_TEST(test_is_running);
00361 CPPUNIT_TEST(test_start_invoking_on_startup);
00362 CPPUNIT_TEST(test_start_with_running);
00363 CPPUNIT_TEST(test_start_with_not_alive);
00364 CPPUNIT_TEST(test_stop_invoking_on_shutdown);
00365 CPPUNIT_TEST(test_stop_with_not_running);
00366 CPPUNIT_TEST(test_start_and_stop_multiple_times);
00367 CPPUNIT_TEST(test_set_rate_and_get_rate);
00368 CPPUNIT_TEST(test_set_rate_with_zero_or_negative_rate);
00369 CPPUNIT_TEST(test_set_rate_invoking_on_rate_changed);
00370 CPPUNIT_TEST(test_add_invoking_attach_executioncontext);
00371 CPPUNIT_TEST(test_add_not_with_data_flow_component);
00372 CPPUNIT_TEST(test_remove_invoking_detach_executioncontext);
00373 CPPUNIT_TEST(test_remove_with_not_attached_component);
00374 CPPUNIT_TEST(test_remove_when_component_is_still_active);
00375 CPPUNIT_TEST(test_remove_when_component_is_inactive);
00376 CPPUNIT_TEST(test_activate_component_invoking_on_activated);
00377 CPPUNIT_TEST(test_activate_component_without_participating);
00378 CPPUNIT_TEST(test_activate_component_in_Error_state);
00379 CPPUNIT_TEST(test_activate_component_not_in_Alive_state);
00380 CPPUNIT_TEST(test_deactivate_component_invoking_on_deactivated);
00381 CPPUNIT_TEST(test_deactivate_component_without_participating);
00382 CPPUNIT_TEST(test_deactivate_component_not_in_Alive_state);
00383 CPPUNIT_TEST(test_reset_component_invoking_on_reset);
00384 CPPUNIT_TEST(test_reset_component_not_in_Error_state);
00385 CPPUNIT_TEST(test_reset_component_not_in_Alive_state);
00386 CPPUNIT_TEST(test_bindComponent);
00387
00388 CPPUNIT_TEST_SUITE_END();
00389
00390 private:
00391 CORBA::ORB_ptr m_pORB;
00392 PortableServer::POA_ptr m_pPOA;
00393
00394 public:
00395
00399 PeriodicExecutionContextTests()
00400 {
00401 int argc(0);
00402 char** argv(NULL);
00403 m_pORB = CORBA::ORB_init(argc, argv);
00404 m_pPOA = PortableServer::POA::_narrow(
00405 m_pORB->resolve_initial_references("RootPOA"));
00406 m_pPOA->the_POAManager()->activate();
00407 }
00408
00412 virtual ~PeriodicExecutionContextTests()
00413 {
00414 }
00415
00419 virtual void setUp()
00420 {
00421 coil::usleep(100000);
00422 }
00423
00427 virtual void tearDown()
00428 {
00429 }
00430
00438 void test_is_running()
00439 {
00440 RTC::PeriodicExecutionContext* ec
00441 = new RTC::PeriodicExecutionContext();
00442
00443
00444 CPPUNIT_ASSERT_EQUAL(CORBA::Boolean(false), ec->is_running());
00445
00446
00447 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->start());
00448 CPPUNIT_ASSERT_EQUAL(CORBA::Boolean(true), ec->is_running());
00449
00450
00451 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->stop());
00452 CPPUNIT_ASSERT_EQUAL(CORBA::Boolean(false), ec->is_running());
00453
00454 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ec));
00455 delete ec;
00456 }
00457
00463 void test_start_invoking_on_startup()
00464 {
00465
00466 POA_RTC::LightweightRTObject* rto
00467 = new DataFlowComponentMock();
00468 DataFlowComponentMock* mock
00469 = dynamic_cast<DataFlowComponentMock*>(rto);
00470
00471
00472 RTC::PeriodicExecutionContext* ec
00473 = new RTC::PeriodicExecutionContext();
00474
00475
00476 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->add_component(rto->_this()));
00477
00478
00479 CPPUNIT_ASSERT_EQUAL(0, mock->countLog("on_startup"));
00480
00481
00482 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->start());
00483
00484
00485 CPPUNIT_ASSERT_EQUAL(1, mock->countLog("on_startup"));
00486
00487 ec->stop();
00488 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ec));
00489 delete ec;
00490 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(rto));
00491 delete rto;
00492 }
00493
00499 void test_start_with_running()
00500 {
00501 RTC::PeriodicExecutionContext* ec
00502 = new RTC::PeriodicExecutionContext();
00503
00504
00505 CPPUNIT_ASSERT_EQUAL(CORBA::Boolean(false), ec->is_running());
00506
00507
00508 ec->start();
00509 CPPUNIT_ASSERT_EQUAL(CORBA::Boolean(true), ec->is_running());
00510
00511
00512 CPPUNIT_ASSERT_EQUAL(RTC::PRECONDITION_NOT_MET, ec->start());
00513
00514 ec->stop();
00515 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ec));
00516 delete ec;
00517 }
00518
00524 void test_start_with_not_alive()
00525 {
00526
00527 POA_RTC::LightweightRTObject* rto
00528 = new DataFlowComponentMock();
00529 DataFlowComponentMock* mock
00530 = dynamic_cast<DataFlowComponentMock*>(rto);
00531
00532
00533 RTC::PeriodicExecutionContext* ec
00534 = new RTC::PeriodicExecutionContext();
00535
00536
00537 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->add_component(rto->_this()));
00538
00539
00540 mock->setAlive(false);
00541 CPPUNIT_ASSERT_EQUAL(CORBA::Boolean(false), rto->is_alive(NULL));
00542
00543 ec->stop();
00544 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ec));
00545 delete ec;
00546 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(rto));
00547 delete rto;
00548 }
00549
00555 void test_stop_invoking_on_shutdown()
00556 {
00557
00558 POA_RTC::LightweightRTObject* rto
00559 = new DataFlowComponentMock();
00560 DataFlowComponentMock* mock
00561 = dynamic_cast<DataFlowComponentMock*>(rto);
00562
00563
00564 RTC::PeriodicExecutionContext* ec
00565 = new RTC::PeriodicExecutionContext();
00566
00567
00568 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->add_component(rto->_this()));
00569
00570
00571 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->start());
00572
00573
00574 CPPUNIT_ASSERT_EQUAL(0, mock->countLog("on_shutdown"));
00575
00576
00577 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->stop());
00578
00579
00580 CPPUNIT_ASSERT_EQUAL(1, mock->countLog("on_shutdown"));
00581
00582 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ec));
00583 delete ec;
00584 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(rto));
00585 delete rto;
00586 }
00587
00593 void test_stop_with_not_running()
00594 {
00595
00596 RTC::PeriodicExecutionContext* ec
00597 = new RTC::PeriodicExecutionContext();
00598
00599
00600
00601 CPPUNIT_ASSERT_EQUAL(CORBA::Boolean(false), ec->is_running());
00602 CPPUNIT_ASSERT_EQUAL(RTC::PRECONDITION_NOT_MET, ec->stop());
00603
00604
00605 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->start());
00606
00607
00608 CPPUNIT_ASSERT_EQUAL(CORBA::Boolean(true), ec->is_running());
00609 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->stop());
00610
00611
00612 CPPUNIT_ASSERT_EQUAL(CORBA::Boolean(false), ec->is_running());
00613 CPPUNIT_ASSERT_EQUAL(RTC::PRECONDITION_NOT_MET, ec->stop());
00614
00615 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ec));
00616 delete ec;
00617 }
00618
00624 void test_start_and_stop_multiple_times()
00625 {
00626
00627 RTC::PeriodicExecutionContext* ec
00628 = new RTC::PeriodicExecutionContext();
00629
00630
00631 for (int i = 0; i < 1000; ++i)
00632 {
00633
00634 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->start());
00635
00636
00637 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->stop());
00638 }
00639
00640 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ec));
00641 delete ec;
00642 }
00643
00649 void test_set_rate_and_get_rate()
00650 {
00651
00652 RTC::PeriodicExecutionContext* ec
00653 = new RTC::PeriodicExecutionContext();
00654
00655
00656 for (int i = 1; i <= 10; ++i)
00657 {
00658 CORBA::Double rate((double) 1.0 * i);
00659 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->set_rate(rate));
00660 CPPUNIT_ASSERT_EQUAL(rate, ec->get_rate());
00661 }
00662
00663 ec->stop();
00664 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ec));
00665 delete ec;
00666 }
00667
00674 void test_set_rate_with_zero_or_negative_rate()
00675 {
00676
00677 RTC::PeriodicExecutionContext* ec
00678 = new RTC::PeriodicExecutionContext();
00679
00680
00681 for (int i = 0; i < 10; ++i)
00682 {
00683 CORBA::Double rate((double) - 1.0 * i);
00684 CPPUNIT_ASSERT_EQUAL(RTC::BAD_PARAMETER, ec->set_rate(rate));
00685 }
00686
00687 ec->stop();
00688 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ec));
00689 delete ec;
00690 }
00691
00697 void test_set_rate_invoking_on_rate_changed()
00698 {
00699
00700 POA_RTC::LightweightRTObject* rto
00701 = new DataFlowComponentMock();
00702 DataFlowComponentMock* mock
00703 = dynamic_cast<DataFlowComponentMock*>(rto);
00704
00705
00706 RTC::PeriodicExecutionContext* ec
00707 = new RTC::PeriodicExecutionContext();
00708 CPPUNIT_ASSERT_EQUAL(RTC::PERIODIC, ec->get_kind());
00709
00710
00711 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->add_component(rto->_this()));
00712
00713
00714 CPPUNIT_ASSERT_EQUAL(0, mock->countLog("on_rate_changed"));
00715
00716 RTC::ExecutionContextProfile* ecp;
00717 ecp = ec->get_profile();
00718 CPPUNIT_ASSERT_EQUAL(CORBA::Double(1000000), ecp->rate);
00719
00720
00721 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->set_rate(CORBA::Double(1.0)));
00722 ecp = ec->get_profile();
00723 CPPUNIT_ASSERT_EQUAL(CORBA::Double(1.0), ecp->rate);
00724
00725
00726 CPPUNIT_ASSERT_EQUAL(1, mock->countLog("on_rate_changed"));
00727
00728 ec->stop();
00729 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ec));
00730 delete ec;
00731 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(rto));
00732 delete rto;
00733 }
00734
00740 void test_add_invoking_attach_executioncontext()
00741 {
00742
00743 POA_RTC::LightweightRTObject* rto
00744 = new DataFlowComponentMock();
00745 DataFlowComponentMock* mock
00746 = dynamic_cast<DataFlowComponentMock*>(rto);
00747
00748
00749 RTC::PeriodicExecutionContext* ec
00750 = new RTC::PeriodicExecutionContext();
00751
00752
00753 CPPUNIT_ASSERT_EQUAL(0, mock->countLog("attach_executioncontext"));
00754
00755
00756 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->add_component(rto->_this()));
00757
00758
00759 CPPUNIT_ASSERT_EQUAL(1, mock->countLog("attach_executioncontext"));
00760
00761 ec->stop();
00762 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ec));
00763 delete ec;
00764 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(rto));
00765 delete rto;
00766 }
00767
00773 void test_add_not_with_data_flow_component()
00774 {
00775
00776
00777 POA_RTC::LightweightRTObject* rto
00778 = new LightweightRTObjectMock();
00779
00780
00781 RTC::PeriodicExecutionContext* ec
00782 = new RTC::PeriodicExecutionContext();
00783 CPPUNIT_ASSERT_EQUAL(RTC::PERIODIC, ec->get_kind());
00784
00785
00786
00787
00788
00789 ec->stop();
00790 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ec));
00791 delete ec;
00792 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(rto));
00793 delete rto;
00794 }
00795
00801 void test_remove_invoking_detach_executioncontext()
00802 {
00803
00804 POA_RTC::LightweightRTObject* rto
00805 = new DataFlowComponentMock();
00806 DataFlowComponentMock* mock
00807 = dynamic_cast<DataFlowComponentMock*>(rto);
00808
00809
00810 RTC::PeriodicExecutionContext* ec
00811 = new RTC::PeriodicExecutionContext();
00812
00813
00814 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->add_component(rto->_this()));
00815
00816
00817 CPPUNIT_ASSERT_EQUAL(0, mock->countLog("detach_executioncontext"));
00818
00819
00820 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->remove_component(rto->_this()));
00821
00822
00823 CPPUNIT_ASSERT_EQUAL(1, mock->countLog("detach_executioncontext"));
00824
00825 ec->stop();
00826 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ec));
00827 delete ec;
00828 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(rto));
00829 delete rto;
00830 }
00831
00837 void test_remove_with_not_attached_component()
00838 {
00839
00840 POA_RTC::LightweightRTObject* rto
00841 = new DataFlowComponentMock();
00842
00843
00844 RTC::PeriodicExecutionContext* ec
00845 = new RTC::PeriodicExecutionContext();
00846
00847
00848
00849 CPPUNIT_ASSERT_EQUAL(RTC::BAD_PARAMETER, ec->remove_component(rto->_this()));
00850
00851 ec->stop();
00852 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ec));
00853 delete ec;
00854 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(rto));
00855 delete rto;
00856 }
00857
00863 void test_remove_when_component_is_still_active()
00864 {
00865
00866 POA_RTC::LightweightRTObject* rto
00867 = new DataFlowComponentMock();
00868
00869
00870 RTC::PeriodicExecutionContext* ec
00871 = new RTC::PeriodicExecutionContext();
00872 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->start());
00873
00874
00875 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->add_component(rto->_this()));
00876
00877
00878 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->activate_component(rto->_this()));
00879 usleep(100000);
00880 CPPUNIT_ASSERT_EQUAL(RTC::ACTIVE_STATE, ec->get_component_state(rto->_this()));
00881
00882
00883
00884
00885 ec->stop();
00886 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ec));
00887 delete ec;
00888 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(rto));
00889 delete rto;
00890 }
00891
00897 void test_remove_when_component_is_inactive()
00898 {
00899
00900 POA_RTC::LightweightRTObject* rto
00901 = new DataFlowComponentMock();
00902
00903
00904 RTC::PeriodicExecutionContext* ec
00905 = new RTC::PeriodicExecutionContext();
00906 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->start());
00907
00908
00909 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->add_component(rto->_this()));
00910
00911
00912 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->activate_component(rto->_this()));
00913 coil::usleep(100000);
00914 CPPUNIT_ASSERT_EQUAL(RTC::ACTIVE_STATE, ec->get_component_state(rto->_this()));
00915
00916
00917 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->deactivate_component(rto->_this()));
00918 coil::usleep(100000);
00919 CPPUNIT_ASSERT_EQUAL(RTC::INACTIVE_STATE, ec->get_component_state(rto->_this()));
00920
00921
00922 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->remove_component(rto->_this()));
00923
00924 ec->stop();
00925 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ec));
00926 delete ec;
00927 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(rto));
00928 delete rto;
00929 }
00930
00936 void test_activate_component_invoking_on_activated()
00937 {
00938
00939 POA_RTC::LightweightRTObject* rto
00940 = new DataFlowComponentMock();
00941 DataFlowComponentMock* mock
00942 = dynamic_cast<DataFlowComponentMock*>(rto);
00943
00944
00945 RTC::PeriodicExecutionContext* ec
00946 = new RTC::PeriodicExecutionContext();
00947 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->start());
00948
00949
00950 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->add_component(rto->_this()));
00951
00952
00953 CPPUNIT_ASSERT_EQUAL(0, mock->countLog("on_activated"));
00954
00955
00956 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->activate_component(rto->_this()));
00957
00958 coil::usleep(100000);
00959
00960
00961 CPPUNIT_ASSERT_EQUAL(1, mock->countLog("on_activated"));
00962
00963
00964
00965 CPPUNIT_ASSERT_EQUAL(RTC::ACTIVE_STATE, ec->get_component_state(rto->_this()));
00966
00967 ec->stop();
00968 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ec));
00969 delete ec;
00970 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(rto));
00971 delete rto;
00972 }
00973
00979 void test_activate_component_without_participating()
00980 {
00981
00982 POA_RTC::LightweightRTObject* rto
00983 = new DataFlowComponentMock();
00984
00985
00986 RTC::PeriodicExecutionContext* ec
00987 = new RTC::PeriodicExecutionContext();
00988 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->start());
00989
00990
00991
00992 CPPUNIT_ASSERT_EQUAL(RTC::BAD_PARAMETER, ec->activate_component(rto->_this()));
00993
00994 ec->stop();
00995 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ec));
00996 delete ec;
00997 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(rto));
00998 delete rto;
00999 }
01000
01006 void test_activate_component_in_Error_state()
01007 {
01008
01009 POA_RTC::LightweightRTObject* rto
01010 = new DataFlowComponentMock();
01011 DataFlowComponentMock* mock
01012 = dynamic_cast<DataFlowComponentMock*>(rto);
01013
01014
01015 RTC::PeriodicExecutionContext* ec
01016 = new RTC::PeriodicExecutionContext();
01017 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->start());
01018
01019
01020 mock->setError(true);
01021 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->add_component(rto->_this()));
01022 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->activate_component(rto->_this()));
01023 coil::sleep(1);
01024 CPPUNIT_ASSERT_EQUAL(RTC::ERROR_STATE, ec->get_component_state(rto->_this()));
01025
01026
01027 CPPUNIT_ASSERT_EQUAL(RTC::PRECONDITION_NOT_MET, ec->activate_component(rto->_this()));
01028
01029 ec->stop();
01030 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ec));
01031 delete ec;
01032 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(rto));
01033 delete rto;
01034 }
01035
01041 void test_activate_component_not_in_Alive_state()
01042 {
01043
01044 POA_RTC::LightweightRTObject* rto
01045 = new DataFlowComponentMock();
01046 DataFlowComponentMock* mock
01047 = dynamic_cast<DataFlowComponentMock*>(rto);
01048
01049
01050 RTC::PeriodicExecutionContext* ec
01051 = new RTC::PeriodicExecutionContext();
01052
01053
01054 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->add_component(rto->_this()));
01055
01056
01057 mock->setAlive(false);
01058 CPPUNIT_ASSERT_EQUAL(CORBA::Boolean(false), rto->is_alive(NULL));
01059
01060
01061
01062
01063 ec->stop();
01064 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ec));
01065 delete ec;
01066 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(rto));
01067 delete rto;
01068 }
01069
01075 void test_deactivate_component_invoking_on_deactivated()
01076 {
01077
01078 POA_RTC::LightweightRTObject* rto
01079 = new DataFlowComponentMock();
01080 DataFlowComponentMock* mock
01081 = dynamic_cast<DataFlowComponentMock*>(rto);
01082
01083
01084 RTC::PeriodicExecutionContext* ec
01085 = new RTC::PeriodicExecutionContext();
01086 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->start());
01087
01088
01089 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->add_component(rto->_this()));
01090
01091
01092 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->activate_component(rto->_this()));
01093 coil::usleep(100000);
01094 CPPUNIT_ASSERT_EQUAL(RTC::ACTIVE_STATE, ec->get_component_state(rto->_this()));
01095
01096
01097 CPPUNIT_ASSERT_EQUAL(0, mock->countLog("on_deactivated"));
01098
01099
01100 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->deactivate_component(rto->_this()));
01101 coil::usleep(100000);
01102 CPPUNIT_ASSERT_EQUAL(RTC::INACTIVE_STATE, ec->get_component_state(rto->_this()));
01103
01104
01105 CPPUNIT_ASSERT_EQUAL(1, mock->countLog("on_deactivated"));
01106
01107 ec->stop();
01108 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ec));
01109 delete ec;
01110 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(rto));
01111 delete rto;
01112 }
01113
01119 void test_deactivate_component_without_participating()
01120 {
01121
01122 POA_RTC::LightweightRTObject* rto
01123 = new DataFlowComponentMock();
01124
01125
01126 RTC::PeriodicExecutionContext* ec
01127 = new RTC::PeriodicExecutionContext();
01128 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->start());
01129
01130
01131
01132 CPPUNIT_ASSERT_EQUAL(RTC::BAD_PARAMETER, ec->deactivate_component(rto->_this()));
01133
01134 ec->stop();
01135 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ec));
01136 delete ec;
01137 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(rto));
01138 delete rto;
01139 }
01140
01146 void test_deactivate_component_not_in_Alive_state()
01147 {
01148
01149 POA_RTC::LightweightRTObject* rto
01150 = new DataFlowComponentMock();
01151 DataFlowComponentMock* mock
01152 = dynamic_cast<DataFlowComponentMock*>(rto);
01153
01154
01155 RTC::PeriodicExecutionContext* ec
01156 = new RTC::PeriodicExecutionContext();
01157
01158 ec->start();
01159
01160
01161 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->add_component(rto->_this()));
01162
01163
01164 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->activate_component(rto->_this()));
01165 usleep(100000);
01166 CPPUNIT_ASSERT_EQUAL(RTC::ACTIVE_STATE, ec->get_component_state(rto->_this()));
01167
01168
01169 mock->setAlive(false);
01170 CPPUNIT_ASSERT_EQUAL(CORBA::Boolean(false), rto->is_alive(NULL));
01171
01172
01173
01174
01175 ec->stop();
01176 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ec));
01177 delete ec;
01178 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(rto));
01179 delete rto;
01180 }
01181
01187 void test_reset_component_invoking_on_reset()
01188 {
01189
01190 POA_RTC::LightweightRTObject* rto
01191 = new DataFlowComponentMock();
01192 DataFlowComponentMock* mock
01193 = dynamic_cast<DataFlowComponentMock*>(rto);
01194
01195
01196 RTC::PeriodicExecutionContext* ec
01197 = new RTC::PeriodicExecutionContext();
01198 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->start());
01199
01200
01201 mock->setError(true);
01202 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->add_component(rto->_this()));
01203 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->activate_component(rto->_this()));
01204 coil::sleep(1);
01205 CPPUNIT_ASSERT_EQUAL(RTC::ERROR_STATE, ec->get_component_state(rto->_this()));
01206
01207
01208 CPPUNIT_ASSERT_EQUAL(0, mock->countLog("on_reset"));
01209
01210
01211 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->reset_component(rto->_this()));
01212 coil::usleep(100000);
01213
01214 CPPUNIT_ASSERT_EQUAL(1, mock->countLog("on_reset"));
01215
01216 ec->stop();
01217 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ec));
01218 delete ec;
01219 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(rto));
01220 delete rto;
01221 }
01222
01228 void test_reset_component_not_in_Error_state()
01229 {
01230
01231 POA_RTC::LightweightRTObject* rto
01232 = new DataFlowComponentMock();
01233
01234
01235 RTC::PeriodicExecutionContext* ec
01236 = new RTC::PeriodicExecutionContext();
01237 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->start());
01238
01239
01240 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->add_component(rto->_this()));
01241
01242
01243 CPPUNIT_ASSERT_EQUAL(RTC::INACTIVE_STATE, ec->get_component_state(rto->_this()));
01244 CPPUNIT_ASSERT_EQUAL(RTC::PRECONDITION_NOT_MET, ec->reset_component(rto->_this()));
01245
01246 ec->stop();
01247 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ec));
01248 delete ec;
01249 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(rto));
01250 delete rto;
01251 }
01252
01258 void test_reset_component_not_in_Alive_state()
01259 {
01260
01261 POA_RTC::LightweightRTObject* rto
01262 = new DataFlowComponentMock();
01263 DataFlowComponentMock* mock
01264 = dynamic_cast<DataFlowComponentMock*>(rto);
01265
01266
01267 mock->setAlive(false);
01268 CPPUNIT_ASSERT_EQUAL(CORBA::Boolean(false), rto->is_alive(NULL));
01269
01270
01271 RTC::PeriodicExecutionContext* ec
01272 = new RTC::PeriodicExecutionContext();
01273
01274
01275 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->add_component(rto->_this()));
01276
01277
01278 CPPUNIT_ASSERT_EQUAL(RTC::PRECONDITION_NOT_MET, ec->reset_component(rto->_this()));
01279
01280 ec->stop();
01281 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ec));
01282 delete ec;
01283 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(rto));
01284 delete rto;
01285 }
01286
01292 void test_bindComponent()
01293 {
01294
01295 RTObjectMock* rto = new RTObjectMock(m_pORB, m_pPOA);
01296 coil::Properties prop;
01297 prop.setProperty("exec_cxt.periodic.type","PeriodicExecutionContext");
01298 prop.setProperty("exec_cxt.periodic.rate","1000");
01299 rto->setProperties(prop);
01300
01301
01302 PeriodicExecutionContextMock* ec = new PeriodicExecutionContextMock();
01303
01304
01305 CPPUNIT_ASSERT_EQUAL(RTC::BAD_PARAMETER, ec->bindComponent(NULL));
01306
01307
01308 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->bindComponent(rto));
01309
01310
01311 for (int i = 0; i < 1000; ++i)
01312 {
01313 ec->bindComponent(rto);
01314 }
01315 CPPUNIT_ASSERT_EQUAL(RTC::RTC_ERROR, ec->bindComponent(rto));
01316
01317
01318 ec->m_refmock = RTC::ExecutionContextService::_nil();
01319 ec->set_m_ref();
01320 CPPUNIT_ASSERT_EQUAL(RTC::RTC_ERROR, ec->bindComponent(rto));
01321
01322
01323 ec->clear_m_comps();
01324
01325 ec->stop();
01326 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ec));
01327 delete ec;
01328 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(rto));
01329 delete rto;
01330 }
01331
01332 };
01333 };
01334
01335
01336
01337
01338 CPPUNIT_TEST_SUITE_REGISTRATION(PeriodicExecutionContext::PeriodicExecutionContextTests);
01339
01340 #ifdef LOCAL_MAIN
01341 int main(int argc, char* argv[])
01342 {
01343
01344 FORMAT format = TEXT_OUT;
01345 int target = 0;
01346 std::string xsl;
01347 std::string ns;
01348 std::string fname;
01349 std::ofstream ofs;
01350
01351 int i(1);
01352 while (i < argc)
01353 {
01354 std::string arg(argv[i]);
01355 std::string next_arg;
01356 if (i + 1 < argc) next_arg = argv[i + 1];
01357 else next_arg = "";
01358
01359 if (arg == "--text") { format = TEXT_OUT; break; }
01360 if (arg == "--xml")
01361 {
01362 if (next_arg == "")
01363 {
01364 fname = argv[0];
01365 fname += ".xml";
01366 }
01367 else
01368 {
01369 fname = next_arg;
01370 }
01371 format = XML_OUT;
01372 ofs.open(fname.c_str());
01373 }
01374 if ( arg == "--compiler" ) { format = COMPILER_OUT; break; }
01375 if ( arg == "--cerr" ) { target = 1; break; }
01376 if ( arg == "--xsl" )
01377 {
01378 if (next_arg == "") xsl = "default.xsl";
01379 else xsl = next_arg;
01380 }
01381 if ( arg == "--namespace" )
01382 {
01383 if (next_arg == "")
01384 {
01385 std::cerr << "no namespace specified" << std::endl;
01386 exit(1);
01387 }
01388 else
01389 {
01390 xsl = next_arg;
01391 }
01392 }
01393 ++i;
01394 }
01395 CppUnit::TextUi::TestRunner runner;
01396 if ( ns.empty() )
01397 runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
01398 else
01399 runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
01400 CppUnit::Outputter* outputter = 0;
01401 std::ostream* stream = target ? &std::cerr : &std::cout;
01402 switch ( format )
01403 {
01404 case TEXT_OUT :
01405 outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
01406 break;
01407 case XML_OUT :
01408 std::cout << "XML_OUT" << std::endl;
01409 outputter = new CppUnit::XmlOutputter(&runner.result(),
01410 ofs, "shift_jis");
01411 static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
01412 break;
01413 case COMPILER_OUT :
01414 outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
01415 break;
01416 }
01417 runner.setOutputter(outputter);
01418 runner.run();
01419 return 0;
01420 }
01421 #endif // MAIN
01422 #endif // PeriodicExecutionContext_cpp