Newer
Older
Martin Christoph Hierholzer
committed
/*
* testTestFacilities.cc
*
* Created on: Feb 20, 2017
* Author: Martin Hierholzer
*/
#include <future>
#include <chrono>
#define BOOST_TEST_MODULE testTestFacilities
#include <boost/test/included/unit_test.hpp>
#include <boost/test/test_case_template.hpp>
#include <boost/mpl/list.hpp>
#include <mtca4u/Device.h>
Martin Christoph Hierholzer
committed
#include <mtca4u/ExperimentalFeatures.h>
Martin Christoph Hierholzer
committed
#include "Application.h"
#include "ScalarAccessor.h"
#include "ApplicationModule.h"
#include "DeviceModule.h"
#include "ControlSystemModule.h"
#include "TestDecoratorRegisterAccessor.h"
Martin Christoph Hierholzer
committed
#include "VariableGroup.h"
#include "TestFacility.h"
Martin Christoph Hierholzer
committed
using namespace boost::unit_test_framework;
namespace ctk = ChimeraTK;
#define CHECK_TIMEOUT(condition, maxMilliseconds) \
{ \
std::chrono::steady_clock::time_point t0 = std::chrono::steady_clock::now(); \
while(!(condition)) { \
bool timeout_reached = (std::chrono::steady_clock::now()-t0) > std::chrono::milliseconds(maxMilliseconds); \
BOOST_CHECK( !timeout_reached ); \
if(timeout_reached) break; \
usleep(1000); \
} \
}
// list of user types the accessors are tested with
typedef boost::mpl::list<int8_t,uint8_t,
int16_t,uint16_t,
int32_t,uint32_t,
float,double> test_types;
constexpr char dummySdm[] = "sdm://./dummy=test.map";
Martin Christoph Hierholzer
committed
/*********************************************************************************************************************/
/* the BlockingReadTestModule blockingly reads its input in the main loop and writes the result to its output */
template<typename T>
struct BlockingReadTestModule : public ctk::ApplicationModule {
using ctk::ApplicationModule::ApplicationModule;
Martin Christoph Hierholzer
committed
ctk::ScalarPushInput<T> someInput{this, "someInput", "cm", "This is just some input for testing"};
ctk::ScalarOutput<T> someOutput{this, "someOutput", "cm", "Description"};
void mainLoop() {
while(true) {
someInput.read();
T val = someInput;
someOutput = val;
usleep(10000); // wait some extra time to make sure we are really blocking the test procedure thread
Martin Christoph Hierholzer
committed
someOutput.write();
}
}
};
/*********************************************************************************************************************/
/* the AsyncReadTestModule asynchronously reads its input in the main loop and writes the result to its output */
Martin Christoph Hierholzer
committed
template<typename T>
Martin Christoph Hierholzer
committed
struct AsyncReadTestModule : public ctk::ApplicationModule {
using ctk::ApplicationModule::ApplicationModule;
Martin Christoph Hierholzer
committed
Martin Christoph Hierholzer
committed
ctk::ScalarPushInput<T> someInput{this, "someInput", "cm", "This is just some input for testing"};
Martin Christoph Hierholzer
committed
ctk::ScalarOutput<T> someOutput{this, "someOutput", "cm", "Description"};
Martin Christoph Hierholzer
committed
void mainLoop() {
while(true) {
auto &future = someInput.readAsync();
future.wait();
T val = someInput;
someOutput = val;
usleep(10000); // wait some extra time to make sure we are really blocking the test procedure thread
Martin Christoph Hierholzer
committed
someOutput.write();
}
}
Martin Christoph Hierholzer
committed
};
Martin Christoph Hierholzer
committed
/*********************************************************************************************************************/
/* the ReadAnyTestModule calls readAny on a bunch of inputs and outputs some information on the received data */
template<typename T>
struct ReadAnyTestModule : public ctk::ApplicationModule {
using ctk::ApplicationModule::ApplicationModule;
Martin Christoph Hierholzer
committed
struct Inputs : public ctk::VariableGroup {
using ctk::VariableGroup::VariableGroup;
ctk::ScalarPushInput<T> v1{this, "v1", "cm", "Input 1 for testing"};
ctk::ScalarPushInput<T> v2{this, "v2", "cm", "Input 2 for testing"};
ctk::ScalarPushInput<T> v3{this, "v3", "cm", "Input 3 for testing"};
ctk::ScalarPushInput<T> v4{this, "v4", "cm", "Input 4 for testing"};
};
Inputs inputs{this, "inputs", "A group of inputs"};
Martin Christoph Hierholzer
committed
ctk::ScalarOutput<T> value{this, "value", "cm", "The last value received from any of the inputs"};
ctk::ScalarOutput<uint32_t> index{this, "index", "", "The index (1..4) of the input where the last value was received"};
void mainLoop() {
while(true) {
auto justRead = inputs.readAny();
if(inputs.v1.getId() == justRead) {
Martin Christoph Hierholzer
committed
index = 1;
value = (T)inputs.v1;
}
else if(inputs.v2.getId() == justRead) {
Martin Christoph Hierholzer
committed
index = 2;
value = (T)inputs.v2;
}
else if(inputs.v3.getId() == justRead) {
Martin Christoph Hierholzer
committed
index = 3;
value = (T)inputs.v3;
}
else if(inputs.v4.getId() == justRead) {
Martin Christoph Hierholzer
committed
index = 4;
value = (T)inputs.v4;
}
else {
index = 0;
value = 0;
}
usleep(10000); // wait some extra time to make sure we are really blocking the test procedure thread
index.write();
value.write();
}
}
};
Martin Christoph Hierholzer
committed
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
/*********************************************************************************************************************/
/* the PollingReadModule is designed to test poll-type transfers (even mixed with push-type) */
template<typename T>
struct PollingReadModule : public ctk::ApplicationModule {
using ctk::ApplicationModule::ApplicationModule;
ctk::ScalarPushInput<T> push{this, "push", "cm", "A push-type input"};
ctk::ScalarPushInput<T> push2{this, "push2", "cm", "A second push-type input"};
ctk::ScalarPollInput<T> poll{this, "poll", "cm", "A poll-type input"};
ctk::ScalarOutput<T> valuePush{this, "valuePush", "cm", "The last value received for 'push'"};
ctk::ScalarOutput<T> valuePoll{this, "valuePoll", "cm", "The last value received for 'poll'"};
ctk::ScalarOutput<int> state{this, "state", "", "State of the test mainLoop"};
void mainLoop() {
while(true) {
push.read();
poll.read();
valuePush = (T)push;
valuePoll = (T)poll;
valuePoll.write();
valuePush.write();
state = 1;
state.write();
push2.read();
push.readNonBlocking();
poll.read();
valuePush = (T)push;
valuePoll = (T)poll;
valuePoll.write();
valuePush.write();
state = 2;
state.write();
push2.read();
push.readLatest();
poll.read();
valuePush = (T)push;
valuePoll = (T)poll;
valuePoll.write();
valuePush.write();
state = 3;
state.write();
}
}
};
Martin Christoph Hierholzer
committed
/*********************************************************************************************************************/
/* dummy application */
template<typename T>
struct TestApplication : public ctk::Application {
Martin Christoph Hierholzer
committed
TestApplication() : Application("testApplication") {
ctk::ExperimentalFeatures::enable();
}
Martin Christoph Hierholzer
committed
~TestApplication() { shutdown(); }
using Application::makeConnections; // we call makeConnections() manually in the tests to catch exceptions etc.
Martin Christoph Hierholzer
committed
void defineConnections() {} // setup is done in the tests
Martin Christoph Hierholzer
committed
ctk::ControlSystemModule cs{""};
ctk::DeviceModule dev{dummySdm,""};
BlockingReadTestModule<T> blockingReadTestModule{this,"blockingReadTestModule", "Module for testing blocking read"};
AsyncReadTestModule<T> asyncReadTestModule{this,"asyncReadTestModule", "Module for testing async read"};
ReadAnyTestModule<T> readAnyTestModule{this,"readAnyTestModule", "Module for testing readAny()"};
Martin Christoph Hierholzer
committed
};
Martin Christoph Hierholzer
committed
/*********************************************************************************************************************/
/* second application */
template<typename T>
struct PollingTestApplication : public ctk::Application {
PollingTestApplication() : Application("testApplication") {}
~PollingTestApplication() { shutdown(); }
Martin Christoph Hierholzer
committed
void defineConnections() {} // setup is done in the tests
Martin Christoph Hierholzer
committed
ctk::ControlSystemModule cs{""};
PollingReadModule<T> pollingReadModule{this,"pollingReadModule", "Module for testing poll-type transfers"};
};
Martin Christoph Hierholzer
committed
/*********************************************************************************************************************/
/* test that no TestDecoratorRegisterAccessor is used if the testable mode is not enabled */
BOOST_AUTO_TEST_CASE_TEMPLATE( testNoDecorator, T, test_types ) {
std::cout << "*********************************************************************************************************************" << std::endl;
std::cout << "==> testNoDecorator<" << typeid(T).name() << ">" << std::endl;
TestApplication<T> app;
auto pvManagers = ctk::createPVManager();
app.setPVManager(pvManagers.second);
Martin Christoph Hierholzer
committed
app.blockingReadTestModule.connectTo(app.cs["blocking"]);
app.asyncReadTestModule.connectTo(app.cs["async"]);
app.readAnyTestModule.connectTo(app.cs["readAny"]);
Martin Christoph Hierholzer
committed
app.initialise();
app.run();
// check if we got the decorator for the input
auto hlinput = app.blockingReadTestModule.someInput.getHighLevelImplElement();
Martin Christoph Hierholzer
committed
BOOST_CHECK( boost::dynamic_pointer_cast<ctk::TestDecoratorRegisterAccessor<T>>(hlinput) == nullptr );
// check that we did not get the decorator for the output
auto hloutput = app.blockingReadTestModule.someOutput.getHighLevelImplElement();
Martin Christoph Hierholzer
committed
BOOST_CHECK( boost::dynamic_pointer_cast<ctk::TestDecoratorRegisterAccessor<T>>(hloutput) == nullptr );
}
Martin Christoph Hierholzer
committed
/*********************************************************************************************************************/
/* test blocking read in test mode */
BOOST_AUTO_TEST_CASE_TEMPLATE( testBlockingRead, T, test_types ) {
std::cout << "*********************************************************************************************************************" << std::endl;
std::cout << "==> testBlockingRead<" << typeid(T).name() << ">" << std::endl;
TestApplication<T> app;
app.cs("input") >> app.blockingReadTestModule.someInput;
app.blockingReadTestModule.someOutput >> app.cs("output");
Martin Christoph Hierholzer
committed
app.asyncReadTestModule.connectTo(app.cs["async"]); // avoid runtime warning
app.readAnyTestModule.connectTo(app.cs["readAny"]); // avoid runtime warning
ctk::TestFacility test;
auto pvInput = test.getScalar<T>("input");
auto pvOutput = test.getScalar<T>("output");
test.runApplication();
Martin Christoph Hierholzer
committed
// test blocking read when taking control in the test thread (note: the blocking read is executed in the app module!)
Martin Christoph Hierholzer
committed
for(int i=0; i<5; ++i) {
pvInput = 120+i;
pvInput.write();
Martin Christoph Hierholzer
committed
usleep(10000);
BOOST_CHECK(pvOutput.readNonBlocking() == false);
test.stepApplication();
CHECK_TIMEOUT(pvOutput.readNonBlocking() == true, 200);
int val = pvOutput;
Martin Christoph Hierholzer
committed
BOOST_CHECK(val == 120+i);
}
}
/*********************************************************************************************************************/
/* test async read in test mode */
BOOST_AUTO_TEST_CASE_TEMPLATE( testAsyncRead, T, test_types ) {
std::cout << "*********************************************************************************************************************" << std::endl;
std::cout << "==> testAsyncRead<" << typeid(T).name() << ">" << std::endl;
TestApplication<T> app;
app.cs("input") >> app.asyncReadTestModule.someInput;
app.asyncReadTestModule.someOutput >> app.cs("output");
Martin Christoph Hierholzer
committed
app.blockingReadTestModule.connectTo(app.cs["blocking"]); // avoid runtime warning
app.readAnyTestModule.connectTo(app.cs["readAny"]); // avoid runtime warning
ctk::TestFacility test;
auto pvInput = test.getScalar<T>("input");
auto pvOutput = test.getScalar<T>("output");
test.runApplication();
Martin Christoph Hierholzer
committed
// test blocking read when taking control in the test thread
for(int i=0; i<5; ++i) {
pvInput = 120+i;
pvInput.write();
Martin Christoph Hierholzer
committed
usleep(10000);
BOOST_CHECK(pvOutput.readNonBlocking() == false);
test.stepApplication();
bool ret = pvOutput.readNonBlocking();
BOOST_CHECK(ret == true);
if(!ret) {
CHECK_TIMEOUT(pvOutput.readNonBlocking() == true, 10000);
}
int val = pvOutput;
Martin Christoph Hierholzer
committed
BOOST_CHECK(val == 120+i);
}
Martin Christoph Hierholzer
committed
}
Martin Christoph Hierholzer
committed
/*********************************************************************************************************************/
/* test testReadAny in test mode */
BOOST_AUTO_TEST_CASE_TEMPLATE( testReadAny, T, test_types ) {
std::cout << "*********************************************************************************************************************" << std::endl;
std::cout << "==> testReadAny<" << typeid(T).name() << ">" << std::endl;
TestApplication<T> app;
Martin Christoph Hierholzer
committed
app.readAnyTestModule.inputs.connectTo(app.cs["input"]);
app.readAnyTestModule.value >> app.cs("value");
app.readAnyTestModule.index >> app.cs("index");
Martin Christoph Hierholzer
committed
app.blockingReadTestModule.connectTo(app.cs["blocking"]); // avoid runtime warning
app.asyncReadTestModule.connectTo(app.cs["async"]); // avoid runtime warning
ctk::TestFacility test;
auto value = test.getScalar<T>("value");
auto index = test.getScalar<uint32_t>("index");
auto v1 = test.getScalar<T>("input/v1");
auto v2 = test.getScalar<T>("input/v2");
auto v3 = test.getScalar<T>("input/v3");
auto v4 = test.getScalar<T>("input/v4");
test.runApplication();
Martin Christoph Hierholzer
committed
// check that we don't receive anything yet
usleep(10000);
BOOST_CHECK(value.readNonBlocking() == false);
BOOST_CHECK(index.readNonBlocking() == false);
Martin Christoph Hierholzer
committed
// send something to v4
v4 = 66;
v4.write();
Martin Christoph Hierholzer
committed
// check that we still don't receive anything yet
usleep(10000);
BOOST_CHECK(value.readNonBlocking() == false);
BOOST_CHECK(index.readNonBlocking() == false);
Martin Christoph Hierholzer
committed
// run the application and check that we got the expected result
test.stepApplication();
BOOST_CHECK(value.readNonBlocking() == true);
BOOST_CHECK(index.readNonBlocking() == true);
BOOST_CHECK(value == 66);
BOOST_CHECK(index == 4);
Martin Christoph Hierholzer
committed
v1 = 33;
v1.write();
// check that we still don't receive anything yet
usleep(10000);
BOOST_CHECK(value.readNonBlocking() == false);
BOOST_CHECK(index.readNonBlocking() == false);
// run the application and check that we got the expected result
test.stepApplication();
BOOST_CHECK(value.readNonBlocking() == true);
BOOST_CHECK(index.readNonBlocking() == true);
BOOST_CHECK(value == 33);
BOOST_CHECK(index == 1);
// send something to v1 again
v1 = 34;
v1.write();
// check that we still don't receive anything yet
usleep(10000);
BOOST_CHECK(value.readNonBlocking() == false);
BOOST_CHECK(index.readNonBlocking() == false);
// run the application and check that we got the expected result
test.stepApplication();
BOOST_CHECK(value.readNonBlocking() == true);
BOOST_CHECK(index.readNonBlocking() == true);
BOOST_CHECK(value == 34);
BOOST_CHECK(index == 1);
v3 = 40;
v3.write();
// check that we still don't receive anything yet
usleep(10000);
BOOST_CHECK(value.readNonBlocking() == false);
BOOST_CHECK(index.readNonBlocking() == false);
// run the application and check that we got the expected result
test.stepApplication();
BOOST_CHECK(value.readNonBlocking() == true);
BOOST_CHECK(index.readNonBlocking() == true);
BOOST_CHECK(value == 40);
BOOST_CHECK(index == 3);
v2 = 50;
v2.write();
// check that we still don't receive anything yet
usleep(10000);
BOOST_CHECK(value.readNonBlocking() == false);
BOOST_CHECK(index.readNonBlocking() == false);
// run the application and check that we got the expected result
test.stepApplication();
BOOST_CHECK(value.readNonBlocking() == true);
BOOST_CHECK(index.readNonBlocking() == true);
BOOST_CHECK(value == 50);
BOOST_CHECK(index == 2);
// check that stepApplication() throws an exception if no input data is available
try {
test.stepApplication();
BOOST_ERROR("IllegalParameter exception expected.");
}
catch(ctk::ApplicationExceptionWithID<ctk::ApplicationExceptionID::illegalParameter>) {
}
// check that we still don't receive anything anymore
usleep(10000);
BOOST_CHECK(value.readNonBlocking() == false);
BOOST_CHECK(index.readNonBlocking() == false);
// send something to v1 a 3rd time
v1 = 35;
v1.write();
// check that we still don't receive anything yet
usleep(10000);
BOOST_CHECK(value.readNonBlocking() == false);
BOOST_CHECK(index.readNonBlocking() == false);
// run the application and check that we got the expected result
test.stepApplication();
BOOST_CHECK(value.readNonBlocking() == true);
BOOST_CHECK(index.readNonBlocking() == true);
BOOST_CHECK(value == 35);
BOOST_CHECK(index == 1);
Martin Christoph Hierholzer
committed
}
/*********************************************************************************************************************/
/* test the interplay of multiple chained modules and their threads in test mode */
BOOST_AUTO_TEST_CASE_TEMPLATE( testChainedModules, T, test_types ) {
std::cout << "*********************************************************************************************************************" << std::endl;
std::cout << "==> testChainedModules<" << typeid(T).name() << ">" << std::endl;
TestApplication<T> app;
// put everything we got into one chain
Martin Christoph Hierholzer
committed
app.readAnyTestModule.inputs.connectTo(app.cs["input"]);
app.readAnyTestModule.value >> app.blockingReadTestModule.someInput;
app.blockingReadTestModule.someOutput >> app.asyncReadTestModule.someInput;
app.asyncReadTestModule.someOutput >> app.cs("value");
app.readAnyTestModule.index >> app.cs("index");
ctk::TestFacility test;
auto value = test.getScalar<T>("value");
auto index = test.getScalar<uint32_t>("index");
auto v1 = test.getScalar<T>("input/v1");
auto v2 = test.getScalar<T>("input/v2");
auto v3 = test.getScalar<T>("input/v3");
auto v4 = test.getScalar<T>("input/v4");
test.runApplication();
// check that we don't receive anything yet
usleep(10000);
BOOST_CHECK(value.readNonBlocking() == false);
BOOST_CHECK(index.readNonBlocking() == false);
// send something to v2
v2 = 11;
v2.write();
// check that we still don't receive anything yet
usleep(10000);
BOOST_CHECK(value.readNonBlocking() == false);
BOOST_CHECK(index.readNonBlocking() == false);
// run the application and check that we got the expected result
BOOST_CHECK(value.readNonBlocking() == true);
BOOST_CHECK(index.readNonBlocking() == true);
BOOST_CHECK(value == 11);
BOOST_CHECK(index == 2);
// send something to v3
v3 = 12;
v3.write();
// check that we still don't receive anything yet
usleep(10000);
BOOST_CHECK(value.readNonBlocking() == false);
BOOST_CHECK(index.readNonBlocking() == false);
// run the application and check that we got the expected result
BOOST_CHECK(value.readNonBlocking() == true);
BOOST_CHECK(index.readNonBlocking() == true);
BOOST_CHECK(value == 12);
BOOST_CHECK(index == 3);
// send something to v3 again
v3 = 13;
v3.write();
// check that we still don't receive anything yet
usleep(10000);
BOOST_CHECK(value.readNonBlocking() == false);
BOOST_CHECK(index.readNonBlocking() == false);
// run the application and check that we got the expected result
BOOST_CHECK(value.readNonBlocking() == true);
BOOST_CHECK(index.readNonBlocking() == true);
BOOST_CHECK(value == 13);
BOOST_CHECK(index == 3);
// check that stepApplication() throws an exception if no input data is available
try {
test.stepApplication();
BOOST_ERROR("IllegalParameter exception expected.");
}
catch(ctk::ApplicationExceptionWithID<ctk::ApplicationExceptionID::illegalParameter>) {
}
// check that we still don't receive anything anymore
BOOST_CHECK(value.readNonBlocking() == false);
BOOST_CHECK(index.readNonBlocking() == false);
/*********************************************************************************************************************/
/* test combination with fan out */
BOOST_AUTO_TEST_CASE_TEMPLATE( testWithFanOut, T, test_types ) {
std::cout << "*********************************************************************************************************************" << std::endl;
std::cout << "==> testWithFanOut<" << typeid(T).name() << ">" << std::endl;
TestApplication<T> app;
// distribute a value to multiple inputs
Martin Christoph Hierholzer
committed
app.readAnyTestModule.inputs.connectTo(app.cs["input"]);
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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
app.readAnyTestModule.value >> app.blockingReadTestModule.someInput >> app.asyncReadTestModule.someInput;
app.blockingReadTestModule.someOutput >> app.cs("valueFromBlocking");
app.asyncReadTestModule.someOutput >> app.cs("valueFromAsync");
app.readAnyTestModule.index >> app.cs("index");
ctk::TestFacility test;
auto valueFromBlocking = test.getScalar<T>("valueFromBlocking");
auto valueFromAsync = test.getScalar<T>("valueFromAsync");
auto index = test.getScalar<uint32_t>("index");
auto v1 = test.getScalar<T>("input/v1");
auto v2 = test.getScalar<T>("input/v2");
auto v3 = test.getScalar<T>("input/v3");
auto v4 = test.getScalar<T>("input/v4");
test.runApplication();
// check that we don't receive anything yet
usleep(10000);
BOOST_CHECK(valueFromBlocking.readNonBlocking() == false);
BOOST_CHECK(valueFromAsync.readNonBlocking() == false);
BOOST_CHECK(index.readNonBlocking() == false);
// send something to v2
v2 = 11;
v2.write();
// check that we still don't receive anything yet
usleep(10000);
BOOST_CHECK(valueFromBlocking.readNonBlocking() == false);
BOOST_CHECK(valueFromAsync.readNonBlocking() == false);
BOOST_CHECK(index.readNonBlocking() == false);
// run the application and check that we got the expected result
test.stepApplication();
BOOST_CHECK(valueFromBlocking.readNonBlocking() == true);
BOOST_CHECK(valueFromAsync.readNonBlocking() == true);
BOOST_CHECK(index.readNonBlocking() == true);
BOOST_CHECK(valueFromBlocking == 11);
BOOST_CHECK(valueFromAsync == 11);
BOOST_CHECK(index == 2);
// send something to v3
v3 = 12;
v3.write();
// check that we still don't receive anything yet
usleep(10000);
BOOST_CHECK(valueFromBlocking.readNonBlocking() == false);
BOOST_CHECK(valueFromAsync.readNonBlocking() == false);
BOOST_CHECK(index.readNonBlocking() == false);
// run the application and check that we got the expected result
test.stepApplication();
BOOST_CHECK(valueFromBlocking.readNonBlocking() == true);
BOOST_CHECK(valueFromAsync.readNonBlocking() == true);
BOOST_CHECK(index.readNonBlocking() == true);
BOOST_CHECK(valueFromBlocking == 12);
BOOST_CHECK(valueFromAsync == 12);
BOOST_CHECK(index == 3);
// send something to v3 again
v3 = 13;
v3.write();
// check that we still don't receive anything yet
usleep(10000);
BOOST_CHECK(valueFromBlocking.readNonBlocking() == false);
BOOST_CHECK(valueFromAsync.readNonBlocking() == false);
BOOST_CHECK(index.readNonBlocking() == false);
// run the application and check that we got the expected result
test.stepApplication();
BOOST_CHECK(valueFromBlocking.readNonBlocking() == true);
BOOST_CHECK(valueFromAsync.readNonBlocking() == true);
BOOST_CHECK(index.readNonBlocking() == true);
BOOST_CHECK(valueFromBlocking == 13);
BOOST_CHECK(valueFromAsync == 13);
BOOST_CHECK(index == 3);
// check that stepApplication() throws an exception if no input data is available
try {
test.stepApplication();
BOOST_ERROR("IllegalParameter exception expected.");
}
catch(ctk::ApplicationExceptionWithID<ctk::ApplicationExceptionID::illegalParameter>) {
}
// check that we still don't receive anything anymore
usleep(10000);
BOOST_CHECK(valueFromBlocking.readNonBlocking() == false);
BOOST_CHECK(valueFromAsync.readNonBlocking() == false);
BOOST_CHECK(index.readNonBlocking() == false);
}
/*********************************************************************************************************************/
/* test combination with trigger */
BOOST_AUTO_TEST_CASE_TEMPLATE( testWithTrigger, T, test_types ) {
std::cout << "*********************************************************************************************************************" << std::endl;
std::cout << "==> testWithTrigger<" << typeid(T).name() << ">" << std::endl;
TestApplication<T> app;
// distribute a value to multiple inputs
auto triggernode = app.cs("trigger", typeid(int), 1);
app.cs("v1") >> app.readAnyTestModule.inputs.v1;
app.dev("REG2") [ triggernode ] >> app.readAnyTestModule.inputs.v2;
app.cs("v3") >> app.readAnyTestModule.inputs.v3;
app.cs("v4") >> app.readAnyTestModule.inputs.v4;
app.readAnyTestModule.value >> app.blockingReadTestModule.someInput >> app.asyncReadTestModule.someInput;
app.blockingReadTestModule.someOutput >> app.cs("valueFromBlocking");
app.asyncReadTestModule.someOutput >> app.cs("valueFromAsync");
app.readAnyTestModule.index >> app.cs("index");
ctk::TestFacility test;
ctk::Device dev;
dev.open(dummySdm);
auto valueFromBlocking = test.getScalar<T>("valueFromBlocking");
auto valueFromAsync = test.getScalar<T>("valueFromAsync");
auto index = test.getScalar<uint32_t>("index");
auto trigger = test.getScalar<int>("trigger");
auto v2 = dev.getScalarRegisterAccessor<T>("REG2");
test.runApplication();
// check that we don't receive anything yet
usleep(10000);
BOOST_CHECK(valueFromBlocking.readNonBlocking() == false);
BOOST_CHECK(valueFromAsync.readNonBlocking() == false);
BOOST_CHECK(index.readNonBlocking() == false);
// send something to v2 and send the trigger
v2 = 11;
v2.write();
trigger.write();
// check that we still don't receive anything yet
usleep(10000);
BOOST_CHECK(valueFromBlocking.readNonBlocking() == false);
BOOST_CHECK(valueFromAsync.readNonBlocking() == false);
BOOST_CHECK(index.readNonBlocking() == false);
// run the application and check that we got the expected result
test.stepApplication();
BOOST_CHECK(valueFromBlocking.readNonBlocking() == true);
BOOST_CHECK(valueFromAsync.readNonBlocking() == true);
BOOST_CHECK(index.readNonBlocking() == true);
BOOST_CHECK(valueFromBlocking == 11);
BOOST_CHECK(valueFromAsync == 11);
BOOST_CHECK(index == 2);
// again send something to v2 and send the trigger
v2 = 22;
v2.write();
trigger.write();
// check that we still don't receive anything yet
usleep(10000);
BOOST_CHECK(valueFromBlocking.readNonBlocking() == false);
BOOST_CHECK(valueFromAsync.readNonBlocking() == false);
BOOST_CHECK(index.readNonBlocking() == false);
// run the application and check that we got the expected result
test.stepApplication();
BOOST_CHECK(valueFromBlocking.readNonBlocking() == true);
BOOST_CHECK(valueFromAsync.readNonBlocking() == true);
BOOST_CHECK(index.readNonBlocking() == true);
BOOST_CHECK(valueFromBlocking == 22);
BOOST_CHECK(valueFromAsync == 22);
BOOST_CHECK(index == 2);
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
// check that stepApplication() throws an exception if no input data is available
try {
test.stepApplication();
BOOST_ERROR("IllegalParameter exception expected.");
}
catch(ctk::ApplicationExceptionWithID<ctk::ApplicationExceptionID::illegalParameter>) {
}
// check that we still don't receive anything anymore
usleep(10000);
BOOST_CHECK(valueFromBlocking.readNonBlocking() == false);
BOOST_CHECK(valueFromAsync.readNonBlocking() == false);
BOOST_CHECK(index.readNonBlocking() == false);
}
/*********************************************************************************************************************/
/* test combination with trigger distributed to mutliple receivers */
BOOST_AUTO_TEST_CASE_TEMPLATE( testWithTriggerFanOut, T, test_types ) {
std::cout << "*********************************************************************************************************************" << std::endl;
std::cout << "==> testWithTriggerFanOut<" << typeid(T).name() << ">" << std::endl;
TestApplication<T> app;
// distribute a value to multiple inputs
auto triggernode = app.cs("trigger", typeid(int), 1);
app.dev("REG1") [ triggernode ] >> app.readAnyTestModule.inputs.v1;
app.cs("v2") >> app.readAnyTestModule.inputs.v2;
app.cs("v3") >> app.readAnyTestModule.inputs.v3;
app.cs("v4") >> app.readAnyTestModule.inputs.v4;
app.dev("REG2") [ triggernode ] >> app.asyncReadTestModule.someInput;
app.dev("REG3") [ triggernode ] >> app.blockingReadTestModule.someInput;
app.readAnyTestModule.value >> app.cs("valueFromAny");
app.readAnyTestModule.index >> app.cs("index");
app.blockingReadTestModule.someOutput >> app.cs("valueFromBlocking");
app.asyncReadTestModule.someOutput >> app.cs("valueFromAsync");
ctk::TestFacility test;
ctk::Device dev;
dev.open(dummySdm);
auto valueFromBlocking = test.getScalar<T>("valueFromBlocking");
auto valueFromAsync = test.getScalar<T>("valueFromAsync");
auto valueFromAny = test.getScalar<T>("valueFromAny");
auto index = test.getScalar<uint32_t>("index");
auto trigger = test.getScalar<int>("trigger");
auto r1 = dev.getScalarRegisterAccessor<T>("REG1");
auto r2 = dev.getScalarRegisterAccessor<T>("REG2");
auto r3 = dev.getScalarRegisterAccessor<T>("REG3");
test.runApplication();
// check that we don't receive anything yet
usleep(10000);
BOOST_CHECK(valueFromBlocking.readNonBlocking() == false);
BOOST_CHECK(valueFromAsync.readNonBlocking() == false);
BOOST_CHECK(index.readNonBlocking() == false);
BOOST_CHECK(index.readNonBlocking() == false);
// send something to the registers and send the trigger
r1 = 11;
r2 = 22;
r3 = 33;
r1.write();
r2.write();
r3.write();
trigger.write();
// check that we still don't receive anything yet
usleep(10000);
BOOST_CHECK(valueFromBlocking.readNonBlocking() == false);
BOOST_CHECK(valueFromAsync.readNonBlocking() == false);
BOOST_CHECK(valueFromAny.readNonBlocking() == false);
BOOST_CHECK(index.readNonBlocking() == false);
// run the application and check that we got the expected result
test.stepApplication();
BOOST_CHECK(valueFromBlocking.readNonBlocking() == true);
BOOST_CHECK(valueFromAsync.readNonBlocking() == true);
BOOST_CHECK(valueFromAny.readNonBlocking() == true);
BOOST_CHECK(index.readNonBlocking() == true);
BOOST_CHECK(valueFromBlocking == 33);
BOOST_CHECK(valueFromAsync == 22);
BOOST_CHECK(valueFromAny == 11);
BOOST_CHECK(index == 1);
// check that we don't receive anything yet
usleep(10000);
BOOST_CHECK(valueFromBlocking.readNonBlocking() == false);
BOOST_CHECK(valueFromAsync.readNonBlocking() == false);
BOOST_CHECK(index.readNonBlocking() == false);
BOOST_CHECK(index.readNonBlocking() == false);
// send something else to the registers and send the trigger
r1 = 6;
r2 = 5;
r3 = 4;
r1.write();
r2.write();
r3.write();
trigger.write();
// check that we still don't receive anything yet
usleep(10000);
BOOST_CHECK(valueFromBlocking.readNonBlocking() == false);
BOOST_CHECK(valueFromAsync.readNonBlocking() == false);
BOOST_CHECK(valueFromAny.readNonBlocking() == false);
BOOST_CHECK(index.readNonBlocking() == false);
// run the application and check that we got the expected result
test.stepApplication();
BOOST_CHECK(valueFromBlocking.readNonBlocking() == true);
BOOST_CHECK(valueFromAsync.readNonBlocking() == true);
BOOST_CHECK(valueFromAny.readNonBlocking() == true);
BOOST_CHECK(index.readNonBlocking() == true);
BOOST_CHECK(valueFromBlocking == 4);
BOOST_CHECK(valueFromAsync == 5);
BOOST_CHECK(valueFromAny == 6);
BOOST_CHECK(index == 1);
// check that stepApplication() throws an exception if no input data is available
try {
test.stepApplication();
BOOST_ERROR("IllegalParameter exception expected.");
}
catch(ctk::ApplicationExceptionWithID<ctk::ApplicationExceptionID::illegalParameter>) {
}
// check that we still don't receive anything anymore
usleep(10000);
BOOST_CHECK(valueFromBlocking.readNonBlocking() == false);
BOOST_CHECK(valueFromAsync.readNonBlocking() == false);
BOOST_CHECK(valueFromAny.readNonBlocking() == false);
BOOST_CHECK(index.readNonBlocking() == false);
/*********************************************************************************************************************/
/* test convenience read functions */
BOOST_AUTO_TEST_CASE_TEMPLATE( testConvenienceRead, T, test_types ) {
std::cout << "*********************************************************************************************************************" << std::endl;
std::cout << "==> testConvenienceRead<" << typeid(T).name() << ">" << std::endl;
TestApplication<T> app;
app.cs("input") >> app.blockingReadTestModule.someInput;
app.blockingReadTestModule.someOutput >> app.cs("output");
app.asyncReadTestModule.connectTo(app.cs["async"]); // avoid runtime warning
app.readAnyTestModule.connectTo(app.cs["readAny"]); // avoid runtime warning
ctk::TestFacility test;
test.runApplication();
// test blocking read when taking control in the test thread (note: the blocking read is executed in the app module!)
for(int i=0; i<5; ++i) {
test.writeScalar<T>("input", 120+i);
test.stepApplication();
CHECK_TIMEOUT(test.readScalar<T>("output") == T(120+i), 200);
}
// same with array function (still a scalar variable behind, but this does not matter)
for(int i=0; i<5; ++i) {
std::vector<T> myValue{T(120+i)};
test.writeArray<T>("input", myValue);
test.stepApplication();
CHECK_TIMEOUT(test.readArray<T>("output") == std::vector<T>{T(120+i)}, 200);
}
}
Martin Christoph Hierholzer
committed
Martin Christoph Hierholzer
committed
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
/*********************************************************************************************************************/
/* test testable mode when reading from constants */
BOOST_AUTO_TEST_CASE_TEMPLATE( testConstants, T, test_types ) {
std::cout << "*********************************************************************************************************************" << std::endl;
std::cout << "==> testConstants<" << typeid(T).name() << ">" << std::endl;
{
TestApplication<T> app;
ctk::VariableNetworkNode::makeConstant<T>(true, 18) >> app.blockingReadTestModule.someInput;
ctk::VariableNetworkNode::makeConstant<T>(true, 20) >> app.asyncReadTestModule.someInput;
ctk::VariableNetworkNode::makeConstant<T>(true, 22) >> app.readAnyTestModule.inputs.v1;
ctk::VariableNetworkNode::makeConstant<T>(true, 23) >> app.readAnyTestModule.inputs.v2;
ctk::VariableNetworkNode::makeConstant<T>(true, 24) >> app.readAnyTestModule.inputs.v3;
app.blockingReadTestModule.someOutput >> app.cs("blockingOutput");
app.asyncReadTestModule.someOutput >> app.cs("asyncOutput");
app.cs("v4") >> app.readAnyTestModule.inputs.v4;
app.readAnyTestModule.value >> app.cs("value");
app.readAnyTestModule.index >> app.cs("index");
ctk::TestFacility test;
test.runApplication();
BOOST_CHECK_EQUAL( (T)app.blockingReadTestModule.someInput, 18 );
BOOST_CHECK_EQUAL( (T)app.asyncReadTestModule.someInput, 20 );
BOOST_CHECK_EQUAL( (T)app.readAnyTestModule.inputs.v1, 22 );
BOOST_CHECK_EQUAL( (T)app.readAnyTestModule.inputs.v2, 23 );
BOOST_CHECK_EQUAL( (T)app.readAnyTestModule.inputs.v3, 24 );
test.writeScalar<T>("v4", 27);
test.stepApplication();
BOOST_CHECK_EQUAL( test.readScalar<uint32_t>("index"), 4 );
BOOST_CHECK_EQUAL( test.readScalar<T>("value"), 27 );
test.writeScalar<T>("v4", 30);
test.stepApplication();
BOOST_CHECK_EQUAL( test.readScalar<uint32_t>("index"), 4 );
BOOST_CHECK_EQUAL( test.readScalar<T>("value"), 30 );
Martin Christoph Hierholzer
committed
}
Martin Christoph Hierholzer
committed
Martin Christoph Hierholzer
committed
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
{
PollingTestApplication<T> app;
ctk::VariableNetworkNode::makeConstant<T>(true, 18) >> app.pollingReadModule.push2;
ctk::VariableNetworkNode::makeConstant<T>(true, 20) >> app.pollingReadModule.poll;
app.pollingReadModule.connectTo(app.cs);
ctk::TestFacility test;
test.runApplication();
BOOST_CHECK_EQUAL( (T)app.pollingReadModule.push2, 18 );
BOOST_CHECK_EQUAL( (T)app.pollingReadModule.poll, 20 );
BOOST_CHECK_EQUAL( test.readScalar<T>("push2"), 18 );
BOOST_CHECK_EQUAL( test.readScalar<T>("poll"), 20 );
test.writeScalar<T>("push", 22);
test.stepApplication();
BOOST_CHECK_EQUAL( test.readScalar<int>("state"), 1 );
BOOST_CHECK_EQUAL( test.readScalar<T>("valuePush"), 22 );
BOOST_CHECK_EQUAL( test.readScalar<T>("valuePoll"), 20 );
// continuing will now stall the tests
test.writeScalar<T>("push", 23);
try {
test.stepApplication();
BOOST_ERROR("Exception expected.");
}
catch(ctk::Application::TestsStalled&) {
}
Martin Christoph Hierholzer
committed
}
Martin Christoph Hierholzer
committed
Martin Christoph Hierholzer
committed
}
Martin Christoph Hierholzer
committed
/*********************************************************************************************************************/
/* test poll-type transfers mixed with push-type */
BOOST_AUTO_TEST_CASE_TEMPLATE( testPolling, T, test_types ) {
std::cout << "*********************************************************************************************************************" << std::endl;
std::cout << "==> testPolling<" << typeid(T).name() << ">" << std::endl;
PollingTestApplication<T> app;
Martin Christoph Hierholzer
committed
app.pollingReadModule.connectTo(app.cs);
Martin Christoph Hierholzer
committed
ctk::TestFacility test;
test.runApplication();
auto pv_push = test.getScalar<T>("push");
auto pv_push2 = test.getScalar<T>("push2");
auto pv_poll = test.getScalar<T>("poll");
auto pv_valuePush = test.getScalar<T>("valuePush");