1 #define TRACE_NAME "CommandableFragmentGenerator_t"
3 #define BOOST_TEST_MODULE CommandableFragmentGenerator_t
4 #include <boost/test/unit_test.hpp>
6 #include "artdaq-core/Data/ContainerFragment.hh"
7 #include "artdaq-core/Data/Fragment.hh"
8 #include "artdaq/DAQrate/RequestSender.hh"
9 #include "artdaq/Generators/CommandableFragmentGenerator.hh"
11 #define TRACE_REQUIRE_EQUAL(l, r) \
16 TLOG(TLVL_DEBUG) << __LINE__ << ": Checking if " << #l << " (" << (l) << ") equals " << #r << " (" << (r) << ")...YES!"; \
20 TLOG(TLVL_ERROR) << __LINE__ << ": Checking if " << #l << " (" << (l) << ") equals " << #r << " (" << (r) << ")...NO!"; \
22 BOOST_REQUIRE_EQUAL((l), (r)); \
25 #define TRACE_REQUIRE(b) \
30 TLOG(TLVL_DEBUG) << __LINE__ << ": Checking if " << #b << " (" << (b) << ") is true...YES!"; \
34 TLOG(TLVL_ERROR) << __LINE__ << ": Checking if " << #b << " (" << (b) << ") is true...NO!"; \
39 namespace artdaqtest {
40 class CommandableFragmentGeneratorTest;
70 bool getNext_(artdaq::FragmentPtrs& frags)
override;
81 void start()
override;
96 void pause()
override;
137 std::atomic<size_t> fireCount_;
138 std::atomic<bool> hwFail_;
139 artdaq::Fragment::timestamp_t ts_;
140 std::atomic<bool> hw_stop_;
141 std::atomic<uint64_t> enabled_ids_;
145 : CommandableFragmentGenerator(ps)
152 metricMan->initialize(ps.get<fhicl::ParameterSet>(
"metrics", fhicl::ParameterSet()));
153 metricMan->do_start();
158 while (fireCount_ > 0)
161 for (
auto&
id : fragmentIDs())
163 if (
id < 64 && ((enabled_ids_ & (0x1 <<
id)) != 0))
165 TLOG(TLVL_DEBUG) <<
"Adding Fragment with ID " <<
id <<
", SeqID " << ev_counter() <<
", and timestamp " << ts_;
166 frags.emplace_back(
new artdaq::Fragment(ev_counter(),
id, artdaq::Fragment::FirstUserFragmentType, ts_));
186 BOOST_AUTO_TEST_SUITE(CommandableFragmentGenerator_t)
190 artdaq::configureMessageFacility(
"CommandableFragmentGenerator_t");
191 TLOG(TLVL_INFO) <<
"Simple test case BEGIN";
192 fhicl::ParameterSet ps;
193 ps.put<
int>(
"board_id", 1);
194 ps.put<
int>(
"fragment_id", 1);
197 testGen.StartCmd(1, 1, 1);
199 artdaq::FragmentPtrs fps;
200 auto sts = testGen.getNext(fps);
201 TRACE_REQUIRE_EQUAL(sts,
true);
202 TRACE_REQUIRE_EQUAL(fps.size(), 1u);
203 TRACE_REQUIRE_EQUAL(fps.front()->fragmentID(), 1);
204 TRACE_REQUIRE_EQUAL(fps.front()->timestamp(), 1);
205 TRACE_REQUIRE_EQUAL(fps.front()->sequenceID(), 1);
206 TLOG(TLVL_INFO) <<
"Simple test case END";
211 artdaq::configureMessageFacility(
"CommandableFragmentGenerator_t");
212 TLOG(TLVL_INFO) <<
"WaitForStart test case BEGIN";
213 fhicl::ParameterSet ps;
214 ps.put<
int>(
"board_id", 1);
215 ps.put<
int>(
"fragment_id", 1);
218 artdaq::FragmentPtrs fps;
219 auto sts = testGen.getNext(fps);
220 TRACE_REQUIRE_EQUAL(sts,
false);
221 TRACE_REQUIRE_EQUAL(fps.size(), 0u);
224 sts = testGen.getNext(fps);
225 TRACE_REQUIRE_EQUAL(sts,
false);
226 TRACE_REQUIRE_EQUAL(fps.size(), 0u);
228 testGen.StartCmd(2, 1, 1);
230 sts = testGen.getNext(fps);
231 TRACE_REQUIRE_EQUAL(sts,
true);
232 TRACE_REQUIRE_EQUAL(fps.size(), 1u);
233 TRACE_REQUIRE_EQUAL(fps.front()->fragmentID(), 1);
234 TRACE_REQUIRE_EQUAL(fps.front()->timestamp(), 1);
235 TRACE_REQUIRE_EQUAL(fps.front()->sequenceID(), 1);
236 TLOG(TLVL_INFO) <<
"WaitForStart test case END";
241 artdaq::configureMessageFacility(
"CommandableFragmentGenerator_t");
242 TLOG(TLVL_INFO) <<
"StateMachine test case BEGIN";
243 fhicl::ParameterSet ps;
244 ps.put<
int>(
"board_id", 1);
245 ps.put<
int>(
"fragment_id", 1);
248 artdaq::FragmentPtrs fps;
249 auto sts = testGen.getNext(fps);
250 TRACE_REQUIRE_EQUAL(sts,
false);
251 TRACE_REQUIRE_EQUAL(fps.size(), 0u);
254 sts = testGen.getNext(fps);
255 TRACE_REQUIRE_EQUAL(sts,
false);
256 TRACE_REQUIRE_EQUAL(fps.size(), 0u);
258 testGen.StartCmd(2, 1, 1);
260 sts = testGen.getNext(fps);
261 TRACE_REQUIRE_EQUAL(sts,
true);
262 TRACE_REQUIRE_EQUAL(fps.size(), 1u);
263 TRACE_REQUIRE_EQUAL(fps.front()->fragmentID(), 1);
264 TRACE_REQUIRE_EQUAL(fps.front()->timestamp(), 1);
265 TRACE_REQUIRE_EQUAL(fps.front()->sequenceID(), 1);
268 testGen.setFireCount(1);
269 testGen.PauseCmd(1, 1);
270 sts = testGen.getNext(fps);
271 TRACE_REQUIRE_EQUAL(sts,
false);
272 TRACE_REQUIRE_EQUAL(fps.size(), 0u);
274 testGen.ResumeCmd(1, 1);
276 sts = testGen.getNext(fps);
277 TRACE_REQUIRE_EQUAL(sts,
true);
278 TRACE_REQUIRE_EQUAL(fps.size(), 1u);
279 TRACE_REQUIRE_EQUAL(fps.front()->fragmentID(), 1);
280 TRACE_REQUIRE_EQUAL(fps.front()->timestamp(), 2);
281 TRACE_REQUIRE_EQUAL(fps.front()->sequenceID(), 2);
284 testGen.StopCmd(1, 1);
285 sts = testGen.getNext(fps);
286 TRACE_REQUIRE_EQUAL(sts,
false);
287 TRACE_REQUIRE_EQUAL(fps.size(), 0u);
289 testGen.StartCmd(2, 1, 1);
290 sts = testGen.getNext(fps);
291 TRACE_REQUIRE_EQUAL(sts,
true);
292 TRACE_REQUIRE_EQUAL(fps.size(), 0u);
294 testGen.setFireCount(1);
295 sts = testGen.getNext(fps);
296 TRACE_REQUIRE_EQUAL(sts,
true);
297 TRACE_REQUIRE_EQUAL(fps.size(), 1u);
298 TRACE_REQUIRE_EQUAL(fps.front()->fragmentID(), 1);
299 TRACE_REQUIRE_EQUAL(fps.front()->timestamp(), 3);
300 TRACE_REQUIRE_EQUAL(fps.front()->sequenceID(), 1);
303 TLOG(TLVL_INFO) <<
"StateMachine test case END";
308 artdaq::configureMessageFacility(
"CommandableFragmentGenerator_t");
309 TLOG(TLVL_INFO) <<
"MultipleIDs test case BEGIN";
310 fhicl::ParameterSet ps;
311 ps.put<
int>(
"board_id", 1);
312 ps.put<std::vector<int>>(
"fragment_ids", {1, 2, 3});
315 testGen.StartCmd(3, 1, 1);
317 artdaq::FragmentPtrs fps;
318 auto sts = testGen.getNext(fps);
320 std::map<artdaq::Fragment::fragment_id_t, size_t> ids;
321 TRACE_REQUIRE_EQUAL(sts,
true);
322 TRACE_REQUIRE_EQUAL(fps.size(), 3u);
323 while (fps.size() > 0)
325 ids[fps.front()->fragmentID()]++;
326 TRACE_REQUIRE_EQUAL(fps.front()->timestamp(), 1);
327 TRACE_REQUIRE_EQUAL(fps.front()->sequenceID(), 1);
331 TRACE_REQUIRE_EQUAL(ids[1], 1);
332 TRACE_REQUIRE_EQUAL(ids[2], 1);
333 TRACE_REQUIRE_EQUAL(ids[3], 1);
338 testGen.setEnabledIds(0x6);
339 testGen.setFireCount(1);
341 sts = testGen.getNext(fps);
343 TRACE_REQUIRE_EQUAL(sts,
true);
344 TRACE_REQUIRE_EQUAL(fps.size(), 2u);
347 ids[fps.front()->fragmentID()]++;
348 TRACE_REQUIRE_EQUAL(fps.front()->timestamp(), 2);
349 TRACE_REQUIRE_EQUAL(fps.front()->sequenceID(), 2);
352 TRACE_REQUIRE_EQUAL(ids[1], 1);
353 TRACE_REQUIRE_EQUAL(ids[2], 1);
354 TRACE_REQUIRE_EQUAL(ids[3], 0);
356 TLOG(TLVL_INFO) <<
"MultipleIDs test case END";
361 artdaq::configureMessageFacility(
"CommandableFragmentGenerator_t");
362 TLOG(TLVL_INFO) <<
"HardwareFailure_NonThreaded test case BEGIN";
363 fhicl::ParameterSet ps;
364 ps.put<
int>(
"board_id", 1);
365 ps.put<
int>(
"fragment_id", 1);
366 ps.put<
bool>(
"separate_data_thread",
false);
367 ps.put<
bool>(
"separate_monitoring_thread",
false);
368 ps.put<int64_t>(
"hardware_poll_interval_us", 10);
370 auto buffer = std::make_shared<artdaq::RequestBuffer>();
371 buffer->setRunning(
true);
376 artdaq::FragmentPtrs fps;
378 TRACE_REQUIRE_EQUAL(sts,
true);
379 TRACE_REQUIRE_EQUAL(fps.size(), 1u);
380 TRACE_REQUIRE_EQUAL(fps.front()->fragmentID(), 1);
381 TRACE_REQUIRE_EQUAL(fps.front()->timestamp(), 1);
382 TRACE_REQUIRE_EQUAL(fps.front()->sequenceID(), 1);
389 TRACE_REQUIRE_EQUAL(sts,
false);
390 TRACE_REQUIRE(fps.empty());
394 TLOG(TLVL_INFO) <<
"HardwareFailure_NonThreaded test case END";
399 artdaq::configureMessageFacility(
"CommandableFragmentGenerator_t");
400 TLOG(TLVL_INFO) <<
"HardwareFailure_Threaded test case BEGIN";
401 fhicl::ParameterSet ps;
402 ps.put<
int>(
"board_id", 1);
403 ps.put<
int>(
"fragment_id", 1);
404 ps.put<
bool>(
"separate_monitoring_thread",
true);
405 ps.put<int64_t>(
"hardware_poll_interval_us", 750000);
407 auto buffer = std::make_shared<artdaq::RequestBuffer>();
408 buffer->setRunning(
true);
413 artdaq::FragmentPtrs fps;
415 TRACE_REQUIRE_EQUAL(sts,
true);
416 TRACE_REQUIRE_EQUAL(fps.size(), 1u);
417 TRACE_REQUIRE_EQUAL(fps.front()->fragmentID(), 1);
418 TRACE_REQUIRE_EQUAL(fps.front()->timestamp(), 1);
419 TRACE_REQUIRE_EQUAL(fps.front()->sequenceID(), 1);
422 TLOG(TLVL_INFO) <<
"Setting failure bit";
427 TLOG(TLVL_INFO) <<
"Checking that failure is reported by getNext";
430 TRACE_REQUIRE_EQUAL(sts,
false);
431 TRACE_REQUIRE(fps.empty());
435 TLOG(TLVL_INFO) <<
"HardwareFailure_Threaded test case END";
438 BOOST_AUTO_TEST_SUITE_END()
bool getNext_(artdaq::FragmentPtrs &frags) override
Generate data and return it to CommandableFragmentGenerator.
void setEnabledIds(uint64_t bitmask)
Set the enabled IDs mask for the Fragment Generator.
CommandableFragmentGenerator derived class for testing.
void StopCmd(uint64_t timeout, uint64_t timestamp)
Stop the CommandableFragmentGenerator.
CommandableFragmentGeneratorTest(const fhicl::ParameterSet &ps)
CommandableFragmentGeneratorTest Constructor.
void StartCmd(int run, uint64_t timeout, uint64_t timestamp)
Start the CommandableFragmentGenerator.
bool getNext(FragmentPtrs &output) overridefinal
getNext calls either applyRequests or getNext_ to get any data that is ready to be sent to the EventB...
bool checkHWStatus_() override
Returns whether the hwFail flag has not been set.
void stopNoMutex() override
Perform immediate stop actions. No-Op.
void start() override
Perform start actions. No-Op.
CommandableFragmentGenerator is a FragmentGenerator-derived abstract class that defines the interface...
void resume() override
Perform resume actions. No-Op.
void pause() override
Perform pause actions. No-Op.
void setHwFail()
Set the hwFail flag.
void setTimestamp(artdaq::Fragment::timestamp_t ts)
Set the timestamp to be used for the next Fragment.
artdaq::Fragment::timestamp_t getTimestamp()
Get the timestamp that will be used for the next Fragment.
void stop() override
Perform stop actions. No-Op.
void SetRequestBuffer(std::shared_ptr< RequestBuffer > buffer)
Set the shared_ptr to the RequestBuffer.
void joinThreads()
Join any data-taking threads. Should be called when destructing CommandableFragmentGenerator.
void setFireCount(size_t count)
Have getNext_ generate count fragments.