ChimeraTK-DeviceAccess  03.18.00
testRebotConnectionTimeouts.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2 // SPDX-License-Identifier: LGPL-3.0-or-later
3 
4 #define BOOST_TEST_DYN_LINK
5 #define BOOST_TEST_MODULE RebotConnectionTimeoutTest
6 
7 #include <boost/test/unit_test.hpp>
8 using namespace boost::unit_test_framework;
9 
10 #include "Device.h"
11 #include "RebotDummyServer.h"
12 #include <condition_variable>
13 
14 #include <chrono>
15 #include <thread>
16 
17 using namespace ChimeraTK;
18 
19 // Test fixture for setup and teardown
20 struct F {
21  F()
22  : rebotServer{0 /*use random port*/, "./mtcadummy_rebot.map", 1 /*protocol version*/},
23  serverThread([&]() { rebotServer.start(); }) {
24  while(not rebotServer.is_running()) {
25  std::this_thread::sleep_for(std::chrono::milliseconds(1));
26  }
27  }
28 
29  ~F() {
30  rebotServer.stop();
31  serverThread.join();
32  }
34  boost::thread serverThread;
35 };
36 
37 BOOST_FIXTURE_TEST_CASE(testOpenConnection, F) {
38  rebotServer.stop();
39 
40  uint32_t timeout_sec = 1;
41  auto accetable_completion_time = std::chrono::seconds(timeout_sec * 5);
42  Device d("(rebot?ip=localhost&port=" + std::to_string(rebotServer.port()) +
43  "&map=mtcadummy_rebot.map&timeout=" + std::to_string(timeout_sec) + ")");
44 
45  BOOST_CHECK(d.isFunctional() == false);
46 
47  auto begin = std::chrono::system_clock::now();
48  { BOOST_CHECK_THROW(d.open(), ChimeraTK::runtime_error); }
49  auto end = std::chrono::system_clock::now();
50 
51  BOOST_CHECK(d.isFunctional() == false);
52 
53  auto execution_duration = end - begin;
54  BOOST_CHECK(execution_duration < accetable_completion_time);
55 }
56 
57 BOOST_FIXTURE_TEST_CASE(testReadTimeout, F) {
58  uint32_t timeout_sec = 1;
59  auto accetable_completion_time = std::chrono::seconds(timeout_sec * 5);
60  Device d("(rebot?ip=localhost&port=" + std::to_string(rebotServer.port()) +
61  "&map=mtcadummy_rebot.map&timeout=" + std::to_string(timeout_sec) + ")");
62 
63  BOOST_CHECK(d.isFunctional() == false);
64 
65  d.open();
66 
67  BOOST_CHECK(d.isFunctional() == true);
68 
69  rebotServer.stop();
70  auto begin = std::chrono::system_clock::now();
71  { BOOST_CHECK_THROW([[maybe_unused]] auto result = d.read<int>("BOARD.WORD_USER"), ChimeraTK::runtime_error); }
72  auto end = std::chrono::system_clock::now();
73 
74  BOOST_CHECK(d.isFunctional() == false);
75 
76  auto execution_duration = end - begin;
77  BOOST_CHECK(execution_duration < accetable_completion_time);
78 }
79 
80 BOOST_FIXTURE_TEST_CASE(testWriteTimeout, F) {
81  uint32_t timeout_sec = 1;
82  auto accetable_completion_time = std::chrono::seconds(timeout_sec * 5);
83  Device d("(rebot?ip=localhost&port=" + std::to_string(rebotServer.port()) +
84  "&map=mtcadummy_rebot.map&timeout=" + std::to_string(timeout_sec) + ")");
85  BOOST_CHECK(d.isFunctional() == false);
86  d.open();
87 
88  BOOST_CHECK(d.isFunctional() == true);
89 
90  rebotServer.stop();
91  auto begin = std::chrono::system_clock::now();
92  { BOOST_CHECK_THROW(d.write("BOARD.WORD_USER", 42), ChimeraTK::runtime_error); }
93  auto end = std::chrono::system_clock::now();
94 
95  BOOST_CHECK(d.isFunctional() == false);
96 
97  auto execution_duration = end - begin;
98  BOOST_CHECK(execution_duration < accetable_completion_time);
99 }
RebotDummyServer.h
ChimeraTK::Device::read
UserType read(const RegisterPath &registerPathName, const AccessModeFlags &flags=AccessModeFlags({})) const
Inefficient convenience function to read a single-word register without obtaining an accessor.
Definition: Device.h:293
ChimeraTK::testable_rebot_sleep::now
boost::chrono::steady_clock::time_point now()
Definition: testableRebotSleep.cc:7
F::~F
~F()
Definition: testRebotConnectionTimeouts.cpp:29
F::rebotServer
RebotDummyServer rebotServer
Definition: testRebotConnectionTimeouts.cpp:33
ChimeraTK::RebotDummyServer
Definition: RebotDummyServer.h:82
ChimeraTK::runtime_error
Exception thrown when a runtime error has occured.
Definition: Exception.h:18
F
Definition: testRebotConnectionTimeouts.cpp:20
BOOST_FIXTURE_TEST_CASE
BOOST_FIXTURE_TEST_CASE(testOpenConnection, F)
Definition: testRebotConnectionTimeouts.cpp:37
F::F
F()
Definition: testRebotConnectionTimeouts.cpp:21
Device.h
F::serverThread
boost::thread serverThread
Definition: testRebotConnectionTimeouts.cpp:34
ChimeraTK::Device
Class allows to read/write registers from device.
Definition: Device.h:39
ChimeraTK::Device::open
void open(std::string const &aliasName)
Open a device by the given alias name from the DMAP file.
Definition: Device.cc:58
ChimeraTK::Device::write
void write(const RegisterPath &registerPathName, UserType value, const AccessModeFlags &flags=AccessModeFlags({}))
Inefficient convenience function to write a single-word register without obtaining an accessor.
Definition: Device.h:314
ChimeraTK::Device::isFunctional
bool isFunctional() const
Return wether a device is working as intended, usually this means it is opened and does not have any ...
Definition: Device.cc:82
ChimeraTK
Definition: DummyBackend.h:16
ChimeraTK::to_string
std::string to_string(Boolean &value)
Definition: SupportedUserTypes.h:59