ChimeraTK-DeviceAccess-DoocsBackend  01.09.02
testCaching.cpp
Go to the documentation of this file.
1 #define BOOST_TEST_MODULE testDoocsBackend
2 #include <ChimeraTK/Device.h>
3 
4 #include <boost/filesystem.hpp>
5 #include <boost/test/included/unit_test.hpp>
6 
7 #include <fstream>
8 
9 /**********************************************************************************************************************/
10 
11 static std::string cacheFile = "cache-" + boost::filesystem::unique_path().string() + ".xml";
12 
13 static void deleteFile(const std::string& filename) {
14  std::string command = "rm " + filename;
15  if(std::system(command.c_str()) != 0) {
16  throw std::runtime_error(command + "failed");
17  }
18 }
19 
20 /**********************************************************************************************************************/
21 
23  std::cout << "generateCacheFile: " << cacheFile << std::endl;
24  // create xml cache file
25  std::string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
26  "<catalogue version=\"1.0\">"
27  " <register>\n"
28  " <name>/DUMMY</name>\n"
29  " <length>1</length>\n"
30  " <access_mode></access_mode>\n"
31  " <doocs_type_id>1</doocs_type_id>\n" // DATA_TEXT
32  " <!--doocs id: INT-->\n"
33  " </register>\n"
34  "</catalogue>\n";
35  std::ofstream o(cacheFile);
36  o << xml;
37  o.close();
38 }
39 
40 /**********************************************************************************************************************/
41 
42 BOOST_AUTO_TEST_CASE(testCacheReading) {
44 
45  {
46  // make device pick the xml we have just created
47  std::string address = "(doocs:doocs://localhost:212/F/D/L?cacheFile=" + cacheFile + ")";
48  auto d = ChimeraTK::Device(address);
49  auto catalogue = d.getRegisterCatalogue();
50 
51  BOOST_CHECK(catalogue.hasRegister("/DUMMY"));
52  } // d writes cache file on destruction; pick this up for cleanup
53 
54  deleteFile(cacheFile);
55 }
56 
57 /**********************************************************************************************************************/
58 
59 BOOST_AUTO_TEST_CASE(testGetAccessorWhileClosed) {
61 
62  {
63  // make device pick the xml we have just created
64  std::string address = "(doocs:doocs://localhost:212/F/D/L?cacheFile=" + cacheFile + ")";
65  auto d = ChimeraTK::Device(address);
66  auto catalogue = d.getRegisterCatalogue();
67 
68  BOOST_CHECK(catalogue.hasRegister("/DUMMY"));
69 
70  // obtain accessor
71  auto acc = d.getOneDRegisterAccessor<std::string>("DUMMY");
72  BOOST_CHECK(acc.getNElements() == 1);
73  BOOST_CHECK_THROW(acc.read(), ChimeraTK::logic_error); // device closed
74  }
75 
76  // cleanup
77  deleteFile(cacheFile);
78 }
79 
80 /**********************************************************************************************************************/
generateCacheFile
void generateCacheFile()
Definition: testCaching.cpp:22
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(testCacheReading)
Definition: testCaching.cpp:42