4 #define BOOST_TEST_DYN_LINK
5 #define BOOST_TEST_MODULE SharedDummyBackendTest
11 #include <boost/filesystem.hpp>
12 #include <boost/interprocess/managed_shared_memory.hpp>
13 #include <boost/test/unit_test.hpp>
28 using namespace boost::unit_test_framework;
31 static void interrupt_handler(
int);
36 static std::string instanceId{
"1"};
37 static std::string mapFileName{
"shareddummy.map"};
39 static bool terminationCaught =
false;
41 BOOST_AUTO_TEST_SUITE(SharedDummyBackendTestSuite)
44 TestFixture() : argc(framework::master_test_suite().argc), argv(framework::master_test_suite().argv) {}
53 unsigned nIterations = 0;
55 std::cout <<
"Illegal number of arguments. Test case must be called with "
56 "the number of read/write cycles!"
60 nIterations = atoi(argv[1]);
65 bool readbackCorrect =
false;
66 bool waitingForResponse =
true;
67 const unsigned maxIncorrectIterations = 20;
68 unsigned iterations = 0;
69 unsigned incorrectIterations = 0;
73 dev.
open(
"SHDMEMDEV");
77 std::vector<int> processVarsOld(processVarsWrite.
getNElements(), 0);
81 for(
size_t i = 0; i < processVarsWrite.
getNElements(); ++i) {
82 processVarsWrite[i] = i + iterations;
84 processVarsWrite.
write();
90 processVarsRead.
read();
91 }
while((std::vector<int>)processVarsRead == (std::vector<int>)processVarsOld && !waitingForResponse);
93 if((std::vector<int>)processVarsWrite == (std::vector<int>)processVarsRead) {
94 if(waitingForResponse) {
95 waitingForResponse =
false;
97 readbackCorrect =
true;
100 readbackCorrect =
false;
101 if(!waitingForResponse) {
102 std::cout <<
"Corrupted data detected:" << std::endl;
103 for(
const auto pvr : processVarsRead) {
104 std::cout <<
" " << pvr << std::endl;
109 if(waitingForResponse) {
110 incorrectIterations++;
115 processVarsOld = processVarsWrite;
116 }
while((readbackCorrect || waitingForResponse) && incorrectIterations != maxIncorrectIterations &&
117 iterations != nIterations);
119 BOOST_CHECK(readbackCorrect);
120 std::cout <<
"Finished test after " << iterations <<
" of " << nIterations <<
" Iterations." << std::endl;
121 if(incorrectIterations == maxIncorrectIterations) {
122 std::cout <<
"Test cancelled because echoing process did not respond!" << std::endl;
136 signal(SIGINT, interrupt_handler);
138 bool keepRunning =
false;
140 if(argc >= 2 && !strcmp(argv[1],
"KEEP_RUNNING")) {
146 boost::filesystem::path absPathToMapFile = boost::filesystem::absolute(mapFileName);
153 dev.
open(
"SHDMEMDEV");
160 processVarsRead.
read();
164 for(
size_t i = 0; i < processVarsRead.
getNElements(); ++i) {
165 processVarsWrite[i] = processVarsRead[i];
167 processVarsWrite.
write();
168 }
while(keepRunning && !terminationCaught);
182 dev.
open(
"SHDMEMDEV");
188 BOOST_CHECK(!roRegisterOne_dw.isReadOnly());
189 BOOST_CHECK(roRegisterOne_dw.isWriteable());
190 BOOST_CHECK(roRegisterTwo.isReadOnly());
191 BOOST_CHECK(!roRegisterTwo.isWriteable());
196 roRegisterTwo.read();
197 const int roRegisterTwoValue = roRegisterTwo;
198 roRegisterOne_dw = roRegisterTwoValue;
199 roRegisterOne_dw.write();
217 dev.
open(
"SHDMEMDEV");
222 std::generate(processVars.
begin(), processVars.
end(), [n]()
mutable { return n++; });
240 dev.
open(
"SHDMEMDEV");
246 BOOST_CHECK(std::all_of(processVars.
begin(), processVars.
end(), [](
int i) { return i == 0; }));
257 boost::filesystem::path absPathToMapFile = boost::filesystem::absolute(mapFileName);
266 BOOST_AUTO_TEST_SUITE_END()
272 static void interrupt_handler(
int signal) {
273 std::cout <<
"Caught interrupt signal (" << signal <<
"). Terminating..." << std::endl;
274 terminationCaught =
true;