-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtaskthread_test.cpp
619 lines (510 loc) · 18.2 KB
/
taskthread_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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/***************************************************************************
tag: Peter Soetens Tue Apr 5 16:53:26 CEST 2005 taskthread_test.cpp
taskthread_test.cpp - description
-------------------
begin : Tue April 05 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 "taskthread_test.hpp"
#include <iostream>
#include <extras/Activities.hpp>
#include <extras/TimerThread.hpp>
#include <extras/SimulationThread.hpp>
#include <os/MainThread.hpp>
#include <Logger.hpp>
#include <rtt-config.h>
using namespace std;
using namespace RTT;
struct A {};
template<class T>
struct TestActivity
: public T
{
bool result, _dothrow;
bool init, stepped, fini;
bool wasrunning, wasactive;
ActivityInterface* owner;
TestActivity(int prio, double per, bool fail, bool dothrow = false)
:T(prio,per), _dothrow(dothrow)
{
this->reset(fail);
}
bool initialize() {
init = true;
return result;
}
void step() {
stepped = true;
wasrunning = this->isRunning();
wasactive = this->isActive();
#ifndef ORO_EMBEDDED
if ( _dothrow )
throw A();
#endif
}
void finalize() {
fini = true;
}
void reset(bool fail) {
result = fail;
init = false;
stepped = false;
fini = false;
wasrunning=false;
wasactive=false;
}
};
struct TestRunner
: public RunnableInterface
{
bool result;
bool init, stepped, fini;
bool looped, broke;
bool wasrunning, wasactive;
TestRunner(bool fail)
{
this->reset(fail);
}
bool initialize() {
init = true;
BOOST_CHECK(getActivity()->isActive());
BOOST_CHECK(!getActivity()->isRunning());
return result;
}
void step() {
stepped = true;
wasrunning=getActivity()->isRunning();
wasactive=getActivity()->isActive();
}
void loop() {
looped = true;
wasrunning=getActivity()->isRunning();
wasactive=getActivity()->isActive();
}
bool breakLoop() {
broke = true;
return true;
}
void finalize() {
fini = true;
BOOST_CHECK(getActivity()->isActive());
BOOST_CHECK(!getActivity()->isRunning());
}
void reset(bool fail) {
result = fail;
init = false;
stepped = false;
fini = false;
looped = false;
broke = false;
wasrunning = false;
wasactive = false;
}
};
void
ActivitiesThreadTest::setUp()
{
t_task_np = new TestActivity<PeriodicActivity>(3, 0.01, true ); // will be stopped if np_bad throws
t_task_np_bad = new TestActivity<PeriodicActivity>(3, 0.01, true, true ); // throws
t_task_p = new TestActivity<PeriodicActivity>(3, 0.032, true ); // does not throw
t_task_a = new TestActivity<Activity>(3, 0.01, true, true ); // throws
}
void
ActivitiesThreadTest::tearDown()
{
delete t_task_np;
delete t_task_np_bad;
delete t_task_p;
delete t_task_a;
}
// Registers the fixture into the 'registry'
BOOST_FIXTURE_TEST_SUITE( ActivitiesThreadTestSuite, ActivitiesThreadTest )
BOOST_AUTO_TEST_CASE(testPeriodic )
{
// Test periodic task sequencing...
PeriodicActivity mtask( 15, 0.01 );
BOOST_CHECK( mtask.isActive() == false );
BOOST_CHECK( mtask.isRunning() == false );
BOOST_CHECK( mtask.thread()->isRunning() == false );
BOOST_CHECK_EQUAL( 0.01, mtask.thread()->getPeriod() );
// Adapt priority levels to OS.
int bprio = 15, rtsched = ORO_SCHED_RT;
os::CheckPriority( rtsched, bprio );
BOOST_CHECK_EQUAL( bprio, mtask.thread()->getPriority() );
BOOST_CHECK_EQUAL( rtsched, mtask.thread()->getScheduler() );
PeriodicActivity m2task( 15, 0.01 );
BOOST_CHECK( mtask.thread() == m2task.thread() );
// starting...
BOOST_CHECK( mtask.start() == true );
BOOST_CHECK( mtask.isRunning() == true );
BOOST_CHECK( mtask.thread()->isRunning() == true );
BOOST_CHECK( m2task.isRunning() == false );
BOOST_CHECK( m2task.start() == true );
BOOST_CHECK( m2task.isRunning() == true );
usleep(100000);
// stopping...
BOOST_CHECK( mtask.stop() == true );
BOOST_CHECK( mtask.isRunning() == false );
BOOST_CHECK( m2task.isRunning() == true );
BOOST_CHECK( m2task.stop() == true );
BOOST_CHECK( m2task.isRunning() == false );
// Different Scheduler (don't test if invalid priorities)
bprio = 15;
rtsched = ORO_SCHED_RT;
if ( os::CheckPriority( rtsched, bprio ) ) {
PeriodicActivity m3task(ORO_SCHED_OTHER, 15, 0.01);
bprio = 15;
rtsched = ORO_SCHED_OTHER;
if ( os::CheckPriority( rtsched, bprio ) ) {
BOOST_CHECK( mtask.thread() != m3task.thread() );
BOOST_CHECK_EQUAL( ORO_SCHED_OTHER, m3task.thread()->getScheduler() );
}
}
// Starting thread if thread not running
BOOST_CHECK( mtask.thread()->stop() );
BOOST_CHECK( mtask.thread()->isRunning() == false );
BOOST_CHECK( mtask.start() );
BOOST_CHECK( mtask.isRunning() == true );
BOOST_CHECK( mtask.thread()->isRunning() == true);
}
BOOST_AUTO_TEST_CASE( testNonPeriodic )
{
// Test periodic task sequencing...
Activity mtask( 15 );
usleep(100000);
// Adapt priority levels to OS.
int bprio = 15, rtsched = ORO_SCHED_RT;
os::CheckPriority( rtsched, bprio );
BOOST_CHECK( mtask.isActive() == false );
BOOST_CHECK( mtask.isRunning() == false );
BOOST_CHECK( mtask.thread()->isRunning() == false );
BOOST_CHECK_EQUAL( bprio, mtask.thread()->getPriority() );
BOOST_CHECK_EQUAL( rtsched, mtask.thread()->getScheduler() );
Activity m2task( 15 );
BOOST_CHECK( mtask.thread() != m2task.thread() );
// starting...
BOOST_CHECK( mtask.start() == true );
BOOST_CHECK( mtask.isActive() == true );
BOOST_CHECK( m2task.isActive() == false );
BOOST_CHECK( m2task.start() == true );
BOOST_CHECK( m2task.isActive() == true );
usleep(100000);
// stopping...
BOOST_CHECK( mtask.stop() == true );
BOOST_CHECK( mtask.isActive() == false );
BOOST_CHECK( m2task.isActive() == true );
BOOST_CHECK( m2task.stop() == true );
BOOST_CHECK( m2task.isActive() == false );
// Different Scheduler
bprio = 15;
rtsched = ORO_SCHED_OTHER;
if ( os::CheckPriority( rtsched, bprio ) ) {
Activity m3task(ORO_SCHED_OTHER, 15, 0.0);
BOOST_CHECK( mtask.thread() != m3task.thread() );
BOOST_CHECK_EQUAL( ORO_SCHED_OTHER, m3task.thread()->getScheduler() );
}
}
BOOST_AUTO_TEST_CASE( testSlave )
{
// Test slave activities
TestRunner r(true);
// without master, NP.
SlaveActivity mtask(&r);
BOOST_CHECK( mtask.isActive() == false );
BOOST_CHECK( mtask.isRunning() == false );
BOOST_CHECK( mtask.isPeriodic() == false );
BOOST_CHECK( mtask.getPeriod() == 0.0 );
BOOST_CHECK( mtask.execute() == false );
BOOST_CHECK( mtask.thread() == os::MainThread::Instance() );
// starting...
BOOST_CHECK( mtask.start() == true );
BOOST_CHECK( r.init == true );
BOOST_CHECK( mtask.isActive() == true );
BOOST_CHECK( mtask.isRunning() == false );
BOOST_CHECK( mtask.start() == false );
// calls loop()
BOOST_CHECK( mtask.execute() );
BOOST_CHECK( r.looped == true );
BOOST_CHECK( mtask.execute() );
BOOST_CHECK( r.wasrunning );
BOOST_CHECK( r.wasactive );
// stopping...
BOOST_CHECK( mtask.stop() == true );
BOOST_CHECK( r.fini == true );
BOOST_CHECK( mtask.isRunning() == false );
BOOST_CHECK( mtask.isActive() == false );
BOOST_CHECK( mtask.stop() == false );
BOOST_CHECK( mtask.execute() == false );
r.reset(true);
// with periodic master:
SlaveActivity mslave( t_task_np, &r );
BOOST_CHECK( mslave.isActive() == false );
BOOST_CHECK( mslave.isRunning() == false );
BOOST_CHECK( mslave.isPeriodic() == true );
BOOST_CHECK_EQUAL( mslave.getPeriod(), t_task_np->getPeriod() );
BOOST_CHECK( mslave.execute() == false );
BOOST_CHECK( mslave.thread() == t_task_np->thread() );
BOOST_CHECK( !mslave.start() );
BOOST_CHECK( t_task_np->start() );
BOOST_CHECK( mslave.start() );
BOOST_CHECK( r.init == true );
BOOST_CHECK( mslave.isActive() );
BOOST_CHECK( mslave.isRunning() );
// calls step()
BOOST_CHECK( mslave.execute() );
BOOST_CHECK( r.stepped == true );
BOOST_CHECK( r.wasrunning );
BOOST_CHECK( r.wasactive );
BOOST_CHECK( mslave.execute() );
BOOST_CHECK( !mslave.start() );
// stopping...
BOOST_CHECK( mslave.stop() );
BOOST_CHECK( r.fini == true );
BOOST_CHECK( !mslave.isActive() );
BOOST_CHECK( !mslave.isRunning() );
r.reset(true);
// periodic:
SlaveActivity mslave_p(0.001, &r);
BOOST_CHECK( mslave_p.isActive() == false );
BOOST_CHECK( mslave_p.isRunning() == false );
BOOST_CHECK( mslave_p.isPeriodic() == true );
BOOST_CHECK( mslave_p.getPeriod() == 0.001 );
BOOST_CHECK( mslave_p.execute() == false );
BOOST_CHECK( mslave_p.thread() == os::MainThread::Instance() );
BOOST_CHECK( mslave_p.start() );
BOOST_CHECK( r.init == true );
BOOST_CHECK( mslave_p.isActive() );
BOOST_CHECK( mslave_p.isRunning() );
BOOST_CHECK( mslave_p.execute() );
BOOST_CHECK( r.stepped == true );
BOOST_CHECK( r.wasrunning );
BOOST_CHECK( r.wasactive );
BOOST_CHECK( !mslave_p.start() );
// stopping...
BOOST_CHECK( mslave_p.stop() );
BOOST_CHECK( r.fini == true );
BOOST_CHECK( !mslave_p.isActive() );
BOOST_CHECK( !mslave_p.isRunning() );
BOOST_CHECK( !mslave_p.execute() );
}
BOOST_AUTO_TEST_CASE( testSequential )
{
// Test sequential activities
TestRunner r(true);
SequentialActivity mtask(&r);
BOOST_CHECK( mtask.isActive() == false );
BOOST_CHECK( mtask.isRunning() == false );
BOOST_CHECK( mtask.isPeriodic() == false );
BOOST_CHECK( mtask.getPeriod() == 0.0 );
BOOST_CHECK( mtask.execute() == false );
BOOST_CHECK( mtask.trigger() == false );
BOOST_CHECK( mtask.thread() == os::MainThread::Instance() );
// starting...
BOOST_CHECK( mtask.start() == true );
BOOST_CHECK( r.init == true );
BOOST_CHECK( mtask.isActive() == true );
BOOST_CHECK( mtask.isRunning() == false );
BOOST_CHECK( mtask.start() == false );
// calls step()
BOOST_CHECK( mtask.trigger() );
BOOST_CHECK( r.stepped == true );
BOOST_CHECK( r.wasrunning );
BOOST_CHECK( r.wasactive );
BOOST_CHECK( mtask.execute() == false );
// stopping...
BOOST_CHECK( mtask.stop() == true );
BOOST_CHECK( r.fini == true );
BOOST_CHECK( mtask.isRunning() == false );
BOOST_CHECK( mtask.isActive() == false );
BOOST_CHECK( mtask.stop() == false );
BOOST_CHECK( mtask.execute() == false );
BOOST_CHECK( mtask.trigger() == false );
r.reset(false);
BOOST_CHECK( mtask.start() == false );
BOOST_CHECK( r.init == true );
BOOST_CHECK( mtask.isActive() == false );
BOOST_CHECK( mtask.isRunning() == false );
BOOST_CHECK( mtask.start() == false );
}
BOOST_AUTO_TEST_CASE( testScheduler )
{
int rtsched = ORO_SCHED_OTHER;
int bprio = 15;
os::CheckPriority( rtsched, bprio );
TimerThreadPtr tt = TimerThread::Instance(rtsched, bprio, 0.0123);
BOOST_CHECK( tt != 0 );
BOOST_CHECK( tt->isRunning() == false );
BOOST_CHECK_EQUAL( 0.0123, tt->getPeriod());
BOOST_CHECK_EQUAL( bprio, tt->getPriority());
BOOST_CHECK_EQUAL( rtsched, tt->getScheduler());
// different scheduler, different thread.
rtsched = ORO_SCHED_RT;
bprio = 15;
if ( os::CheckPriority( rtsched, bprio ) ) {
TimerThreadPtr tt2 = TimerThread::Instance(rtsched, bprio, 0.0123);
BOOST_CHECK( tt2 != 0 );
BOOST_CHECK( tt2 != tt );
usleep(100000);
BOOST_CHECK_EQUAL( rtsched, tt2->getScheduler());
tt = TimerThread::Instance(bprio, 0.0123); // ORO_SCHED_RT is the default.
BOOST_CHECK( tt == tt2 );
}
}
#if !defined( OROCOS_TARGET_MACOSX )
/**
* Checks if the rtos_task_get_pid function works properly.
*/
BOOST_AUTO_TEST_CASE( testThreadPID )
{
unsigned int tid = t_task_a->getPid();
BOOST_CHECK( tid );
unsigned int mpid = MainThread::Instance()->getPid();
BOOST_CHECK( mpid );
BOOST_CHECK_NE( mpid, tid );
// cout << "PID:" << mpid <<endl;
// cout << "TID:" << tid << endl;
}
#endif
#if !defined( OROCOS_TARGET_WIN32 )
BOOST_AUTO_TEST_CASE( testThreadConfig )
{
int rtsched = ORO_SCHED_RT;
int bprio = 15;
TimerThreadPtr tt = TimerThread::Instance(bprio, 0.0123);
// Test creation of new thread, check functions when not running.
BOOST_CHECK( tt->isRunning() == false );
BOOST_CHECK_EQUAL( 0.0123, tt->getPeriod());
// only do this if valid priority/scheduler range:
if ( os::CheckPriority( rtsched, bprio ) == true)
{
BOOST_CHECK_EQUAL( bprio, tt->getPriority());
// different priority, different thread.
TimerThreadPtr tt2 = TimerThread::Instance(bprio - 1, 0.0123);
BOOST_CHECK( tt2 != 0 );
BOOST_CHECK( tt2 != tt );
// different period, different thread.
TimerThreadPtr tt3 = TimerThread::Instance(bprio, 0.123);
BOOST_CHECK( tt3 != 0 );
BOOST_CHECK( tt3 != tt );
BOOST_CHECK( tt3 != tt2 );
}
tt = TimerThread::Instance(bprio, 0.0123);
BOOST_CHECK( tt != 0 );
BOOST_CHECK( tt == TimerThread::Instance(bprio,0.0123) );
// switching hard/soft
// do not ASSERT since the ret-value may be false...
if ( tt->setScheduler(ORO_SCHED_OTHER) ) {
usleep(100000);
BOOST_CHECK( tt->getScheduler() == ORO_SCHED_OTHER );
}
if ( tt->setScheduler(ORO_SCHED_RT) ) {
usleep(100000);
BOOST_CHECK( tt->getScheduler() == ORO_SCHED_RT );
}
tt->setScheduler(ORO_SCHED_OTHER);
tt->setScheduler(ORO_SCHED_RT);
tt->setScheduler(ORO_SCHED_OTHER);
tt->setScheduler(ORO_SCHED_RT);
if ( tt->setPriority( 4 ) ) {
BOOST_CHECK_EQUAL( tt->getPriority(), 4 );
// even if the priority was changed after construction,
// the thread can be found:
BOOST_CHECK( tt == TimerThread::Instance(4,0.0123) );
}
BOOST_CHECK( tt->start() );
// Check functions when running:
BOOST_CHECK( tt->isRunning() == true );
// prints annoying warning messages...
Logger::LogLevel ll = Logger::log().getLogLevel();
Logger::log().setLogLevel(Logger::Critical);
#ifndef OROCOS_TARGET_XENOMAI // disabled until 'persistent' scheduler switching is implemented for Xenomai.
if ( tt->setScheduler(ORO_SCHED_RT) ) {
usleep(100000);
BOOST_CHECK( tt->getScheduler() == ORO_SCHED_RT );
}
if ( tt->setScheduler(ORO_SCHED_OTHER) ) {
usleep(100000);
BOOST_CHECK( tt->getScheduler() == ORO_SCHED_OTHER );
}
#endif
Logger::log().setLogLevel( ll );
// reconfigure periodicity when running.
BOOST_CHECK( tt->setPeriod(0.5) == true );
// reconfigure periodicity when stopped.
BOOST_CHECK( tt->stop() );
BOOST_CHECK( tt->setPeriod(0.3) );
BOOST_CHECK_EQUAL( Seconds_to_nsecs(0.3), tt->getPeriodNS() );
// some quick start/stops.
BOOST_CHECK( tt->start() );
BOOST_CHECK( tt->stop() );
BOOST_CHECK( tt->start() );
}
#endif
#if !defined( ORO_EMBEDDED ) && !defined( OROCOS_TARGET_WIN32 )
BOOST_AUTO_TEST_CASE( testExceptionRecovery )
{
Logger::LogLevel ll = Logger::log().getLogLevel();
Logger::log().setLogLevel( Logger::Never );
BOOST_CHECK(t_task_np->start());
BOOST_CHECK(t_task_np_bad->start()); // must stop t_task_np too.
BOOST_CHECK(t_task_p->start());
BOOST_CHECK(t_task_a->start());
usleep(100000);
// thread should stop :
Logger::log().setLogLevel( ll );
BOOST_CHECK( !t_task_np_bad->thread()->isRunning() );
// This sometimes fails because of a bug in gnulinux Thread/fosi:
// two TimerThreads are created because the first one is not yet
// running, hence np_bad runs in a different thread than np...
BOOST_CHECK( !t_task_np->isRunning() );
BOOST_CHECK( !t_task_np_bad->isRunning() );
BOOST_CHECK( t_task_p->isRunning() );
BOOST_CHECK( !t_task_a->isRunning() );
BOOST_CHECK( t_task_np->init );
BOOST_CHECK( t_task_np_bad->init );
BOOST_CHECK( t_task_p->init );
BOOST_CHECK( t_task_a->init );
BOOST_CHECK( t_task_np->stepped );
BOOST_CHECK( t_task_np->wasrunning );
BOOST_CHECK( t_task_np->wasactive );
BOOST_CHECK( t_task_np_bad->stepped );
BOOST_CHECK( t_task_np_bad->wasrunning );
BOOST_CHECK( t_task_np_bad->wasactive );
BOOST_CHECK( t_task_p->stepped );
BOOST_CHECK( t_task_p->wasrunning );
BOOST_CHECK( t_task_p->wasactive );
BOOST_CHECK( t_task_a->stepped );
BOOST_CHECK( t_task_a->wasrunning );
BOOST_CHECK( t_task_a->wasactive );
// must be stopped since bad throwed an exception
BOOST_CHECK( t_task_np->fini );
BOOST_CHECK( t_task_np_bad->fini );
BOOST_CHECK( t_task_a->fini );
t_task_p->stop();
// see if we recovered ok :
Logger::log().setLogLevel( Logger::Never );
// do not start np_bad
BOOST_CHECK(t_task_np->start());
BOOST_CHECK(t_task_a->start());
usleep(100000);
Logger::log().setLogLevel( ll );
t_task_p->reset(true);
BOOST_CHECK( t_task_np->isRunning() );
BOOST_CHECK( t_task_np->init );
BOOST_CHECK( t_task_np->stepped );
BOOST_CHECK( t_task_np->wasrunning );
BOOST_CHECK( t_task_np->wasactive );
BOOST_CHECK(t_task_np->stop());
BOOST_CHECK( t_task_np->fini );
}
#endif
BOOST_AUTO_TEST_SUITE_END()