Line data Source code
1 : /**
2 : * @file Application_test.cxx Application class Unit Tests
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 "Application.hpp"
10 :
11 : #include "appfwk/cmd/Nljs.hpp"
12 :
13 : #include "iomanager/IOManager.hpp"
14 : #include "rcif/cmd/Nljs.hpp"
15 :
16 : #define BOOST_TEST_MODULE Application_test // NOLINT
17 :
18 : #include "boost/test/unit_test.hpp"
19 :
20 : #include <memory>
21 : #include <string>
22 : #include <type_traits>
23 :
24 : BOOST_AUTO_TEST_SUITE(Application_test)
25 :
26 : using namespace dunedaq::appfwk;
27 :
28 : const std::string TEST_JSON_FILE = // NOLINT
29 : std::string(getenv("DBT_AREA_ROOT")) + "/sourcecode/appfwk/test/scripts/test.json"; // NOLINT
30 : const std::string TEST_OKS_DB = "test/config/appSession.data.xml"; // NOLINT
31 :
32 2 : BOOST_AUTO_TEST_CASE(Constructor)
33 : {
34 1 : Application app(
35 1 : "TestApp", "the-test-session", "stdin://" + TEST_JSON_FILE, "oksconflibs:" + TEST_OKS_DB, "test-session");
36 1 : }
37 :
38 2 : BOOST_AUTO_TEST_CASE(ConstructorFailures)
39 : {
40 1 : std::shared_ptr<Application> app_ptr;
41 5 : BOOST_REQUIRE_EXCEPTION(
42 : app_ptr = std::make_shared<Application>(
43 : "TestApp", "the-test-session", "stdin://" + TEST_JSON_FILE, "oksconflibs:" + TEST_OKS_DB, "bad-session"),
44 : MissingComponent,
45 : [&](MissingComponent) { return true; });
46 5 : BOOST_REQUIRE_EXCEPTION(
47 : app_ptr = std::make_shared<Application>(
48 : "BADApp", "the-test-session", "stdin://" + TEST_JSON_FILE, "oksconflibs:" + TEST_OKS_DB, "test-session"),
49 : MissingComponent,
50 : [&](MissingComponent) { return true; });
51 1 : }
52 :
53 2 : BOOST_AUTO_TEST_CASE(Init)
54 : {
55 1 : dunedaq::get_iomanager()->reset();
56 1 : Application app(
57 1 : "TestApp", "the-test-session", "stdin://" + TEST_JSON_FILE, "oksconflibs:" + TEST_OKS_DB, "test-session");
58 1 : app.init();
59 :
60 1 : dunedaq::iomanager::IOManager::get()->reset();
61 1 : }
62 :
63 2 : BOOST_AUTO_TEST_CASE(Run)
64 : {
65 1 : std::atomic<bool> end_marker = false;
66 :
67 1 : dunedaq::get_iomanager()->reset();
68 1 : Application app(
69 1 : "TestApp", "the-test-session", "stdin://" + TEST_JSON_FILE, "oksconflibs:" + TEST_OKS_DB, "test-session");
70 :
71 3 : BOOST_REQUIRE_EXCEPTION(
72 : app.run(end_marker), ApplicationNotInitialized, [&](ApplicationNotInitialized) { return true; });
73 :
74 1 : app.init();
75 :
76 1 : app.run(end_marker);
77 :
78 1 : BOOST_REQUIRE(app.get_state() == "INITIAL");
79 1 : dunedaq::iomanager::IOManager::get()->reset();
80 1 : }
81 :
82 2 : BOOST_AUTO_TEST_CASE(Start)
83 : {
84 1 : dunedaq::get_iomanager()->reset();
85 1 : Application app(
86 1 : "TestApp", "the-test-session", "stdin://" + TEST_JSON_FILE, "oksconflibs:" + TEST_OKS_DB, "test-session");
87 1 : app.init();
88 :
89 1 : dunedaq::appfwk::cmd::CmdObj start;
90 1 : dunedaq::appfwk::cmd::AddressedCmd addr_cmd;
91 1 : dunedaq::rcif::cmd::StartParams start_params;
92 1 : start_params.run = 1010;
93 1 : nlohmann::json start_param_data;
94 1 : to_json(start_param_data, start_params);
95 :
96 1 : addr_cmd.data = start_param_data;
97 1 : addr_cmd.match = "";
98 1 : start.modules.push_back(addr_cmd);
99 1 : nlohmann::json start_data;
100 1 : to_json(start_data, start);
101 1 : dunedaq::rcif::cmd::RCCommand cmd;
102 1 : nlohmann::json cmd_data;
103 1 : cmd.id = "start";
104 1 : cmd.data = start_data;
105 1 : cmd.exit_state = "RUNNING";
106 1 : to_json(cmd_data, cmd);
107 :
108 1 : bool cmd_valid = app.check_state_for_cmd(cmd_data);
109 1 : BOOST_REQUIRE_EQUAL(cmd_valid, true);
110 1 : app.execute(cmd_data);
111 1 : dunedaq::iomanager::IOManager::get()->reset();
112 1 : }
113 2 : BOOST_AUTO_TEST_CASE(Stop)
114 : {
115 1 : dunedaq::get_iomanager()->reset();
116 1 : Application app(
117 1 : "TestApp", "the-test-session", "stdin://" + TEST_JSON_FILE, "oksconflibs:" + TEST_OKS_DB, "test-session");
118 1 : app.init();
119 :
120 1 : dunedaq::rcif::cmd::StartParams start_params;
121 1 : start_params.run = 1010;
122 1 : nlohmann::json start_param_data;
123 1 : to_json(start_param_data, start_params);
124 :
125 1 : dunedaq::appfwk::cmd::AddressedCmd addr_cmd;
126 1 : addr_cmd.data = start_param_data;
127 1 : addr_cmd.match = "";
128 :
129 1 : dunedaq::appfwk::cmd::CmdObj start;
130 1 : start.modules.push_back(addr_cmd);
131 1 : nlohmann::json start_data;
132 1 : to_json(start_data, start);
133 :
134 1 : dunedaq::rcif::cmd::RCCommand cmd;
135 1 : nlohmann::json cmd_data;
136 :
137 1 : cmd.id = "start";
138 1 : cmd.data = start_data;
139 1 : cmd.exit_state = "RUNNING";
140 1 : to_json(cmd_data, cmd);
141 :
142 1 : bool cmd_valid = app.check_state_for_cmd(cmd_data);
143 1 : BOOST_REQUIRE_EQUAL(cmd_valid, true);
144 1 : app.execute(cmd_data);
145 :
146 1 : dunedaq::appfwk::cmd::CmdObj stop;
147 1 : nlohmann::json stop_data;
148 1 : to_json(stop_data, stop);
149 1 : cmd.id = "stop";
150 1 : cmd.data = start_data;
151 1 : cmd.exit_state = "CONFIGURED";
152 1 : to_json(cmd_data, cmd);
153 :
154 1 : cmd_valid = app.check_state_for_cmd(cmd_data);
155 1 : BOOST_REQUIRE_EQUAL(cmd_valid, true);
156 1 : app.execute(cmd_data);
157 1 : dunedaq::iomanager::IOManager::get()->reset();
158 1 : }
159 :
160 2 : BOOST_AUTO_TEST_CASE(NotInitialized)
161 : {
162 1 : dunedaq::get_iomanager()->reset();
163 2 : Application app(
164 1 : "TestApp", "the-test-session", "stdin://" + TEST_JSON_FILE, "oksconflibs:" + TEST_OKS_DB, "test-session");
165 1 : dunedaq::rcif::cmd::RCCommand cmd;
166 1 : nlohmann::json cmd_data;
167 :
168 1 : dunedaq::appfwk::cmd::CmdObj start;
169 1 : nlohmann::json start_data;
170 1 : to_json(start_data, start);
171 1 : cmd.id = "start";
172 1 : cmd.data = start_data;
173 1 : cmd.exit_state = "RUNNING";
174 1 : to_json(cmd_data, cmd);
175 :
176 1 : bool cmd_valid = app.check_state_for_cmd(cmd_data);
177 1 : BOOST_REQUIRE_EQUAL(cmd_valid, true);
178 :
179 3 : BOOST_REQUIRE_EXCEPTION(
180 : app.execute(cmd_data), DAQModuleManagerNotInitialized, [&](DAQModuleManagerNotInitialized) { return true; });
181 :
182 1 : cmd_valid = app.check_state_for_cmd(cmd_data);
183 1 : BOOST_REQUIRE_EQUAL(cmd_valid, false);
184 1 : dunedaq::iomanager::IOManager::get()->reset();
185 1 : }
186 :
187 2 : BOOST_AUTO_TEST_CASE(CommandStateTest)
188 : {
189 :
190 1 : dunedaq::get_iomanager()->reset();
191 1 : Application app(
192 1 : "TestApp", "the-test-session", "stdin://" + TEST_JSON_FILE, "oksconflibs:" + TEST_OKS_DB, "test-session");
193 1 : app.init();
194 :
195 1 : dunedaq::rcif::cmd::RCCommand cmd;
196 1 : cmd.id = "badCommand";
197 1 : cmd.entry_state = "NonexistentState";
198 1 : nlohmann::json cmd_data;
199 1 : to_json(cmd_data, cmd);
200 :
201 1 : bool cmd_valid = app.check_state_for_cmd(cmd_data);
202 1 : BOOST_REQUIRE_EQUAL(cmd_valid, false);
203 :
204 3 : BOOST_REQUIRE_EXCEPTION(app.execute(cmd_data), InvalidStateForCommand, [&](InvalidStateForCommand) { return true; });
205 :
206 1 : cmd_valid = app.check_state_for_cmd(cmd_data);
207 1 : BOOST_REQUIRE_EQUAL(cmd_valid, false);
208 1 : dunedaq::iomanager::IOManager::get()->reset();
209 1 : }
210 :
211 2 : BOOST_AUTO_TEST_CASE(CommandThrowsException)
212 : {
213 1 : dunedaq::get_iomanager()->reset();
214 1 : Application app(
215 1 : "TestApp", "the-test-session", "stdin://" + TEST_JSON_FILE, "oksconflibs:" + TEST_OKS_DB, "test-session");
216 1 : app.init();
217 :
218 1 : dunedaq::rcif::cmd::RCCommand cmd;
219 1 : nlohmann::json cmd_data;
220 :
221 1 : dunedaq::appfwk::cmd::AddressedCmd addr_cmd;
222 1 : addr_cmd.match = "dummy_module_0";
223 :
224 1 : dunedaq::appfwk::cmd::CmdObj cmd_obj;
225 1 : cmd_obj.modules.push_back(addr_cmd);
226 1 : nlohmann::json cmd_obj_data;
227 1 : to_json(cmd_obj_data, cmd_obj);
228 :
229 1 : cmd.id = "stuff";
230 1 : cmd.data = cmd_obj_data;
231 1 : to_json(cmd_data, cmd);
232 1 : app.execute(cmd_data);
233 :
234 1 : cmd.id = "bad_stuff";
235 1 : to_json(cmd_data, cmd);
236 3 : BOOST_REQUIRE_EXCEPTION(
237 : app.execute(cmd_data), CommandDispatchingFailed, [&](CommandDispatchingFailed) { return true; });
238 1 : dunedaq::iomanager::IOManager::get()->reset();
239 1 : }
240 :
241 2 : BOOST_AUTO_TEST_CASE(State)
242 : {
243 1 : dunedaq::get_iomanager()->reset();
244 1 : Application app(
245 1 : "TestApp", "the-test-session", "stdin://" + TEST_JSON_FILE, "oksconflibs:" + TEST_OKS_DB, "test-session");
246 :
247 1 : std::string state_in = "state";
248 1 : app.set_state(state_in);
249 1 : std::string state = app.get_state();
250 1 : BOOST_REQUIRE_EQUAL(state_in, state);
251 1 : dunedaq::iomanager::IOManager::get()->reset();
252 1 : }
253 :
254 : BOOST_AUTO_TEST_SUITE_END()
|