PublisherFlushTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00012 /*
00013  * $Log: PublisherFlushTests.cpp,v $
00014  * Revision 1.2  2008/01/23 09:20:42  arafune
00015  * modified some tests.
00016  *
00017  * Revision 1.1  2007/12/20 07:50:18  arafune
00018  * *** empty log message ***
00019  *
00020  * Revision 1.2  2007/01/12 14:54:32  n-ando
00021  * The constructor's signature was changed.
00022  * InPortConsumer base class is now abstruct class. It needs concrete class.
00023  *
00024  * Revision 1.1  2006/12/18 06:51:31  n-ando
00025  * The first commitment.
00026  *
00027  *
00028  */
00029 
00030 #ifndef PublisherFlush_cpp
00031 #define PublisherFlush_cpp
00032 
00033 #include <cppunit/ui/text/TestRunner.h>
00034 #include <cppunit/TextOutputter.h>
00035 #include <cppunit/extensions/TestFactoryRegistry.h>
00036 #include <cppunit/extensions/HelperMacros.h>
00037 #include <cppunit/TestAssert.h>
00038 
00039 #include <rtm/InPortCorbaCdrConsumer.h>
00040 #include <rtm/PublisherFlush.h>
00041 #include <rtm/CdrRingBuffer.h>
00042 
00043 #include <rtm/idl/BasicDataTypeSkel.h>
00044 #include <rtm/ConnectorListener.h>
00045 
00046 #include <sys/time.h>
00052 namespace PublisherFlush
00053 {
00054 
00055   int m_OnCheck = 0;
00056 
00060   class DataListener
00061     : public RTC::ConnectorDataListenerT<RTC::TimedLong>
00062   {
00063   public:
00064     DataListener(const char* name) : m_name(name) {}
00065     virtual ~DataListener()
00066     {
00067     }
00068 
00069     virtual void operator()(const RTC::ConnectorInfo& info,
00070                             const RTC::TimedLong& data)
00071     {
00072       std::cout << "------------------------------" << std::endl;
00073       std::cout << "Listener: " << m_name << std::endl;
00074       std::cout << "    Data: " << data.data << std::endl;
00075       std::cout << "------------------------------" << std::endl;
00076     };
00077     std::string m_name;
00078   };
00079 
00084   class InPortCorbaCdrConsumerMock
00085     : public RTC::InPortCorbaCdrConsumer
00086   {
00087   public:
00092       InPortCorbaCdrConsumerMock(void)
00093       {
00094           m_buffer = new RTC::CdrRingBuffer();
00095       }
00100       virtual ~InPortCorbaCdrConsumerMock()
00101       {
00102           delete m_buffer;
00103       }
00108       virtual ReturnCode put(const cdrMemoryStream& data)
00109       {
00110           if (m_buffer->full())
00111           {
00112                return RTC::PublisherFlush::SEND_FULL;
00113            }
00114 
00115 
00116            RTC::BufferStatus::Enum ret = m_buffer->write(data);
00117 
00118           //Listener check
00119           if(m_OnCheck == 0) {
00120             switch(ret)
00121             {
00122                case RTC::BufferStatus::BUFFER_OK:
00123                    return RTC::PublisherFlush::PORT_OK;
00124                    break;
00125                case RTC::BufferStatus::BUFFER_ERROR:
00126                    return RTC::PublisherFlush::PORT_ERROR;
00127                    break;
00128                case RTC::BufferStatus::BUFFER_FULL:
00129                    return RTC::PublisherFlush::SEND_FULL;
00130                    break;
00131                case RTC::BufferStatus::BUFFER_EMPTY:
00132                    return RTC::PublisherFlush::BUFFER_EMPTY;
00133                    break;
00134                case RTC::BufferStatus::TIMEOUT:
00135                    return RTC::PublisherFlush::SEND_TIMEOUT;
00136                    break;
00137                default:
00138                    return RTC::PublisherFlush::UNKNOWN_ERROR;
00139             }
00140             return RTC::PublisherFlush::UNKNOWN_ERROR;
00141           }
00142           else if(m_OnCheck == 1) {
00143                return RTC::PublisherFlush::PORT_OK;
00144           }
00145           else if(m_OnCheck == 2) {
00146                return RTC::PublisherFlush::PORT_ERROR;
00147           }
00148           else if(m_OnCheck == 3) {
00149                return RTC::PublisherFlush::SEND_FULL;
00150           }
00151           else if(m_OnCheck == 4) {
00152                return RTC::PublisherFlush::SEND_TIMEOUT;
00153           }
00154           else if(m_OnCheck == 5) {
00155                return RTC::PublisherFlush::UNKNOWN_ERROR;
00156           }
00157 
00158 
00159       }
00164       cdrMemoryStream get_m_put_data(void)
00165       {
00166           cdrMemoryStream cdr;
00167           m_buffer->read(cdr);
00168 
00169           return cdr;
00170       }
00171   private:
00172        RTC::CdrBufferBase* m_buffer;
00173        ::OpenRTM::CdrData  m_put_data;
00174   };
00175 
00176   class PublisherFlushTests
00177     : public CppUnit::TestFixture
00178   {
00179     CPPUNIT_TEST_SUITE(PublisherFlushTests);
00180                 
00181     CPPUNIT_TEST(test_setConsumer);
00182     CPPUNIT_TEST(test_activate_deactivate_isActive);
00183     CPPUNIT_TEST(test_write);
00184 //    CPPUNIT_TEST(test_update_immediacy);
00185                 
00186     CPPUNIT_TEST_SUITE_END();
00187                 
00188   private:
00189                 
00190   public:
00191     RTC::ConnectorListeners m_listeners;
00192         
00196     PublisherFlushTests()
00197     {
00198     }
00199                 
00203     ~PublisherFlushTests()
00204     {
00205     }
00206                 
00210     virtual void setUp()
00211     {
00212     }
00213                 
00217     virtual void tearDown()
00218     { 
00219 //      sleep(1);
00220     }
00221                 
00226     void test_setConsumer(void)
00227     {
00228         RTC::InPortCorbaCdrConsumer *consumer0 
00229                                     = new RTC::InPortCorbaCdrConsumer();
00230         RTC::InPortCorbaCdrConsumer *consumer1 
00231                                      = new RTC::InPortCorbaCdrConsumer();
00232         RTC::PublisherFlush publisher;
00233 
00234         //NULLを渡した場合INVALID_ARGSとなることを確認する。
00235         CPPUNIT_ASSERT_EQUAL(RTC::PublisherFlush::INVALID_ARGS, 
00236                              publisher.setConsumer(NULL));
00237 
00238         //
00239         CPPUNIT_ASSERT_EQUAL(RTC::PublisherFlush::PORT_OK, 
00240                              publisher.setConsumer(consumer0));
00241 
00242         //
00243         CPPUNIT_ASSERT_EQUAL(RTC::PublisherFlush::PORT_OK, 
00244                              publisher.setConsumer(consumer1));
00245 
00246         delete consumer0;
00247         delete consumer1;
00248  
00249     }
00254     void test_activate_deactivate_isActive(void)
00255     {
00256         RTC::InPortCorbaCdrConsumer *consumer 
00257                                     = new RTC::InPortCorbaCdrConsumer();
00258         RTC::PublisherFlush publisher;
00259         publisher.setConsumer(consumer);
00260 
00261         CPPUNIT_ASSERT_EQUAL(false, 
00262                              publisher.isActive());
00263         
00264         CPPUNIT_ASSERT_EQUAL(RTC::PublisherFlush::PORT_OK, 
00265                              publisher.activate());
00266 
00267         CPPUNIT_ASSERT_EQUAL(true, 
00268                              publisher.isActive());
00269         
00270         //既に activate されている場合は 
00271         //activateすると
00272         //PORT_OK を返すことを確認する。
00273         CPPUNIT_ASSERT_EQUAL(RTC::PublisherFlush::PORT_OK, 
00274                              publisher.activate());
00275 
00276         CPPUNIT_ASSERT_EQUAL(true, 
00277                              publisher.isActive());
00278         
00279         CPPUNIT_ASSERT_EQUAL(RTC::PublisherFlush::PORT_OK, 
00280                              publisher.deactivate());
00281 
00282         CPPUNIT_ASSERT_EQUAL(false, 
00283                              publisher.isActive());
00284         
00285         //activate されていない状態で、
00286         //deactivateすると
00287         //PORT_OK を返すことを確認する。
00288         CPPUNIT_ASSERT_EQUAL(RTC::PublisherFlush::PORT_OK, 
00289                              publisher.deactivate());
00290         
00291         CPPUNIT_ASSERT_EQUAL(false, 
00292                              publisher.isActive());
00293         
00294         delete consumer;
00295     }
00300     void test_write(void)
00301     {
00302         InPortCorbaCdrConsumerMock *consumer 
00303                                     = new InPortCorbaCdrConsumerMock();
00304 
00305         RTC::PublisherFlush publisher;
00306 
00307         //ConnectorInfo
00308         coil::Properties prop;
00309         coil::vstring ports;
00310         RTC::ConnectorInfo info("name", "id", ports, prop);
00311 
00312         //ConnectorListeners
00313         m_listeners.connectorData_[RTC::ON_BUFFER_WRITE].addListener(
00314                                    new DataListener("ON_BUFFER_WRITE"), true);
00315         m_listeners.connectorData_[RTC::ON_BUFFER_FULL].addListener(
00316                                    new DataListener("ON_BUFFER_FULL"), true);
00317         m_listeners.connectorData_[RTC::ON_BUFFER_WRITE_TIMEOUT].addListener(
00318                                    new DataListener("ON_BUFFER_WRITE_TIMEOUT"), true);
00319         m_listeners.connectorData_[RTC::ON_BUFFER_OVERWRITE].addListener(
00320                                    new DataListener("ON_BUFFER_OVERWRITE"), true);
00321         m_listeners.connectorData_[RTC::ON_BUFFER_READ].addListener(
00322                                    new DataListener("ON_BUFFER_READ"), true);
00323         m_listeners.connectorData_[RTC::ON_SEND].addListener(
00324                                    new DataListener("ON_SEND"), true);
00325         m_listeners.connectorData_[RTC::ON_RECEIVED].addListener(
00326                                    new DataListener("ON_RECEIVED"), true);
00327         m_listeners.connectorData_[RTC::ON_RECEIVER_FULL].addListener(
00328                                    new DataListener("ON_RECEIVER_FULL"), true);
00329         m_listeners.connectorData_[RTC::ON_RECEIVER_TIMEOUT].addListener(
00330                                    new DataListener("ON_RECEIVER_TIMEOUT"), true);
00331         m_listeners.connectorData_[RTC::ON_RECEIVER_ERROR].addListener(
00332                                    new DataListener("ON_RECEIVER_ERROR"), true);
00333 
00334         // setListener
00335         CPPUNIT_ASSERT_EQUAL(RTC::DataPortStatus::INVALID_ARGS, 
00336                              publisher.setListener(info, 0));
00337         CPPUNIT_ASSERT_EQUAL(RTC::DataPortStatus::PORT_OK, 
00338                              publisher.setListener(info, &m_listeners));
00339 
00340         //Consumerを設定しない状態でwriteをコール
00341         {
00342         cdrMemoryStream cdr;
00343         RTC::TimedLong td;
00344         td.data = 12345;
00345         td >>= cdr;
00346         CPPUNIT_ASSERT_EQUAL(RTC::PublisherFlush::PRECONDITION_NOT_MET, 
00347                              publisher.write(cdr,0,0));
00348         }
00349 
00350         publisher.setConsumer(consumer);
00351 
00352         //activateする前にwriteをコール
00353         {
00354         cdrMemoryStream cdr;
00355         RTC::TimedLong td;
00356         td.data = 123;
00357         td >>= cdr;
00358 
00359         CPPUNIT_ASSERT_EQUAL(RTC::PublisherFlush::PORT_OK,
00360                              publisher.write(cdr,0,0));
00361         }
00362         publisher.activate();
00363 
00364         CORBA::Long testdata[8] = { 123,279,3106,31611,125563,
00365                                     125563,846459,2071690107 };
00366 
00367         for(int icc(0);icc<7;++icc)
00368         {
00369             cdrMemoryStream cdr;
00370             RTC::TimedLong td;
00371             td.data = testdata[icc+1];
00372             td >>= cdr;
00373 
00374             CPPUNIT_ASSERT_EQUAL(RTC::PublisherFlush::PORT_OK,
00375                                  publisher.write(cdr,0,0));
00376 
00377         }
00378 
00379         //full の状態でコール(full)
00380         {
00381         cdrMemoryStream cdr;
00382         RTC::TimedLong td;
00383         td.data = 12345;
00384         td >>= cdr;
00385         CPPUNIT_ASSERT_EQUAL(RTC::PublisherFlush::SEND_FULL,
00386                                  publisher.write(cdr,0,0));
00387         }
00388 
00389         for(int icc(0);icc<8;++icc)
00390         {
00391             cdrMemoryStream data;
00392             data = consumer->get_m_put_data();
00393             CORBA::ULong inlen = data.bufSize();
00394             CPPUNIT_ASSERT_EQUAL(12,(int)inlen);
00395 
00396             RTC::TimedLong rtd;
00397             rtd <<= data;
00398 
00399             CPPUNIT_ASSERT_EQUAL((long)testdata[icc], (long)rtd.data);
00400         }
00401 
00402         //deactivateしてからwriteをコール
00403         publisher.deactivate();
00404         {
00405         cdrMemoryStream cdr;
00406         RTC::TimedLong td;
00407         td.data = 12345;
00408         td >>= cdr;
00409         CPPUNIT_ASSERT_EQUAL(RTC::PublisherFlush::PORT_OK,
00410                              publisher.write(cdr,0,0));
00411         }
00412 
00413         //Listener callback check
00414         cdrMemoryStream cdr;
00415         RTC::TimedLong td;
00416         td.data = 777;
00417         td >>= cdr;
00418         m_OnCheck = 1;  // PORT_OK:onReceived()
00419         publisher.write(cdr,0,0);
00420         m_OnCheck = 2;  // PORT_ERROR:onReceiverError()
00421         publisher.write(cdr,0,0);
00422         m_OnCheck = 3;  // SEND_FULL:onReceiverFull()
00423         publisher.write(cdr,0,0);
00424         m_OnCheck = 4;  // SEND_TIMEOUT:onReceiverTimeout()
00425         publisher.write(cdr,0,0);
00426         m_OnCheck = 5;  // UNKNOWN_ERROR:onReceiverError()
00427         publisher.write(cdr,0,0);
00428 
00429         delete consumer;
00430     }
00431 
00440 /*
00441     void test_update_immediacy()
00442     {
00443       long sleepTick = 100000; // 0.1 [sec]
00444                         
00445       MockConsumer* consumer = new MockConsumer(sleepTick);
00446       coil::Properties prop;
00447       RTC::PublisherFlush publisher(consumer, prop);
00448                         
00449       for (int i = 0; i < 10; i++)
00450         {
00451           consumer->setDelayStartTime();
00452           publisher.update();
00453           consumer->recordReturnTick();
00454         }
00455                         
00456       long permissibleDelayTick = 100000; // 0.1 [sec]
00457       const std::vector<long>& delayTicks = consumer->getDelayTicks();
00458       for (std::vector<long>::size_type i = 0; i < delayTicks.size(); i++)
00459         {
00460           CPPUNIT_ASSERT(delayTicks[i] < permissibleDelayTick);
00461         }
00462                         
00463       long permissibleReturnTick = 100000; // 0.1 [sec]
00464       const std::vector<long>& returnTicks = consumer->getReturnTicks();
00465       for (std::vector<long>::size_type i = 0; i < returnTicks.size(); i++)
00466         {
00467           CPPUNIT_ASSERT(returnTicks[i] < permissibleReturnTick);
00468         }
00469     }
00470 */
00471                 
00472   };
00473 }; // namespace PublisherFlush
00474 
00475 /*
00476  * Register test suite
00477  */
00478 CPPUNIT_TEST_SUITE_REGISTRATION(PublisherFlush::PublisherFlushTests);
00479 
00480 #ifdef LOCAL_MAIN
00481 int main(int argc, char* argv[])
00482 {
00483 
00484   FORMAT format = TEXT_OUT;
00485   int target = 0;
00486   std::string xsl;
00487   std::string ns;
00488   std::string fname;
00489   std::ofstream ofs;
00490 
00491   int i(1);
00492   while (i < argc)
00493     {
00494       std::string arg(argv[i]);
00495       std::string next_arg;
00496       if (i + 1 < argc) next_arg = argv[i + 1];
00497       else              next_arg = "";
00498 
00499       if (arg == "--text") { format = TEXT_OUT; break; }
00500       if (arg == "--xml")
00501         {
00502           if (next_arg == "")
00503             {
00504               fname = argv[0];
00505               fname += ".xml";
00506             }
00507           else
00508             {
00509               fname = next_arg;
00510             }
00511           format = XML_OUT;
00512           ofs.open(fname.c_str());
00513         }
00514       if ( arg == "--compiler"  ) { format = COMPILER_OUT; break; }
00515       if ( arg == "--cerr"      ) { target = 1; break; }
00516       if ( arg == "--xsl"       )
00517         {
00518           if (next_arg == "") xsl = "default.xsl"; 
00519           else                xsl = next_arg;
00520         }
00521       if ( arg == "--namespace" )
00522         {
00523           if (next_arg == "")
00524             {
00525               std::cerr << "no namespace specified" << std::endl;
00526               exit(1); 
00527             }
00528           else
00529             {
00530               xsl = next_arg;
00531             }
00532         }
00533       ++i;
00534     }
00535   CppUnit::TextUi::TestRunner runner;
00536   if ( ns.empty() )
00537     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00538   else
00539     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
00540   CppUnit::Outputter* outputter = 0;
00541   std::ostream* stream = target ? &std::cerr : &std::cout;
00542   switch ( format )
00543     {
00544     case TEXT_OUT :
00545       outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
00546       break;
00547     case XML_OUT :
00548       std::cout << "XML_OUT" << std::endl;
00549       outputter = new CppUnit::XmlOutputter(&runner.result(),
00550                                             ofs, "shift_jis");
00551       static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
00552       break;
00553     case COMPILER_OUT :
00554       outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
00555       break;
00556     }
00557   runner.setOutputter(outputter);
00558   runner.run();
00559   return 0; // runner.run() ? 0 : 1;
00560 }
00561 #endif // MAIN
00562 #endif // PublisherFlush_cpp


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sun Mar 26 2017 03:37:17