dispatch_test.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Mon Jan 10 15:59:51 CET 2005 dispatch_test.cpp
3 
4  dispatch_test.cpp - description
5  -------------------
6  begin : Mon January 10 2005
7  copyright : (C) 2005 Peter Soetens
8  email : peter.soetens@mech.kuleuven.ac.be
9 
10  ***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 
19 #include "unit.hpp"
20 
21 #include "dispatch_test.hpp"
22 
23 #include <iostream>
24 #include <sstream>
27 #include <OperationCaller.hpp>
28 #include <OperationCaller.hpp>
29 #include <Service.hpp>
30 
31 using namespace std;
32 
34  : gtc("root"),
35  mtc("space"),
36  ltc("subspace")
37 {
38  setUp();
39 }
40 
41 
42 void
44 {
45  ltc.clear();
46  mtc.clear();
47  gtc.clear();
48  // ltc has a test object
49  ltc.addService( this->createObject("test", ltc.engine()) );
50  // mtc has two methods.
51  mtc.addService( this->createObject("test", mtc.engine()) );
52 
53  gtc.addPeer( &mtc );
54  mtc.connectPeers( &ltc );
55 
57  mtc.setActivity( new SimulationActivity(0.05) );
58  ltc.setActivity( new SimulationActivity(0.01) );
59 }
60 
61 
62 void
64 {
65  gtc.removePeer( "space" );
66  ltc.disconnectPeers( "subspace" );
67 }
68 
69 
70 bool DispatchTest::assertBool( bool b) {
71  return b;
72 }
73 bool DispatchTest::assertMsg( bool b, const std::string& msg) {
74  if ( b == false )
75  cout << "Asserted :" << b << " with :" << msg << endl;
76  return b;
77 }
78 
79 
80 Service* DispatchTest::createObject(string a, CommandProcessor* cp)
81 {
82  Service* dat = new Service(a);
83  dat->addOperation("assert", &DispatchTest::assertBool, this).doc("Assert").arg("bool", "");
84  dat->addOperation("assertMsg", &DispatchTest::assertMsg, this).doc("Assert message").arg("bool", "").arg("text", "text");
85  dat->addOperation("isTrue", &DispatchTest::assertBool, this).doc("Identity function").arg("bool", "");
86  dat->addOperation("instantDone", &DispatchTest::true_genCom, this).doc("returns immediately");
87 addOperation("instantDoneDone", &DispatchTest::true_gen, this).doc("Returns true when instantDone is done.");
88  dat->addOperation("neverDone", &DispatchTest::true_genCom, this).doc("returns never");
89 addOperation("neverDoneDone", &DispatchTest::false_gen, this).doc("Returns true when neverDone is done.");
90  dat->addCommand( command( "instantNotDone", &DispatchTest::true_genCom,
91  &DispatchTest::true_gen, this, cp, false),
92  "returns never");
93  dat->addOperation("instantFail", &DispatchTest::false_genCom, this).doc("fails immediately");
94 addOperation("instantFailDone", &DispatchTest::true_gen, this).doc("Returns true when instantFail is done.");
95  dat->addOperation("totalFail", &DispatchTest::false_genCom, this).doc("fails in command and condition");
96 addOperation("totalFailDone", &DispatchTest::false_gen, this).doc("Returns true when totalFail is done.");
97  return dat;
98 }
99 
100 // Registers the fixture into the 'registry'
101 BOOST_FIXTURE_TEST_SUITE( DispatchTestSuite, DispatchTest )
102 
104 {
105  // this is a global program requesting a method on a local
106  // task/processor (ie assert) and a command (instantDone)
107  string prog = string("program x { do space.subspace.test.assertMsg(true,\"tpdtrue\") \n")
108  + " if space.subspace.test.assert(true) then \n"
109  + " do nothing\n"
110  + " do space.subspace.test.instantDone() \n"
111 // + " do space.assertMsg(true,\"tpdfail\")\n"
112 // + " do this.space.assertMsg(true,\"donotreach\")\n"
113  + "}";
114 
115  this->doDispatch( prog, &gtc );
116 
117  BOOST_CHECK( gtc.engine()->programs()->getProgramStatus("x") != ProgramInterface::Status::error );
118  BOOST_CHECK( gtc.engine()->programs()->getProgramStatus("x") != ProgramInterface::Status::running );
119  BOOST_CHECK( gtc.engine()->programs()->getProgramStatus("x") == ProgramInterface::Status::stopped );
120 
121  this->finishDispatch( &gtc, "x");
122 }
123 
125 {
126  // this is a global program requesting a command on a local
127  // task/processor (ie instantFail).
128  string prog = string("program x { do space.subspace.test.instantFail() \n")
129  + "}";
130 
131  this->doDispatch( prog, &gtc );
132 
133  BOOST_CHECK( gtc.engine()->programs()->getProgramStatus("x") == ProgramInterface::Status::error );
134 
135  this->finishDispatch( &gtc, "x");
136 }
138 {
139  // see if checking a remote condition works
140  // also tests peerparser in expressions
141  string prog = string("program x { if ( space.subspace.test.assert(true) ) then \n")
142  + "do space.subspace.test.instantDone() \n"
143  + "else \n"
144  + "do space.subspace.test.instantFail() \n"
145  + "if ( space.subspace.test.assert(false) ) then \n"
146  + "do space.subspace.test.instantFail() \n"
147  + "else \n"
148  + "do space.subspace.test.instantDone() \n"
149  + " }";
150  this->doDispatch( prog, &gtc );
151 
152  stringstream msg;
153  msg << "Status was not 'stopped', but "+gtc.engine()->programs()->getProgramStatusStr("x");
154  msg << " on line " << gtc.engine()->programs()->getProgram("x")->getLineNumber();
155  BOOST_CHECK_MESSAGE(gtc.engine()->programs()->getProgramStatus("x") == ProgramInterface::Status::stopped,
156  msg.str());
157 
158  this->finishDispatch( &gtc, "x");
159 }
160 
162 {
163  // see if checking a remote condition works
164  string prog = string("program x { do space.subspace.test.assert(true)\n")
165  + "and space.subspace.test.assert(true) \n"
166  + "and space.subspace.test.assert(true) \n"
167  + "do space.subspace.test.instantDone() \n"
168  + "and space.subspace.test.instantDone() \n"
169  + "and space.subspace.test.instantDone() \n"
170  + " }";
171  this->doDispatch( prog, &gtc );
172 
173  stringstream msg;
174  msg << "Status was not 'stopped', but "+gtc.engine()->programs()->getProgramStatusStr("x");
175  msg << " on line " << gtc.engine()->programs()->getProgram("x")->getLineNumber();
176  BOOST_CHECK_MESSAGE(gtc.engine()->programs()->getProgramStatus("x") == ProgramInterface::Status::stopped
177  , msg.str());
178 
179  this->finishDispatch( &gtc, "x");
180 }
181 
183 {
184  // see if checking a remote condition works
185  string prog = string("program x { try space.subspace.test.assert(false)\n")
186  + "try space.subspace.test.assert(true) \n"
187  + "and space.subspace.test.assert(false) \n"
188  + "and space.subspace.test.assert(true) \n"
189  + "try space.subspace.test.instantFail()\n"
190  + "try space.subspace.test.instantDone() \n"
191  + "and space.subspace.test.instantFail() \n"
192  + "and space.subspace.test.instantDone() \n"
193  + " }";
194  this->doDispatch( prog, &gtc );
195 
196  BOOST_CHECK( gtc.engine()->programs()->getProgramStatus("x") != ProgramInterface::Status::error );
197  stringstream msg;
198  msg << "Status was not 'stopped', but "+gtc.engine()->programs()->getProgramStatusStr("x");
199  msg << " on line " << gtc.engine()->programs()->getProgram("x")->getLineNumber();
200  BOOST_CHECK_MESSAGE(gtc.engine()->programs()->getProgramStatus("x") == ProgramInterface::Status::stopped
201  , msg.str());
202 
203  this->finishDispatch( &gtc, "x");
204 }
205 
207 {
208  // see if checking a remote condition works
209  string prog = string("program x { do space.subspace.test.instantDone()\n")
210  + "until { \n"
211  + " if time > 10 ms then continue \n" // test in simulation takes far less than 1 second
212  + "} \n"
213  + "do space.subspace.test.instantDone()\n"
214  + "until { \n"
215  + " if done then continue \n"
216  + "} \n"
217  + " }";
218  this->doDispatch( prog, &gtc );
219 
220  stringstream msg;
221  msg << "Status was not 'stopped', but "+gtc.engine()->programs()->getProgramStatusStr("x");
222  msg << " on line " << gtc.engine()->programs()->getProgram("x")->getLineNumber();
223  BOOST_CHECK_MESSAGE(gtc.engine()->programs()->getProgramStatus("x") == ProgramInterface::Status::stopped,
224  msg.str());
225 
226  this->finishDispatch( &gtc, "x");
227 }
228 
230 {
231  // see if checking a remote condition works
232  string prog = string("program x { do space.subspace.test.instantFail()\n")
233  + "until { \n"
234  + " if done then continue \n" // program should go into error
235  + "} \n"
236  + " }";
237  this->doDispatch( prog, &gtc );
238 
239  BOOST_CHECK( gtc.engine()->programs()->getProgramStatus("x") == ProgramInterface::Status::error );
240 
241  this->finishDispatch( &gtc, "x");
242 }
243 
245 {
246  // XXX not a valid test. send not present in Orocos, this looks like 'try'
247  // a program which must not fail, even if the command failes.
248  string prog = string("program x { ")
249  +" do space.subspace.test.instantDone()\n"
250  +" do space.subspace.test.instantDone()\n"
251  +" do space.subspace.test.instantDone()\n"
252  +" do space.subspace.test.instantDone()\n"
253  +" }";
254  this->doDispatch( prog, &gtc );
255 
256  BOOST_CHECK( gtc.engine()->programs()->getProgramStatus("x") != ProgramInterface::Status::error );
257  BOOST_CHECK( gtc.engine()->programs()->getProgramStatus("x") == ProgramInterface::Status::stopped );
258 
259  this->finishDispatch( &gtc, "x" );
260 }
261 
263 
264 void DispatchTest::doDispatch( const std::string& prog, TaskContext* tc )
265 {
266  Parser::ParsedPrograms pg_list;
267  try {
268  pg_list = parser.parseProgram( prog, tc );
269  }
270  catch( const file_parse_exception& exc )
271  {
272  BOOST_REQUIRE_MESSAGE( false , exc.what());
273  }
274  if ( pg_list.empty() )
275  {
276  BOOST_REQUIRE( false );
277  }
278  BOOST_REQUIRE( tc->engine()->programs()->loadProgram( *pg_list.begin() ) );
279  BOOST_CHECK(ltc.start());
280  BOOST_CHECK(mtc.start());
281  BOOST_CHECK(gtc.start());
282  BOOST_REQUIRE( tc->engine()->programs()->getProgram( (*pg_list.begin())->getName() )->start() );
283 
284  SimulationThread::Instance()->stop();
285  SimulationThread::Instance()->run(1000);
286 }
287 
288 void DispatchTest::finishDispatch(TaskContext* tc, std::string prog_name)
289 {
290  BOOST_CHECK(gtc.stop());
291  BOOST_CHECK(mtc.stop());
292  BOOST_CHECK(ltc.stop());
293  tc->engine()->programs()->getProgram( prog_name )->stop();
294  BOOST_CHECK( tc->engine()->programs()->unloadProgram( prog_name ) );
295 
296 }
297 
TaskContext mtc
void doDispatch(const std::string &prog, TaskContext *)
const std::string what() const
#define BOOST_FIXTURE_TEST_SUITE(suite_name, F)
void testDispatchAnd()
bool assertMsg(bool, const std::string &msg)
bool assertBool(bool)
virtual bool stop()
#define BOOST_AUTO_TEST_SUITE_END()
Definition: mystd.hpp:163
bool false_gen() const
Operation< Signature > & addOperation(Operation< Signature > &op)
Definition: Service.hpp:341
bool true_gen() const
virtual bool connectPeers(TaskContext *peer)
virtual void removePeer(const std::string &name)
Service * createObject(std::string a, CommandProcessor *cp)
void testDispatchMany()
bool true_genCom()
TaskContext gtc
bool setActivity(base::ActivityInterface *new_act)
printstream cout
Definition: rtstreams.cpp:45
void testDispatchTry()
std::vector< ProgramInterfacePtr > ParsedPrograms
Definition: Parser.hpp:101
virtual void disconnectPeers(const std::string &name)
basic_ostreams & endl(basic_ostreams &s)
Definition: rtstreams.cpp:110
void finishDispatch(TaskContext *, std::string)
void testDispatchUntil()
BOOST_AUTO_TEST_CASE(testParseDispatch)
bool false_genCom()
ParsedPrograms parseProgram(const std::string &s, TaskContext *, const std::string &filename="stream")
Reads out the string, parses it, and returns a new ProgramGraph.
Definition: Parser.cpp:103
void testParseDispatch()
void testDispatchFailure()
A SimulationActivity is a PeriodicActivity which is used for simulation.
TaskContext ltc
void testDispatchCondition()
const ExecutionEngine * engine() const
Definition: TaskCore.hpp:306
virtual bool start()
virtual bool addPeer(TaskContext *peer, std::string alias="")
void testDispatchUntilFail()
virtual void clear()


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