service_port_test.cpp
Go to the documentation of this file.
1 
2 #include <TaskContext.hpp>
3 #include <InputPort.hpp>
4 #include <OutputPort.hpp>
5 #include <Service.hpp>
6 #include <ServiceRequester.hpp>
7 
8 #include "unit.hpp"
9 #include "operations_fixture.hpp"
10 
12 
13 // Registers the suite into the 'registry'
14 BOOST_FIXTURE_TEST_SUITE( ServicePortTestSuite, ServicePortFixture )
15 
16 
20 class TestService : public Service {
21 public:
25  TestService(TaskContext* owner = 0) : Service("portservice", owner)
26  {
27  addPort("ip",ip).doc("ip");
28  addPort("ip",ip2).doc("ip"); // overrides ip
29  addPort("op",op).doc("op");
30  }
31 };
32 
33 class TestEventService : public Service {
34 public:
38  TestEventService(TaskContext* owner = 0) : Service("portservice", owner)
39  {
40  addEventPort("ip",ip).doc("ip");
41  addEventPort("ip",ip2).doc("ip"); // overrides ip
42  addPort("op",op).doc("op");
43  }
44 };
45 
47 {
48  TestService* ts = new TestService();
49  Service::shared_ptr s( ts );
50  TaskContext tc("tc");
51 
52  tc.provides()->addService( s );
53 
54  // check that last port is the real thing:
55  BOOST_CHECK( tc.provides("portservice")->getPort("ip") == &ts->ip2 );
56 
57  BOOST_CHECK( tc.provides("portservice")->getPort("op") == &ts->op );
58 }
59 
60 BOOST_AUTO_TEST_CASE(testAddPortWithOwner)
61 {
62  TaskContext tc("tc");
63  TestService* ts = new TestService( &tc );
64  Service::shared_ptr s( ts );
65 
66  tc.provides()->addService( s );
67 
68  // check that last port is the real thing:
69  BOOST_CHECK( tc.provides("portservice")->getPort("ip") == &ts->ip2 );
70 
71  BOOST_CHECK( tc.provides("portservice")->getPort("op") == &ts->op );
72 }
73 
74 
75 BOOST_AUTO_TEST_CASE(testAddEventPort)
76 {
78  Service::shared_ptr s( ts );
79  TaskContext tc("tc");
80 
81  tc.provides()->addService( s );
82 
83  // check that last port is the real thing:
84  BOOST_CHECK( tc.provides("portservice")->getPort("ip") == &ts->ip2 );
85 
86  BOOST_CHECK( tc.provides("portservice")->getPort("op") == &ts->op );
87 }
88 
89 BOOST_AUTO_TEST_CASE(testAddEventPortWithOwner)
90 {
91  TaskContext tc("tc");
92  TestEventService* ts = new TestEventService(&tc);
93  Service::shared_ptr s( ts );
94 
95  tc.provides()->addService( s );
96 
97  // check that last port is the real thing:
98  BOOST_CHECK( tc.provides("portservice")->getPort("ip") == &ts->ip2 );
99 
100  BOOST_CHECK( tc.provides("portservice")->getPort("op") == &ts->op );
101 }
102 
103 
104 #ifndef ORO_DISABLE_PORT_DATA_SCRIPTING
105 
107 {
108  TestService* ts = new TestService();
109  // should work because TestService already adds itself to children... but this memleaks
110  Service::shared_ptr s1 = ts->provides();
111  // should work as well because TestService inherits from enable_shared_from_raw :
112  Service::shared_ptr s( ts );
113  TaskContext tc("tc");
114 
115  tc.provides()->addService( s );
116 
117  ts->ip2.connectTo( &ts->op );
118 
119  // use operation interface of port:
120  BOOST_REQUIRE( tc.provides()->hasService("portservice") );
121  BOOST_REQUIRE( tc.provides("portservice")->hasService("op") );
122  BOOST_REQUIRE( tc.provides("portservice")->provides("op")->hasOperation("write") );
123 
124  OperationCaller<WriteStatus(int const&)> write = tc.provides("portservice")->provides("op")->getOperation("write");
125  BOOST_CHECK( write.ready() );
126  BOOST_CHECK_EQUAL( write( 3 ), WriteSuccess );
127 
128  int result;
129  OperationCaller<FlowStatus(int&)> read = tc.provides("portservice")->provides("ip")->getOperation("read");
130  BOOST_CHECK( read.ready() );
131  FlowStatus fs = read( result );
132  BOOST_CHECK_EQUAL( result, 3);
133  BOOST_CHECK_EQUAL( fs, NewData );
134 }
135 
136 BOOST_AUTO_TEST_CASE(testUsePortWithOwner)
137 {
138  TaskContext tc("tc");
139  TestService* ts = new TestService(&tc);
140  Service::shared_ptr s( ts );
141 
142  tc.provides()->addService( s );
143 
144  ts->ip2.connectTo( &ts->op );
145 
146  // use operation interface of port:
147  OperationCaller<void(int const&)> write = tc.provides("portservice")->provides("op")->getOperation("write");
148  BOOST_CHECK( write.ready() );
149  write( 3 );
150 
151  int result;
152  OperationCaller<FlowStatus(int&)> read = tc.provides("portservice")->provides("ip")->getOperation("read");
153  BOOST_CHECK( read.ready() );
154  FlowStatus fs = read( result );
155  BOOST_CHECK_EQUAL( result, 3);
156  BOOST_CHECK_EQUAL( fs, NewData );
157 }
158 
159 #endif
160 
#define BOOST_FIXTURE_TEST_SUITE(suite_name, F)
Service::shared_ptr provides()
Definition: Service.cpp:113
Service::shared_ptr provides()
#define BOOST_AUTO_TEST_SUITE_END()
FlowStatus
Definition: FlowStatus.hpp:56
OutputPort< int > op
InputPort< int > ip
boost::shared_ptr< Service > shared_ptr
Definition: Service.hpp:101
OutputPort< int > op
TestService(TaskContext *owner=0)
BOOST_AUTO_TEST_CASE(testAddPort)
const std::string & doc() const
Definition: Service.hpp:144
virtual bool connectTo(PortInterface *other, ConnPolicy const &policy)
TestEventService(TaskContext *owner=0)
InputPort< int > ip2
InputPort< int > ip2
InputPort< int > ip


rtt
Author(s): RTT Developers
autogenerated on Tue Jun 25 2019 19:33:28