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