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);
35 std::string mapFileName{
"shareddummy.map"};
39 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")) {
149 dev.
open(
"SHDMEMDEV");
156 processVarsRead.
read();
160 for(
size_t i = 0; i < processVarsRead.
getNElements(); ++i) {
161 processVarsWrite[i] = processVarsRead[i];
163 processVarsWrite.
write();
164 }
while(keepRunning && !terminationCaught);
178 dev.
open(
"SHDMEMDEV");
184 BOOST_CHECK(!roRegisterOne_dw.isReadOnly());
185 BOOST_CHECK(roRegisterOne_dw.isWriteable());
186 BOOST_CHECK(roRegisterTwo.isReadOnly());
187 BOOST_CHECK(!roRegisterTwo.isWriteable());
192 roRegisterTwo.read();
193 const int roRegisterTwoValue = roRegisterTwo;
194 roRegisterOne_dw = roRegisterTwoValue;
195 roRegisterOne_dw.write();
213 dev.
open(
"SHDMEMDEV");
218 std::generate(processVars.
begin(), processVars.
end(), [n]()
mutable { return n++; });
222 std::this_thread::sleep_until(std::chrono::system_clock::now() + std::chrono::seconds(20));
236 dev.
open(
"SHDMEMDEV");
242 BOOST_CHECK(std::all_of(processVars.
begin(), processVars.
end(), [](
int i) { return i == 0; }));
258 BOOST_AUTO_TEST_SUITE_END()
264 static void interrupt_handler(
int signal) {
265 std::cout <<
"Caught interrupt signal (" << signal <<
"). Terminating..." << std::endl;
266 terminationCaught =
true;
std::string getUserName()
Class allows to read/write registers from device.
bool isOpened() const
Check if the device is currently opened.
void close()
Close the device.
ScalarRegisterAccessor< UserType > getScalarRegisterAccessor(const RegisterPath ®isterPathName, size_t wordOffsetInRegister=0, const AccessModeFlags &flags=AccessModeFlags({})) const
Get a ScalarRegisterObject object for the given register.
OneDRegisterAccessor< UserType > getOneDRegisterAccessor(const RegisterPath ®isterPathName, size_t numberOfWords=0, size_t wordOffsetInRegister=0, const AccessModeFlags &flags=AccessModeFlags({})) const
Get a OneDRegisterAccessor object for the given register.
void open(std::string const &aliasName)
Open a device by the given alias name from the DMAP file.
Accessor class to read and write registers transparently by using the accessor object like a vector o...
unsigned int getNElements()
Return number of elements/samples in the register.
Accessor class to read and write scalar registers transparently by using the accessor object like a v...
bool write(ChimeraTK::VersionNumber versionNumber={})
Write the data to device.
void read()
Read the data from the device.
Exception thrown when a logic error has occured.
std::size_t shmDummyInstanceIdHash(const std::string &address, const std::map< std::string, std::string > ¶meters)
Generates shm dummy instanceId hash from address and parameter map, Intended for use with parseDevice...
std::string createShmName(std::size_t instanceIdHash, const std::string &mapFileName, const std::string &userName)
Generates shm dummy name from parameter hashes.
void setDMapFilePath(std::string dmapFilePath)
Set the location of the dmap file.
bool shm_exists(std::string shmName)
BOOST_AUTO_TEST_CASE(testSerialize)
BOOST_FIXTURE_TEST_CASE(testSlowReader, DeviceFixture)