ChimeraTK-DeviceAccess 03.25.00
Loading...
Searching...
No Matches
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>
8using namespace boost::unit_test_framework;
9
10#include "Device.h"
11#include "RebotDummyServer.h"
12
13#include <chrono>
14#include <condition_variable>
15#include <thread>
16
17using namespace ChimeraTK;
18
19// Test fixture for setup and teardown
20struct 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() {
31 serverThread.join();
32 }
34 boost::thread serverThread;
35};
36
37BOOST_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 {
49 BOOST_CHECK_THROW(d.open(), ChimeraTK::runtime_error);
50 }
51 auto end = std::chrono::system_clock::now();
52
53 BOOST_CHECK(d.isFunctional() == false);
54
55 auto execution_duration = end - begin;
56 BOOST_CHECK(execution_duration < accetable_completion_time);
57}
58
59BOOST_FIXTURE_TEST_CASE(testReadTimeout, F) {
60 uint32_t timeout_sec = 1;
61 auto accetable_completion_time = std::chrono::seconds(timeout_sec * 5);
62 Device d("(rebot?ip=localhost&port=" + std::to_string(rebotServer.port()) +
63 "&map=mtcadummy_rebot.map&timeout=" + std::to_string(timeout_sec) + ")");
64
65 BOOST_CHECK(d.isFunctional() == false);
66
67 d.open();
68
69 BOOST_CHECK(d.isFunctional() == true);
70
71 rebotServer.stop();
72 auto begin = std::chrono::system_clock::now();
73 {
74 BOOST_CHECK_THROW([[maybe_unused]] auto result = d.read<int>("BOARD.WORD_USER"), ChimeraTK::runtime_error);
75 }
76 auto end = std::chrono::system_clock::now();
77
78 BOOST_CHECK(d.isFunctional() == false);
79
80 auto execution_duration = end - begin;
81 BOOST_CHECK(execution_duration < accetable_completion_time);
82}
83
84BOOST_FIXTURE_TEST_CASE(testWriteTimeout, F) {
85 uint32_t timeout_sec = 1;
86 auto accetable_completion_time = std::chrono::seconds(timeout_sec * 5);
87 Device d("(rebot?ip=localhost&port=" + std::to_string(rebotServer.port()) +
88 "&map=mtcadummy_rebot.map&timeout=" + std::to_string(timeout_sec) + ")");
89 BOOST_CHECK(d.isFunctional() == false);
90 d.open();
91
92 BOOST_CHECK(d.isFunctional() == true);
93
94 rebotServer.stop();
95 auto begin = std::chrono::system_clock::now();
96 {
97 BOOST_CHECK_THROW(d.write("BOARD.WORD_USER", 42), ChimeraTK::runtime_error);
98 }
99 auto end = std::chrono::system_clock::now();
100
101 BOOST_CHECK(d.isFunctional() == false);
102
103 auto execution_duration = end - begin;
104 BOOST_CHECK(execution_duration < accetable_completion_time);
105}
Class allows to read/write registers from device.
Definition Device.h:39
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
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:296
void open(std::string const &aliasName)
Open a device by the given alias name from the DMAP file.
Definition Device.cc:58
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:317
Exception thrown when a runtime error has occured.
Definition Exception.h:18
std::string to_string(const std::string &v)
RebotDummyServer rebotServer
boost::thread serverThread
BOOST_FIXTURE_TEST_CASE(testOpenConnection, F)