Line data Source code
1 : /**
2 : * @file moniotorable_object_test.cxx Test application that tests MonitorableObject.
3 : *
4 : * This is part of the DUNE DAQ Application Framework, copyright 2020.
5 : * Licensing/copyright details are in the COPYING file that you should have
6 : * received with this code.
7 : */
8 :
9 : #include "confmodel/OpMonConf.hpp"
10 :
11 : #include "opmonlib/MonitorableObject.hpp"
12 : #include "opmonlib/TestOpMonManager.hpp"
13 : #include "opmonlib/opmon/test.pb.h"
14 :
15 : #define BOOST_TEST_MODULE monitorable_object_test // NOLINT
16 :
17 : #include "boost/test/unit_test.hpp"
18 :
19 : #include <type_traits>
20 : #include <vector>
21 :
22 : using namespace dunedaq::opmonlib;
23 : using namespace dunedaq::opmon;
24 :
25 : BOOST_AUTO_TEST_SUITE(Monitorable_Object_Test)
26 :
27 : struct my_fixture
28 : {
29 :
30 : class TestObject : public MonitorableObject
31 : {
32 :
33 : public:
34 : using MonitorableObject::publish;
35 : using MonitorableObject::register_node;
36 8 : TestObject()
37 8 : : MonitorableObject()
38 : {
39 8 : ;
40 8 : }
41 : };
42 :
43 6 : my_fixture()
44 6 : : manager("test", "manager")
45 6 : , node_p(new TestObject)
46 : {
47 6 : ;
48 6 : }
49 :
50 : TestOpMonManager manager;
51 : std::shared_ptr<TestObject> node_p;
52 : };
53 :
54 1 : BOOST_AUTO_TEST_CASE(pointer_casting)
55 : {
56 :
57 0 : static_assert(std::is_convertible_v<std::shared_ptr<OpMonLink>, std::shared_ptr<MonitorableObject>>);
58 0 : static_assert(!std::is_convertible_v<std::shared_ptr<OpMonManager>, std::shared_ptr<MonitorableObject>>);
59 0 : }
60 :
61 2 : BOOST_FIXTURE_TEST_CASE(test_manager, my_fixture)
62 : {
63 :
64 1 : auto facility = manager.get_backend_facility();
65 1 : BOOST_CHECK_EQUAL(bool(facility), true);
66 :
67 1 : std::shared_ptr<TestObject> child(new TestObject);
68 1 : manager.register_node("grand_child", child);
69 :
70 1 : dunedaq::opmon::TestInfo ct;
71 1 : ct.set_string_example("child_test");
72 1 : ct.set_int_example(10);
73 :
74 1 : child->publish(std::move(ct));
75 :
76 1 : auto list = facility->get_entries();
77 1 : BOOST_CHECK_EQUAL(list.size(), 1);
78 1 : }
79 :
80 2 : BOOST_FIXTURE_TEST_CASE(opmon_ids, my_fixture)
81 : {
82 :
83 1 : BOOST_CHECK_EQUAL(to_string(node_p->get_opmon_id()), "");
84 1 : BOOST_CHECK_EQUAL(to_string(manager.get_opmon_id()), "test.manager");
85 :
86 1 : manager.register_node("child", node_p);
87 1 : BOOST_CHECK_EQUAL(to_string(node_p->get_opmon_id()), "test.manager.child");
88 1 : }
89 :
90 2 : BOOST_FIXTURE_TEST_CASE(opmon_level, my_fixture)
91 : {
92 :
93 1 : manager.register_node("child", node_p);
94 1 : auto parent_level = manager.get_opmon_level();
95 1 : auto child_level = node_p->get_opmon_level();
96 :
97 1 : BOOST_CHECK_EQUAL(child_level, parent_level);
98 1 : BOOST_CHECK_EQUAL(child_level, to_level(SystemOpMonLevel::kAll));
99 :
100 1 : manager.set_opmon_level(to_level(SystemOpMonLevel::kDisabled));
101 :
102 1 : parent_level = manager.get_opmon_level();
103 1 : child_level = node_p->get_opmon_level();
104 1 : BOOST_CHECK_EQUAL(child_level, parent_level);
105 1 : BOOST_CHECK_EQUAL(child_level, to_level(SystemOpMonLevel::kDisabled));
106 1 : }
107 :
108 2 : BOOST_FIXTURE_TEST_CASE(counters, my_fixture)
109 : {
110 :
111 1 : manager.register_node("child", node_p);
112 :
113 1 : std::shared_ptr<TestObject> child(new TestObject);
114 1 : node_p->register_node("grand_child", child);
115 :
116 1 : dunedaq::opmon::TestInfo ct;
117 1 : ct.set_string_example("child_test");
118 1 : ct.set_int_example(1000);
119 :
120 1 : node_p->publish(dunedaq::opmon::TestInfo(ct));
121 :
122 1 : ct.set_int_example(2000);
123 1 : manager.set_opmon_level(to_level(SystemOpMonLevel::kDisabled));
124 1 : BOOST_CHECK_NO_THROW(child->publish(dunedaq::opmon::TestInfo(ct)));
125 :
126 1 : auto data = manager.collect();
127 1 : BOOST_CHECK_EQUAL(data.n_published_measurements(), 1);
128 1 : BOOST_CHECK_EQUAL(data.n_ignored_measurements(), 1);
129 1 : BOOST_CHECK_EQUAL(data.n_errors(), 0);
130 1 : }
131 :
132 2 : BOOST_FIXTURE_TEST_CASE(start_stop, my_fixture)
133 : {
134 :
135 : // there is no configuration, so the monitoring thread cannot start
136 1 : BOOST_CHECK_THROW(manager.start_monitoring(), dunedaq::opmonlib::MissingConfiguration);
137 :
138 1 : auto db = std::make_shared<dunedaq::conffwk::Configuration>("oksconflibs:test/config/opmon.data.xml");
139 :
140 1 : auto conf = db->get<dunedaq::confmodel::OpMonConf>("test_conf");
141 :
142 1 : manager.set_opmon_conf(conf);
143 :
144 5 : for (int i = 0; i < 4; ++i) {
145 4 : manager.start_monitoring();
146 4 : std::this_thread::sleep_for(std::chrono::seconds(2));
147 4 : BOOST_CHECK_NO_THROW(manager.stop_monitoring());
148 : }
149 :
150 : // finally let's check we can start multuiple monitoring threads and only one works
151 5 : for (int i = 0; i < 4; ++i) {
152 4 : manager.start_monitoring();
153 4 : std::this_thread::sleep_for(std::chrono::seconds(1));
154 : }
155 1 : BOOST_CHECK_NO_THROW(manager.stop_monitoring());
156 1 : }
157 :
158 2 : BOOST_FIXTURE_TEST_CASE(multiple_start, my_fixture)
159 : {
160 :
161 1 : auto db = std::make_shared<dunedaq::conffwk::Configuration>("oksconflibs:test/config/opmon.data.xml");
162 :
163 1 : auto conf = db->get<dunedaq::confmodel::OpMonConf>("test_conf");
164 :
165 1 : manager.set_opmon_conf(conf);
166 :
167 : // what we are testing here is that the creation of a separate monitoring thread
168 : // is actually forcing the previous one to top due to the jthread correct desctructor
169 :
170 5 : for (int i = 0; i < 4; ++i) {
171 4 : manager.start_monitoring();
172 4 : std::this_thread::sleep_for(std::chrono::seconds(2));
173 : }
174 1 : BOOST_CHECK_NO_THROW(manager.stop_monitoring());
175 1 : }
176 :
177 : BOOST_AUTO_TEST_SUITE_END()
|