service_port_test.cpp
Go to the documentation of this file.
00001 
00002 #include <TaskContext.hpp>
00003 #include <InputPort.hpp>
00004 #include <OutputPort.hpp>
00005 #include <Service.hpp>
00006 #include <ServiceRequester.hpp>
00007 
00008 #include "unit.hpp"
00009 #include "operations_fixture.hpp"
00010 
00011 struct ServicePortFixture {};
00012 
00013 // Registers the suite into the 'registry'
00014 BOOST_FIXTURE_TEST_SUITE(  ServicePortTestSuite,  ServicePortFixture )
00015 
00016 
00020 class TestService : public Service {
00021 public:
00022     InputPort<int> ip;
00023     InputPort<int> ip2;
00024     OutputPort<int> op;
00025     TestService(TaskContext* owner = 0) : Service("portservice", owner)
00026     {
00027         addPort("ip",ip).doc("ip");
00028         addPort("ip",ip2).doc("ip"); // overrides ip
00029         addPort("op",op).doc("op");
00030     }
00031 };
00032 
00033 class TestEventService : public Service {
00034 public:
00035     InputPort<int> ip;
00036     InputPort<int> ip2;
00037     OutputPort<int> op;
00038     TestEventService(TaskContext* owner = 0) : Service("portservice", owner)
00039     {
00040         addEventPort("ip",ip).doc("ip");
00041         addEventPort("ip",ip2).doc("ip"); // overrides ip
00042         addPort("op",op).doc("op");
00043     }
00044 };
00045 
00046 BOOST_AUTO_TEST_CASE(testAddPort)
00047 {
00048     TestService* ts = new TestService();
00049     Service::shared_ptr s( ts );
00050     TaskContext tc("tc");
00051 
00052     tc.provides()->addService( s );
00053 
00054     // check that last port is the real thing:
00055     BOOST_CHECK( tc.provides("portservice")->getPort("ip") == &ts->ip2 );
00056 
00057     BOOST_CHECK( tc.provides("portservice")->getPort("op") == &ts->op );
00058 }
00059 
00060 BOOST_AUTO_TEST_CASE(testAddPortWithOwner)
00061 {
00062     TaskContext tc("tc");
00063     TestService* ts = new TestService( &tc );
00064     Service::shared_ptr s( ts );
00065 
00066     tc.provides()->addService( s );
00067 
00068     // check that last port is the real thing:
00069     BOOST_CHECK( tc.provides("portservice")->getPort("ip") == &ts->ip2 );
00070 
00071     BOOST_CHECK( tc.provides("portservice")->getPort("op") == &ts->op );
00072 }
00073 
00074 
00075 BOOST_AUTO_TEST_CASE(testAddEventPort)
00076 {
00077     TestEventService* ts = new TestEventService();
00078     Service::shared_ptr s( ts );
00079     TaskContext tc("tc");
00080 
00081     tc.provides()->addService( s );
00082 
00083     // check that last port is the real thing:
00084     BOOST_CHECK( tc.provides("portservice")->getPort("ip") == &ts->ip2 );
00085 
00086     BOOST_CHECK( tc.provides("portservice")->getPort("op") == &ts->op );
00087 }
00088 
00089 BOOST_AUTO_TEST_CASE(testAddEventPortWithOwner)
00090 {
00091     TaskContext tc("tc");
00092     TestEventService* ts = new TestEventService(&tc);
00093     Service::shared_ptr s( ts );
00094 
00095     tc.provides()->addService( s );
00096 
00097     // check that last port is the real thing:
00098     BOOST_CHECK( tc.provides("portservice")->getPort("ip") == &ts->ip2 );
00099 
00100     BOOST_CHECK( tc.provides("portservice")->getPort("op") == &ts->op );
00101 }
00102 
00103 
00104 #ifndef ORO_DISABLE_PORT_DATA_SCRIPTING
00105 
00106 BOOST_AUTO_TEST_CASE(testUsePort)
00107 {
00108     TestService* ts = new TestService();
00109     // should work because TestService already adds itself to children... but this memleaks
00110     Service::shared_ptr s1 = ts->provides();
00111     // should work as well because TestService inherits from enable_shared_from_raw :
00112     Service::shared_ptr s( ts );
00113     TaskContext tc("tc");
00114 
00115     tc.provides()->addService( s );
00116 
00117     ts->ip2.connectTo( &ts->op );
00118 
00119     // use operation interface of port:
00120     BOOST_REQUIRE( tc.provides()->hasService("portservice") );
00121     BOOST_REQUIRE( tc.provides("portservice")->hasService("op") );
00122     BOOST_REQUIRE( tc.provides("portservice")->provides("op")->hasOperation("write") );
00123 
00124     OperationCaller<void(int const&)> write = tc.provides("portservice")->provides("op")->getOperation("write");
00125     BOOST_CHECK( write.ready() );
00126     write( 3 );
00127 
00128     int result;
00129     OperationCaller<FlowStatus(int&)> read = tc.provides("portservice")->provides("ip")->getOperation("read");
00130     BOOST_CHECK( read.ready() );
00131     FlowStatus fs = read( result  );
00132     BOOST_CHECK_EQUAL( result, 3);
00133     BOOST_CHECK_EQUAL( fs, NewData );
00134 }
00135 
00136 BOOST_AUTO_TEST_CASE(testUsePortWithOwner)
00137 {
00138     TaskContext tc("tc");
00139     TestService* ts = new TestService(&tc);
00140     Service::shared_ptr s( ts );
00141 
00142     tc.provides()->addService( s );
00143 
00144     ts->ip2.connectTo( &ts->op );
00145 
00146     // use operation interface of port:
00147     OperationCaller<void(int const&)> write = tc.provides("portservice")->provides("op")->getOperation("write");
00148     BOOST_CHECK( write.ready() );
00149     write( 3 );
00150 
00151     int result;
00152     OperationCaller<FlowStatus(int&)> read = tc.provides("portservice")->provides("ip")->getOperation("read");
00153     BOOST_CHECK( read.ready() );
00154     FlowStatus fs = read( result  );
00155     BOOST_CHECK_EQUAL( result, 3);
00156     BOOST_CHECK_EQUAL( fs, NewData );
00157 }
00158 
00159 #endif
00160 
00161 BOOST_AUTO_TEST_SUITE_END()


rtt
Author(s): RTT Developers
autogenerated on Wed Aug 26 2015 16:15:53