File size: 36,270 Bytes
68ce2fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-    */
/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */

/*
  Author(s):  Min Yang Jung, Anton Deguet
  Created on: 2009-11-17

  (C) Copyright 2009-2026 Johns Hopkins University (JHU), All Rights Reserved.

--- begin cisst license - do not edit ---

This software is provided "as is" under an open source license, with
no warranty.  The complete license can be found in license.txt and
http://www.cisst.org/cisst/license.txt.

--- end cisst license ---
*/

#include "mtsCommandAndEventLocalTest.h"

#include <cisstMultiTask/mtsManagerLocal.h>

#include "mtsTestComponents.h"


mtsCommandAndEventLocalTest::mtsCommandAndEventLocalTest()
{
}


void mtsCommandAndEventLocalTest::setUp(void)
{
}


void mtsCommandAndEventLocalTest::tearDown(void)
{
}

template <class _clientType, class _serverType>
void mtsCommandAndEventLocalTest::TestExecution(_clientType * client, _serverType * server,
                                                double clientExecutionDelay, double serverExecutionDelay,
                                                double blockingDelay)
{
    cmnLogger::SetMask(CMN_LOG_ALLOW_ALL);
    cmnLogger::SetMaskClassAll(CMN_LOG_ALLOW_ALL);
    cmnLogger::SetMaskDefaultLog(CMN_LOG_ALLOW_ALL);

    // execution result used by all functions
    mtsExecutionResult executionResult;

    // we assume both client and servers use the same type
    typedef typename _serverType::value_type value_type;

    // value we used to make sure commands are processed, default is
    // -1, void command set to 0
    value_type valueWrite, valueWritePlusOne, valueRead;
    valueWrite = 4;

    // test functions not connected
    CPPUNIT_ASSERT(!client->InterfaceRequired1.FunctionVoid.IsValid());
    executionResult = client->InterfaceRequired1.FunctionVoid();
    CPPUNIT_ASSERT_EQUAL(mtsExecutionResult::FUNCTION_NOT_BOUND, executionResult.GetResult());
    executionResult = client->InterfaceRequired1.FunctionVoid.ExecuteBlocking();
    CPPUNIT_ASSERT_EQUAL(mtsExecutionResult::FUNCTION_NOT_BOUND, executionResult.GetResult());

    CPPUNIT_ASSERT(!client->InterfaceRequired1.FunctionWrite.IsValid());
    executionResult = client->InterfaceRequired1.FunctionWrite(valueWrite);
    CPPUNIT_ASSERT_EQUAL(mtsExecutionResult::FUNCTION_NOT_BOUND, executionResult.GetResult());
    executionResult = client->InterfaceRequired1.FunctionWrite.ExecuteBlocking(valueWrite);
    CPPUNIT_ASSERT_EQUAL(mtsExecutionResult::FUNCTION_NOT_BOUND, executionResult.GetResult());

    CPPUNIT_ASSERT(!client->InterfaceRequired1.FunctionFilteredWrite.IsValid());
    executionResult = client->InterfaceRequired1.FunctionFilteredWrite(valueWrite);
    CPPUNIT_ASSERT_EQUAL(mtsExecutionResult::FUNCTION_NOT_BOUND, executionResult.GetResult());
    executionResult = client->InterfaceRequired1.FunctionFilteredWrite.ExecuteBlocking(valueWrite);
    CPPUNIT_ASSERT_EQUAL(mtsExecutionResult::FUNCTION_NOT_BOUND, executionResult.GetResult());

    CPPUNIT_ASSERT(!client->InterfaceRequired1.FunctionVoidReturn.IsValid());
    executionResult = client->InterfaceRequired1.FunctionVoidReturn(valueRead);
    CPPUNIT_ASSERT_EQUAL(mtsExecutionResult::FUNCTION_NOT_BOUND, executionResult.GetResult());

    CPPUNIT_ASSERT(!client->InterfaceRequired1.FunctionWriteReturn.IsValid());
    executionResult = client->InterfaceRequired1.FunctionWriteReturn(valueWritePlusOne, valueRead);
    CPPUNIT_ASSERT_EQUAL(mtsExecutionResult::FUNCTION_NOT_BOUND, executionResult.GetResult());

    CPPUNIT_ASSERT(!client->InterfaceRequired1.FunctionRead.IsValid());
    executionResult = client->InterfaceRequired1.FunctionRead(valueRead);
    CPPUNIT_ASSERT_EQUAL(mtsExecutionResult::FUNCTION_NOT_BOUND, executionResult.GetResult());

    CPPUNIT_ASSERT(!client->InterfaceRequired1.FunctionQualifiedRead.IsValid());
    executionResult = client->InterfaceRequired1.FunctionQualifiedRead(valueWrite, valueRead);
    CPPUNIT_ASSERT_EQUAL(mtsExecutionResult::FUNCTION_NOT_BOUND, executionResult.GetResult());

    CPPUNIT_ASSERT(!client->InterfaceRequired1.FunctionStateTableRead.IsValid());
    executionResult = client->InterfaceRequired1.FunctionStateTableRead(valueRead);
    CPPUNIT_ASSERT_EQUAL(mtsExecutionResult::FUNCTION_NOT_BOUND, executionResult.GetResult());

    CPPUNIT_ASSERT(!client->InterfaceRequired1.FunctionStateTableFilteredReadGenericR.IsValid());
    executionResult = client->InterfaceRequired1.FunctionStateTableFilteredReadGenericR(valueRead);
    CPPUNIT_ASSERT_EQUAL(mtsExecutionResult::FUNCTION_NOT_BOUND, executionResult.GetResult());

    CPPUNIT_ASSERT(!client->InterfaceRequired1.FunctionStateTableFilteredReadGenericVR.IsValid());
    executionResult = client->InterfaceRequired1.FunctionStateTableFilteredReadGenericVR(valueRead);
    CPPUNIT_ASSERT_EQUAL(mtsExecutionResult::FUNCTION_NOT_BOUND, executionResult.GetResult());

    CPPUNIT_ASSERT(!client->InterfaceRequired1.FunctionStateTableFilteredReadGenericCF.IsValid());
    executionResult = client->InterfaceRequired1.FunctionStateTableFilteredReadGenericCF(valueRead);
    CPPUNIT_ASSERT_EQUAL(mtsExecutionResult::FUNCTION_NOT_BOUND, executionResult.GetResult());

#if 0
    CPPUNIT_ASSERT(!client->InterfaceRequired1.FunctionStateTableFilteredReadV3VR.IsValid());
    executionResult = client->InterfaceRequired1.FunctionStateTableFilteredReadV3VR(valueRead);
    CPPUNIT_ASSERT_EQUAL(mtsExecutionResult::FUNCTION_NOT_BOUND, executionResult.GetResult());
#endif

    CPPUNIT_ASSERT(!client->InterfaceRequired1.FunctionStateTableFilteredReadV3CF.IsValid());
    executionResult = client->InterfaceRequired1.FunctionStateTableFilteredReadV3CF(valueRead);
    CPPUNIT_ASSERT_EQUAL(mtsExecutionResult::FUNCTION_NOT_BOUND, executionResult.GetResult());

    CPPUNIT_ASSERT(!client->InterfaceRequired1.FunctionStateTableAdvance.IsValid());
    executionResult = client->InterfaceRequired1.FunctionStateTableAdvance();
    CPPUNIT_ASSERT_EQUAL(mtsExecutionResult::FUNCTION_NOT_BOUND, executionResult.GetResult());

    mtsManagerLocal * manager = mtsManagerLocal::GetInstance();
    manager->RemoveAllUserComponents();

    // add to manager and start all
    CPPUNIT_ASSERT(manager->AddComponent(client));
    CPPUNIT_ASSERT(manager->AddComponent(server));
    CPPUNIT_ASSERT(manager->Connect(client->GetName(), "r1", server->GetName(), "p1"));
    manager->CreateAll();
    CPPUNIT_ASSERT(manager->WaitForStateAll(mtsComponentState::READY, StateTransitionMaximumDelay));
    manager->StartAll();
    CPPUNIT_ASSERT(manager->WaitForStateAll(mtsComponentState::ACTIVE, StateTransitionMaximumDelay));

    // test commands and timing
    const double queueingDelay = 10.0 * cmn_ms;
    const osaTimeServer & timeServer = manager->GetTimeServer();
    double startTime, stopTime;

    // check initial values
    CPPUNIT_ASSERT_EQUAL(-1, server->InterfaceProvided1.GetValue()); // initial value
    CPPUNIT_ASSERT_EQUAL(-1, client->InterfaceRequired1.GetValue()); // initial value

    // check that all functions are bound
    CPPUNIT_ASSERT(client->InterfaceRequired1.FunctionVoid.IsValid());
    CPPUNIT_ASSERT(client->InterfaceRequired1.FunctionWrite.IsValid());
    CPPUNIT_ASSERT(client->InterfaceRequired1.FunctionFilteredWrite.IsValid());
    CPPUNIT_ASSERT(client->InterfaceRequired1.FunctionVoidReturn.IsValid());
    CPPUNIT_ASSERT(client->InterfaceRequired1.FunctionWriteReturn.IsValid());
    CPPUNIT_ASSERT(client->InterfaceRequired1.FunctionRead.IsValid());
    CPPUNIT_ASSERT(client->InterfaceRequired1.FunctionQualifiedRead.IsValid());

    // loop over void and write commands to alternate blocking and non
    // blocking commands
    unsigned int index;
    for (index = 0; index < 3; index++) {
        // test void command non blocking
        startTime = timeServer.GetRelativeTime();
        executionResult = client->InterfaceRequired1.FunctionVoid();
        CPPUNIT_ASSERT(executionResult.IsOK()); // queued or succeeded
        stopTime = timeServer.GetRelativeTime();
        CPPUNIT_ASSERT((stopTime - startTime) <= queueingDelay); // make sure execution is fast
        osaSleep(serverExecutionDelay + blockingDelay); // time to dequeue and let command execute
        CPPUNIT_ASSERT_EQUAL(0,  server->InterfaceProvided1.GetValue()); // reset
        CPPUNIT_ASSERT_EQUAL(-1, client->InterfaceRequired1.GetValue()); // unchanged

        // test write command
        startTime = timeServer.GetRelativeTime();
        executionResult = client->InterfaceRequired1.FunctionWrite(valueWrite);
        CPPUNIT_ASSERT(executionResult.IsOK()); // queued or succeeded
        stopTime = timeServer.GetRelativeTime();
        CPPUNIT_ASSERT((stopTime - startTime) <= queueingDelay); // make sure execution is fast
        osaSleep(serverExecutionDelay + blockingDelay);  // time to dequeue and let command execute
        CPPUNIT_ASSERT(valueWrite == server->InterfaceProvided1.GetValue()); // set to new value
        CPPUNIT_ASSERT(-1 == client->InterfaceRequired1.GetValue()); // unchanged

        // test filtered write command
        startTime = timeServer.GetRelativeTime();
        executionResult = client->InterfaceRequired1.FunctionFilteredWrite(valueWrite);
        CPPUNIT_ASSERT(executionResult.IsOK()); // queued or succeeded
        stopTime = timeServer.GetRelativeTime();
        CPPUNIT_ASSERT((stopTime - startTime) <= queueingDelay); // make sure execution is fast
        osaSleep(serverExecutionDelay + blockingDelay);  // time to dequeue and let command execute
        CPPUNIT_ASSERT((valueWrite + 1) == server->InterfaceProvided1.GetValue()); // set to new value + 1 (filter)
        CPPUNIT_ASSERT(-1 == client->InterfaceRequired1.GetValue()); // unchanged

        // test void command blocking
        if (blockingDelay > 0.0) {
            startTime = timeServer.GetRelativeTime();
            executionResult = client->InterfaceRequired1.FunctionVoid.ExecuteBlocking();
            stopTime = timeServer.GetRelativeTime();
            {
                std::stringstream message;
                message << "Execution result expected was COMMAND_SUCCEEDED, got: "
                        << mtsExecutionResult::EnumToString(executionResult.GetResult());
                CPPUNIT_ASSERT_EQUAL_MESSAGE(message.str(),
                                             mtsExecutionResult::COMMAND_SUCCEEDED,
                                             executionResult.GetResult());
            }
            {
                std::stringstream message;
                message << "Actual: " << (stopTime - startTime) << " >= " << (blockingDelay * 0.9);
                CPPUNIT_ASSERT_MESSAGE(message.str(), (stopTime - startTime) >= (blockingDelay * 0.9));
            }
        } else {
            // no significant delay but result should be guaranteed without sleep
            executionResult = client->InterfaceRequired1.FunctionVoid.ExecuteBlocking();
            {
                std::stringstream message;
                message << "Execution result expected was COMMAND_SUCCEEDED, got: "
                        << mtsExecutionResult::EnumToString(executionResult.GetResult());
                CPPUNIT_ASSERT_EQUAL_MESSAGE(message.str(),
                                             mtsExecutionResult::COMMAND_SUCCEEDED,
                                             executionResult.GetResult());
            }
        }
        CPPUNIT_ASSERT(0 == server->InterfaceProvided1.GetValue()); // reset
        CPPUNIT_ASSERT(-1 == client->InterfaceRequired1.GetValue()); // unchanged

        // test write command blocking
        if (blockingDelay > 0.0) {
            startTime = timeServer.GetRelativeTime();
            executionResult = client->InterfaceRequired1.FunctionWrite.ExecuteBlocking(valueWrite);
            stopTime = timeServer.GetRelativeTime();
            {
                std::stringstream message;
                message << "Execution result expected was COMMAND_SUCCEEDED, got: "
                        << mtsExecutionResult::EnumToString(executionResult.GetResult());
                CPPUNIT_ASSERT_EQUAL_MESSAGE(message.str(),
                                             mtsExecutionResult::COMMAND_SUCCEEDED,
                                             executionResult.GetResult());
            }
            {
                std::stringstream message;
                message << "Actual: " << (stopTime - startTime) << " >= " << (blockingDelay * 0.9);
                CPPUNIT_ASSERT_MESSAGE(message.str(), (stopTime - startTime) >= (blockingDelay * 0.9));
            }
        } else {
            // no significant delay but result should be guaranteed without sleep
            executionResult = client->InterfaceRequired1.FunctionWrite.ExecuteBlocking(valueWrite);
            {
                std::stringstream message;
                message << "Execution result expected was COMMAND_SUCCEEDED, got: "
                        << mtsExecutionResult::EnumToString(executionResult.GetResult());
                CPPUNIT_ASSERT_EQUAL_MESSAGE(message.str(),
                                             mtsExecutionResult::COMMAND_SUCCEEDED,
                                             executionResult.GetResult());
            }
        }
        CPPUNIT_ASSERT(valueWrite == server->InterfaceProvided1.GetValue()); // set to new value
        CPPUNIT_ASSERT(-1 == client->InterfaceRequired1.GetValue()); // unchanged

        // test filtered write command blocking
        if (blockingDelay > 0.0) {
            startTime = timeServer.GetRelativeTime();
            executionResult = client->InterfaceRequired1.FunctionFilteredWrite.ExecuteBlocking(valueWrite);
            stopTime = timeServer.GetRelativeTime();
            {
                std::stringstream message;
                message << "Execution result expected was COMMAND_SUCCEEDED, got: "
                        << mtsExecutionResult::EnumToString(executionResult.GetResult());
                CPPUNIT_ASSERT_EQUAL_MESSAGE(message.str(),
                                             mtsExecutionResult::COMMAND_SUCCEEDED,
                                             executionResult.GetResult());
            }
            {
                std::stringstream message;
                message << "Actual: " << (stopTime - startTime) << " >= " << (blockingDelay * 0.9);
                CPPUNIT_ASSERT_MESSAGE(message.str(), (stopTime - startTime) >= (blockingDelay * 0.9));
            }
        } else {
            // no significant delay but result should be guaranteed without sleep
            executionResult = client->InterfaceRequired1.FunctionFilteredWrite.ExecuteBlocking(valueWrite);
            {
                std::stringstream message;
                message << "Execution result expected was COMMAND_SUCCEEDED, got: "
                        << mtsExecutionResult::EnumToString(executionResult.GetResult());
                CPPUNIT_ASSERT_EQUAL_MESSAGE(message.str(),
                                             mtsExecutionResult::COMMAND_SUCCEEDED,
                                             executionResult.GetResult());
            }
        }
        CPPUNIT_ASSERT((valueWrite + 1) == server->InterfaceProvided1.GetValue()); // set to new value + 1 (filter)
        CPPUNIT_ASSERT(-1 == client->InterfaceRequired1.GetValue()); // unchanged

        // test void return command (always blocking)
        if (blockingDelay > 0.0) {
            startTime = timeServer.GetRelativeTime();
            executionResult = client->InterfaceRequired1.FunctionVoidReturn(valueRead);
            stopTime = timeServer.GetRelativeTime();
            {
                std::stringstream message;
                message << "Execution result expected was COMMAND_SUCCEEDED, got: "
                        << mtsExecutionResult::EnumToString(executionResult.GetResult());
                CPPUNIT_ASSERT_EQUAL_MESSAGE(message.str(),
                                             mtsExecutionResult::COMMAND_SUCCEEDED,
                                             executionResult.GetResult());
            }
            {
                std::stringstream message;
                message << "Actual: " << (stopTime - startTime) << " >= " << (blockingDelay * 0.9);
                CPPUNIT_ASSERT_MESSAGE(message.str(), (stopTime - startTime) >= (blockingDelay * 0.9));
            }
        } else {
            // no significant delay but result should be guaranteed without sleep
            executionResult = client->InterfaceRequired1.FunctionVoidReturn(valueRead);
            {
                std::stringstream message;
                message << "Execution result expected was COMMAND_SUCCEEDED, got: "
                        << mtsExecutionResult::EnumToString(executionResult.GetResult());
                CPPUNIT_ASSERT_EQUAL_MESSAGE(message.str(),
                                             mtsExecutionResult::COMMAND_SUCCEEDED,
                                             executionResult.GetResult());
            }
        }
        CPPUNIT_ASSERT(valueRead == 1); // number was positive
        CPPUNIT_ASSERT((-(valueWrite + 1)) == server->InterfaceProvided1.GetValue()); // negated
        CPPUNIT_ASSERT(-1 == client->InterfaceRequired1.GetValue()); // unchanged

        // call write return to change sign of value back
        valueWritePlusOne = valueWrite + 1;
        if (blockingDelay > 0.0) {
            startTime = timeServer.GetRelativeTime();
            executionResult = client->InterfaceRequired1.FunctionWriteReturn(valueWritePlusOne, valueRead);
            stopTime = timeServer.GetRelativeTime();
            {
                std::stringstream message;
                message << "Execution result expected was COMMAND_SUCCEEDED, got: "
                        << mtsExecutionResult::EnumToString(executionResult.GetResult());
                CPPUNIT_ASSERT_EQUAL_MESSAGE(message.str(),
                                             mtsExecutionResult::COMMAND_SUCCEEDED,
                                             executionResult.GetResult());
            }
            {
                std::stringstream message;
                message << "Actual: " << (stopTime - startTime) << " >= " << (blockingDelay * 0.9);
                CPPUNIT_ASSERT_MESSAGE(message.str(), (stopTime - startTime) >= (blockingDelay * 0.9));
            }
        } else {
            // no significant delay but result should be guaranteed without sleep
            executionResult = client->InterfaceRequired1.FunctionWriteReturn(valueWritePlusOne, valueRead);
            {
                std::stringstream message;
                message << "Execution result expected was COMMAND_SUCCEEDED, got: "
                        << mtsExecutionResult::EnumToString(executionResult.GetResult());
                CPPUNIT_ASSERT_EQUAL_MESSAGE(message.str(),
                                             mtsExecutionResult::COMMAND_SUCCEEDED,
                                             executionResult.GetResult());
            }
        }
        CPPUNIT_ASSERT(valueRead == -1); // number was negative
        CPPUNIT_ASSERT((valueWrite + 1) == server->InterfaceProvided1.GetValue()); // negated back
        CPPUNIT_ASSERT(-1 == client->InterfaceRequired1.GetValue()); // unchanged

        // test the state table read, first advance (blocking to make sure it happened)
        client->InterfaceRequired1.FunctionStateTableAdvance.ExecuteBlocking();
        client->InterfaceRequired1.FunctionStateTableRead(valueRead);
        CPPUNIT_ASSERT(valueRead == static_cast<int>(index + 1));
        valueRead = 0;
        client->InterfaceRequired1.FunctionStateTableFilteredReadGenericR(valueRead);
        CPPUNIT_ASSERT(valueRead == static_cast<int>(index + 1));
        valueRead = 0;
        client->InterfaceRequired1.FunctionStateTableFilteredReadGenericVR(valueRead);
        CPPUNIT_ASSERT(valueRead == static_cast<int>(index + 1));
        valueRead = 0;
        client->InterfaceRequired1.FunctionStateTableFilteredReadGenericCF(valueRead);
        CPPUNIT_ASSERT(valueRead == static_cast<int>(index + 1));
#if 0
        // Not currently supported
        valueRead = 0;
        client->InterfaceRequired1.FunctionStateTableFilteredReadV3VR(valueRead);
        CPPUNIT_ASSERT(valueRead == static_cast<int>(3*(index + 1)));
#endif
        valueRead = 0;
        client->InterfaceRequired1.FunctionStateTableFilteredReadV3CF(valueRead);
        CPPUNIT_ASSERT(valueRead == static_cast<int>(3*(index + 1)));
    }

    // test read command
    valueRead = 0;
    executionResult = client->InterfaceRequired1.FunctionRead(valueRead);
    {
        std::stringstream message;
        message << "Execution result expected was COMMAND_SUCCEEDED, got: "
                << mtsExecutionResult::EnumToString(executionResult.GetResult());
        CPPUNIT_ASSERT_EQUAL_MESSAGE(message.str(),
                                     mtsExecutionResult::COMMAND_SUCCEEDED,
                                     executionResult.GetResult());
    }
    CPPUNIT_ASSERT((valueWrite + 1) == valueRead);
    CPPUNIT_ASSERT((valueWrite + 1) == server->InterfaceProvided1.GetValue()); // unchanged
    CPPUNIT_ASSERT(-1 == client->InterfaceRequired1.GetValue()); // unchanged
    // test read command with downcast to bool for execution result
    CPPUNIT_ASSERT(client->InterfaceRequired1.FunctionRead(valueRead));

    // test qualified read command
    valueRead = 0;
    executionResult = client->InterfaceRequired1.FunctionQualifiedRead(valueWrite, valueRead);
    {
        std::stringstream message;
        message << "Execution result expected was COMMAND_SUCCEEDED, got: "
                << mtsExecutionResult::EnumToString(executionResult.GetResult());
        CPPUNIT_ASSERT_EQUAL_MESSAGE(message.str(),
                                     mtsExecutionResult::COMMAND_SUCCEEDED,
                                     executionResult.GetResult());
    }
    CPPUNIT_ASSERT((valueWrite + 1) == valueRead);
    CPPUNIT_ASSERT((valueWrite + 1) == server->InterfaceProvided1.GetValue()); // unchanged
    CPPUNIT_ASSERT(-1 == client->InterfaceRequired1.GetValue()); // unchanged

    // test void event
    server->InterfaceProvided1.EventVoid();
    osaSleep(clientExecutionDelay);
    CPPUNIT_ASSERT((valueWrite + 1) == server->InterfaceProvided1.GetValue()); // unchanged
    CPPUNIT_ASSERT(0 == client->InterfaceRequired1.GetValue()); // reset by void event

    // test write event
    server->InterfaceProvided1.EventWrite(valueWrite);
    osaSleep(clientExecutionDelay);
    CPPUNIT_ASSERT((valueWrite + 1) == server->InterfaceProvided1.GetValue()); // unchanged
    CPPUNIT_ASSERT(valueWrite == client->InterfaceRequired1.GetValue()); // set by write event

    // stop all and cleanup
    manager->KillAll();
    CPPUNIT_ASSERT(manager->WaitForStateAll(mtsComponentState::FINISHED, StateTransitionMaximumDelay));
    CPPUNIT_ASSERT(manager->Disconnect(client->GetName(), "r1", server->GetName(), "p1"));
    CPPUNIT_ASSERT(manager->RemoveComponent(client));
    CPPUNIT_ASSERT(manager->RemoveComponent(server));
}


template <class _elementType>
void mtsCommandAndEventLocalTest::TestDeviceDevice(void)
{
    mtsTestDevice2<_elementType> * client = new mtsTestDevice2<_elementType>;
    mtsTestDevice3<_elementType> * server = new mtsTestDevice3<_elementType>;
    TestExecution(client, server, 0.0, 0.0);
    delete client;
    delete server;
}
void mtsCommandAndEventLocalTest::TestDeviceDevice_mtsInt(void) {
    mtsCommandAndEventLocalTest::TestDeviceDevice<mtsInt>();
}
void mtsCommandAndEventLocalTest::TestDeviceDevice_int(void) {
    mtsCommandAndEventLocalTest::TestDeviceDevice<int>();
}


template <class _elementType>
void mtsCommandAndEventLocalTest::TestPeriodicPeriodic(void)
{
    mtsTestPeriodic1<_elementType> * client = new mtsTestPeriodic1<_elementType>("mtsTestPeriodic1Client");
    mtsTestPeriodic1<_elementType> * server = new mtsTestPeriodic1<_elementType>("mtsTestPeriodic1Server");
    // these delays are OS dependent, we might need to increase them later
    const double clientExecutionDelay = 0.1 * cmn_s;
    const double serverExecutionDelay = 0.1 * cmn_s;
    TestExecution(client, server, clientExecutionDelay, serverExecutionDelay);
    delete client;
    delete server;
}
void mtsCommandAndEventLocalTest::TestPeriodicPeriodic_mtsInt(void) {
    mtsCommandAndEventLocalTest::TestPeriodicPeriodic<mtsInt>();
}
void mtsCommandAndEventLocalTest::TestPeriodicPeriodic_int(void) {
    mtsCommandAndEventLocalTest::TestPeriodicPeriodic<int>();
}


template <class _elementType>
void mtsCommandAndEventLocalTest::TestContinuousContinuous(void)
{
    mtsTestContinuous1<_elementType> * client = new mtsTestContinuous1<_elementType>("mtsTestContinuous1Client");
    mtsTestContinuous1<_elementType> * server = new mtsTestContinuous1<_elementType>("mtsTestContinuous1Server");
    // these delays are OS dependent, we might need to increase them later
    const double clientExecutionDelay = 0.1 * cmn_s;
    const double serverExecutionDelay = 0.1 * cmn_s;
    TestExecution(client, server, clientExecutionDelay, serverExecutionDelay);
    delete client;
    delete server;
}
void mtsCommandAndEventLocalTest::TestContinuousContinuous_mtsInt(void) {
    mtsCommandAndEventLocalTest::TestContinuousContinuous<mtsInt>();
}
void mtsCommandAndEventLocalTest::TestContinuousContinuous_int(void) {
    mtsCommandAndEventLocalTest::TestContinuousContinuous<int>();
}


template <class _elementType>
void mtsCommandAndEventLocalTest::TestFromCallbackFromCallback(void)
{
    mtsTestFromCallback1<_elementType> * client = new mtsTestFromCallback1<_elementType>("mtsTestFromCallback1Client");
    mtsTestCallbackTrigger * clientTrigger = new mtsTestCallbackTrigger(client);
    mtsTestFromCallback1<_elementType> * server = new mtsTestFromCallback1<_elementType>("mtsTestFromCallback1Server");
    mtsTestCallbackTrigger * serverTrigger = new mtsTestCallbackTrigger(server);
    // these delays are OS dependent, we might need to increase them later
    const double clientExecutionDelay = 0.1 * cmn_s;
    const double serverExecutionDelay = 0.1 * cmn_s;
    TestExecution(client, server, clientExecutionDelay, serverExecutionDelay);
    clientTrigger->Stop();
    delete clientTrigger;
    delete client;
    serverTrigger->Stop();
    delete serverTrigger;
    delete server;
}
void mtsCommandAndEventLocalTest::TestFromCallbackFromCallback_mtsInt(void) {
    mtsCommandAndEventLocalTest::TestFromCallbackFromCallback<mtsInt>();
}
void mtsCommandAndEventLocalTest::TestFromCallbackFromCallback_int(void) {
    mtsCommandAndEventLocalTest::TestFromCallbackFromCallback<int>();
}


template <class _elementType>
void mtsCommandAndEventLocalTest::TestFromSignalFromSignal(void)
{
    mtsTestFromSignal1<_elementType> * client = new mtsTestFromSignal1<_elementType>("mtsTestFromSignal1Client");
    mtsTestFromSignal1<_elementType> * server = new mtsTestFromSignal1<_elementType>("mtsTestFromSignal1Server");
    // these delays are OS dependent, we might need to increase them later
    const double clientExecutionDelay = 0.1 * cmn_s;
    const double serverExecutionDelay = 0.1 * cmn_s;
    TestExecution(client, server, clientExecutionDelay, serverExecutionDelay);
    delete client;
    delete server;
}
void mtsCommandAndEventLocalTest::TestFromSignalFromSignal_mtsInt(void) {
    mtsCommandAndEventLocalTest::TestFromSignalFromSignal<mtsInt>();
}
void mtsCommandAndEventLocalTest::TestFromSignalFromSignal_int(void) {
    mtsCommandAndEventLocalTest::TestFromSignalFromSignal<int>();
}


template <class _elementType>
void mtsCommandAndEventLocalTest::TestPeriodicPeriodicBlocking(void)
{
    const double blockingDelay = 0.25 * cmn_s;
    mtsTestPeriodic1<_elementType> * client = new mtsTestPeriodic1<_elementType>("mtsTestPeriodic1Client");
    mtsTestPeriodic1<_elementType> * server = new mtsTestPeriodic1<_elementType>("mtsTestPeriodic1Server", blockingDelay);
    // these delays are OS dependent, increased to avoid CI timing failures
    const double clientExecutionDelay = 0.5 * cmn_s;
    const double serverExecutionDelay = 0.5 * cmn_s;
    TestExecution(client, server, clientExecutionDelay, serverExecutionDelay, blockingDelay);
    delete client;
    delete server;
}
void mtsCommandAndEventLocalTest::TestPeriodicPeriodicBlocking_mtsInt(void) {
    mtsCommandAndEventLocalTest::TestPeriodicPeriodicBlocking<mtsInt>();
}
void mtsCommandAndEventLocalTest::TestPeriodicPeriodicBlocking_int(void) {
    mtsCommandAndEventLocalTest::TestPeriodicPeriodicBlocking<int>();
}


template <class _elementType>
void mtsCommandAndEventLocalTest::TestContinuousContinuousBlocking(void)
{
    const double blockingDelay = 0.25 * cmn_s;
    mtsTestContinuous1<_elementType> * client = new mtsTestContinuous1<_elementType>("mtsTestContinuous1Client");
    mtsTestContinuous1<_elementType> * server = new mtsTestContinuous1<_elementType>("mtsTestContinuous1Server", blockingDelay);
    // these delays are OS dependent, increased to avoid CI timing failures
    const double clientExecutionDelay = 0.5 * cmn_s;
    const double serverExecutionDelay = 0.5 * cmn_s;
    TestExecution(client, server, clientExecutionDelay, serverExecutionDelay, blockingDelay);
    delete client;
    delete server;
}
void mtsCommandAndEventLocalTest::TestContinuousContinuousBlocking_mtsInt(void) {
    mtsCommandAndEventLocalTest::TestContinuousContinuousBlocking<mtsInt>();
}
void mtsCommandAndEventLocalTest::TestContinuousContinuousBlocking_int(void) {
    mtsCommandAndEventLocalTest::TestContinuousContinuousBlocking<int>();
}


template <class _elementType>
void mtsCommandAndEventLocalTest::TestFromCallbackFromCallbackBlocking(void)
{
    const double blockingDelay = 0.25 * cmn_s;
    mtsTestFromCallback1<_elementType> * client = new mtsTestFromCallback1<_elementType>("mtsTestFromCallback1Client");
    mtsTestCallbackTrigger * clientTrigger = new mtsTestCallbackTrigger(client);
    mtsTestFromCallback1<_elementType> * server = new mtsTestFromCallback1<_elementType>("mtsTestFromCallback1Server", blockingDelay);
    mtsTestCallbackTrigger * serverTrigger = new mtsTestCallbackTrigger(server);
    // these delays are OS dependent, increased to avoid CI timing failures
    const double clientExecutionDelay = 0.5 * cmn_s;
    const double serverExecutionDelay = 0.5 * cmn_s;
    TestExecution(client, server, clientExecutionDelay, serverExecutionDelay, blockingDelay);
    clientTrigger->Stop();
    delete clientTrigger;
    delete client;
    serverTrigger->Stop();
    delete serverTrigger;
    delete server;
}
void mtsCommandAndEventLocalTest::TestFromCallbackFromCallbackBlocking_mtsInt(void) {
    mtsCommandAndEventLocalTest::TestFromCallbackFromCallbackBlocking<mtsInt>();
}
void mtsCommandAndEventLocalTest::TestFromCallbackFromCallbackBlocking_int(void) {
    mtsCommandAndEventLocalTest::TestFromCallbackFromCallbackBlocking<int>();
}


template <class _elementType>
void mtsCommandAndEventLocalTest::TestFromSignalFromSignalBlocking(void)
{
    const double blockingDelay = 0.25 * cmn_s;
    mtsTestFromSignal1<_elementType> * client = new mtsTestFromSignal1<_elementType>("mtsTestFromSignal1Client");
    mtsTestFromSignal1<_elementType> * server = new mtsTestFromSignal1<_elementType>("mtsTestFromSignal1Server", blockingDelay);
    // these delays are OS dependent, increased to avoid CI timing failures
    const double clientExecutionDelay = 0.5 * cmn_s;
    const double serverExecutionDelay = 0.5 * cmn_s;
    TestExecution(client, server, clientExecutionDelay, serverExecutionDelay, blockingDelay);
    delete client;
    delete server;
}
void mtsCommandAndEventLocalTest::TestFromSignalFromSignalBlocking_mtsInt(void) {
    mtsCommandAndEventLocalTest::TestFromSignalFromSignalBlocking<mtsInt>();
}
void mtsCommandAndEventLocalTest::TestFromSignalFromSignalBlocking_int(void) {
    mtsCommandAndEventLocalTest::TestFromSignalFromSignalBlocking<int>();
}


template <class _elementType>
void mtsCommandAndEventLocalTest::TestArgumentPrototypes(void)
{
    typedef _elementType value_type;

    mtsTestContinuous1<value_type> * client = new mtsTestContinuous1<value_type>("mtsTestContinuous1Client");
    mtsTestContinuous1<value_type> * server = new mtsTestContinuous1<value_type>("mtsTestContinuous1Server");

    mtsManagerLocal * manager = mtsManagerLocal::GetInstance();
    manager->RemoveAllUserComponents();

    // add to manager and start all
    CPPUNIT_ASSERT(manager->AddComponent(client));
    CPPUNIT_ASSERT(manager->AddComponent(server));
    CPPUNIT_ASSERT(manager->Connect(client->GetName(), "r1", server->GetName(), "p1"));
    manager->CreateAll();
    CPPUNIT_ASSERT(manager->WaitForStateAll(mtsComponentState::READY, StateTransitionMaximumDelay));
    manager->StartAll();
    CPPUNIT_ASSERT(manager->WaitForStateAll(mtsComponentState::ACTIVE, StateTransitionMaximumDelay));

    // test that values are set properly for all argument prototypes
    const mtsGenericObject * argumentPrototypeGeneric;
    const mtsInt * argumentPrototype;
    int expected, actual;

    // test argument prototype for void with result command
    argumentPrototypeGeneric = client->InterfaceRequired1.FunctionVoidReturn.GetResultPrototype();
    CPPUNIT_ASSERT(argumentPrototypeGeneric != 0);
    argumentPrototype = dynamic_cast<const mtsInt *>(argumentPrototypeGeneric);
    CPPUNIT_ASSERT(argumentPrototype != 0);
    expected = mtsTestInterfaceProvided<mtsInt>::Argument2PrototypeDefault;
    actual = *argumentPrototype;
    CPPUNIT_ASSERT_EQUAL(expected, actual);

    // test argument prototype for write command
    argumentPrototypeGeneric = client->InterfaceRequired1.FunctionWrite.GetArgumentPrototype();
    CPPUNIT_ASSERT(argumentPrototypeGeneric != 0);
    argumentPrototype = dynamic_cast<const mtsInt *>(argumentPrototypeGeneric);
    CPPUNIT_ASSERT(argumentPrototype != 0);
    expected = mtsTestInterfaceProvided<mtsInt>::Argument1PrototypeDefault;
    actual = *argumentPrototype;
    CPPUNIT_ASSERT_EQUAL(expected, actual);

    // test argument prototype for read command
    argumentPrototypeGeneric = client->InterfaceRequired1.FunctionRead.GetArgumentPrototype();
    CPPUNIT_ASSERT(argumentPrototypeGeneric != 0);
    argumentPrototype = dynamic_cast<const mtsInt *>(argumentPrototypeGeneric);
    CPPUNIT_ASSERT(argumentPrototype != 0);
    expected = mtsTestInterfaceProvided<mtsInt>::Argument1PrototypeDefault;
    actual = *argumentPrototype;
    CPPUNIT_ASSERT_EQUAL(expected, actual);

    // test argument prototype for qualified read command
    argumentPrototypeGeneric = client->InterfaceRequired1.FunctionQualifiedRead.GetArgument1Prototype();
    CPPUNIT_ASSERT(argumentPrototypeGeneric != 0);
    argumentPrototype = dynamic_cast<const mtsInt *>(argumentPrototypeGeneric);
    CPPUNIT_ASSERT(argumentPrototype != 0);
    expected = mtsTestInterfaceProvided<mtsInt>::Argument1PrototypeDefault;
    actual = *argumentPrototype;
    CPPUNIT_ASSERT_EQUAL(expected, actual);

    argumentPrototypeGeneric = client->InterfaceRequired1.FunctionQualifiedRead.GetArgument2Prototype();
    CPPUNIT_ASSERT(argumentPrototypeGeneric != 0);
    argumentPrototype = dynamic_cast<const mtsInt *>(argumentPrototypeGeneric);
    CPPUNIT_ASSERT(argumentPrototype != 0);
    expected = mtsTestInterfaceProvided<mtsInt>::Argument2PrototypeDefault;
    actual = *argumentPrototype;
    CPPUNIT_ASSERT_EQUAL(expected, actual);

    // stop all and cleanup
    manager->KillAll();
    CPPUNIT_ASSERT(manager->WaitForStateAll(mtsComponentState::FINISHED, StateTransitionMaximumDelay));
    CPPUNIT_ASSERT(manager->Disconnect(client->GetName(), "r1", server->GetName(), "p1"));
    CPPUNIT_ASSERT(manager->RemoveComponent(client));
    CPPUNIT_ASSERT(manager->RemoveComponent(server));
    delete client;
    delete server;
}
void mtsCommandAndEventLocalTest::TestArgumentPrototypes_mtsInt(void) {
    mtsCommandAndEventLocalTest::TestArgumentPrototypes<mtsInt>();
}
void mtsCommandAndEventLocalTest::TestArgumentPrototypes_int(void) {
    mtsCommandAndEventLocalTest::TestArgumentPrototypes<int>();
}


CPPUNIT_TEST_SUITE_REGISTRATION(mtsCommandAndEventLocalTest);