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/Generators/CommandableFragmentGenerator.hh"
10 #define TRACE_REQUIRE_EQUAL(l, r) \
15 TLOG(TLVL_DEBUG) << __LINE__ << ": Checking if " << #l << " (" << (l) << ") equals " << #r << " (" << (r) << ")...YES!"; \
19 TLOG(TLVL_ERROR) << __LINE__ << ": Checking if " << #l << " (" << (l) << ") equals " << #r << " (" << (r) << ")...NO!"; \
21 BOOST_REQUIRE_EQUAL((l), (r)); \
24 #define TRACE_REQUIRE(b) \
29 TLOG(TLVL_DEBUG) << __LINE__ << ": Checking if " << #b << " (" << (b) << ") is true...YES!"; \
33 TLOG(TLVL_ERROR) << __LINE__ << ": Checking if " << #b << " (" << (b) << ") is true...NO!"; \
38 namespace artdaqtest {
39 class CommandableFragmentGeneratorTest;
69 bool getNext_(artdaq::FragmentPtrs& frags)
override;
80 void start()
override;
95 void pause()
override;
136 std::atomic<size_t> fireCount_;
137 std::atomic<bool> hwFail_;
138 artdaq::Fragment::timestamp_t ts_;
139 std::atomic<bool> hw_stop_;
140 std::atomic<uint64_t> enabled_ids_;
144 : CommandableFragmentGenerator(ps)
151 metricMan->initialize(ps.get<fhicl::ParameterSet>(
"metrics", fhicl::ParameterSet()));
152 metricMan->do_start();
157 while (fireCount_ > 0)
160 for (
auto&
id : fragmentIDs())
162 if (
id < 64 && ((enabled_ids_ & (0x1 <<
id)) != 0))
164 TLOG(TLVL_DEBUG) <<
"Adding Fragment with ID " <<
id <<
", SeqID " << ev_counter() <<
", and timestamp " << ts_;
165 frags.emplace_back(
new artdaq::Fragment(ev_counter(),
id, artdaq::Fragment::FirstUserFragmentType, ts_));
185 BOOST_AUTO_TEST_SUITE(CommandableFragmentGenerator_t)
189 artdaq::configureMessageFacility(
"CommandableFragmentGenerator_t");
190 TLOG(TLVL_INFO) <<
"Simple test case BEGIN";
191 fhicl::ParameterSet ps;
192 ps.put<
int>(
"board_id", 1);
193 ps.put<
int>(
"fragment_id", 1);
196 testGen.StartCmd(1, 1, 1);
198 artdaq::FragmentPtrs fps;
199 auto sts = testGen.getNext(fps);
200 TRACE_REQUIRE_EQUAL(sts,
true);
201 TRACE_REQUIRE_EQUAL(fps.size(), 1u);
202 TRACE_REQUIRE_EQUAL(fps.front()->fragmentID(), 1);
203 TRACE_REQUIRE_EQUAL(fps.front()->timestamp(), 1);
204 TRACE_REQUIRE_EQUAL(fps.front()->sequenceID(), 1);
205 TLOG(TLVL_INFO) <<
"Simple test case END";
210 artdaq::configureMessageFacility(
"CommandableFragmentGenerator_t");
211 TLOG(TLVL_INFO) <<
"WaitForStart test case BEGIN";
212 fhicl::ParameterSet ps;
213 ps.put<
int>(
"board_id", 1);
214 ps.put<
int>(
"fragment_id", 1);
217 artdaq::FragmentPtrs fps;
218 auto sts = testGen.getNext(fps);
219 TRACE_REQUIRE_EQUAL(sts,
false);
220 TRACE_REQUIRE_EQUAL(fps.size(), 0u);
223 sts = testGen.getNext(fps);
224 TRACE_REQUIRE_EQUAL(sts,
false);
225 TRACE_REQUIRE_EQUAL(fps.size(), 0u);
227 testGen.StartCmd(2, 1, 1);
229 sts = testGen.getNext(fps);
230 TRACE_REQUIRE_EQUAL(sts,
true);
231 TRACE_REQUIRE_EQUAL(fps.size(), 1u);
232 TRACE_REQUIRE_EQUAL(fps.front()->fragmentID(), 1);
233 TRACE_REQUIRE_EQUAL(fps.front()->timestamp(), 1);
234 TRACE_REQUIRE_EQUAL(fps.front()->sequenceID(), 1);
235 TLOG(TLVL_INFO) <<
"WaitForStart test case END";
240 artdaq::configureMessageFacility(
"CommandableFragmentGenerator_t");
241 TLOG(TLVL_INFO) <<
"StateMachine test case BEGIN";
242 fhicl::ParameterSet ps;
243 ps.put<
int>(
"board_id", 1);
244 ps.put<
int>(
"fragment_id", 1);
247 artdaq::FragmentPtrs fps;
248 auto sts = testGen.getNext(fps);
249 TRACE_REQUIRE_EQUAL(sts,
false);
250 TRACE_REQUIRE_EQUAL(fps.size(), 0u);
253 sts = testGen.getNext(fps);
254 TRACE_REQUIRE_EQUAL(sts,
false);
255 TRACE_REQUIRE_EQUAL(fps.size(), 0u);
257 testGen.StartCmd(2, 1, 1);
259 sts = testGen.getNext(fps);
260 TRACE_REQUIRE_EQUAL(sts,
true);
261 TRACE_REQUIRE_EQUAL(fps.size(), 1u);
262 TRACE_REQUIRE_EQUAL(fps.front()->fragmentID(), 1);
263 TRACE_REQUIRE_EQUAL(fps.front()->timestamp(), 1);
264 TRACE_REQUIRE_EQUAL(fps.front()->sequenceID(), 1);
267 testGen.setFireCount(1);
268 testGen.PauseCmd(1, 1);
269 sts = testGen.getNext(fps);
270 TRACE_REQUIRE_EQUAL(sts,
false);
271 TRACE_REQUIRE_EQUAL(fps.size(), 0u);
273 testGen.ResumeCmd(1, 1);
275 sts = testGen.getNext(fps);
276 TRACE_REQUIRE_EQUAL(sts,
true);
277 TRACE_REQUIRE_EQUAL(fps.size(), 1u);
278 TRACE_REQUIRE_EQUAL(fps.front()->fragmentID(), 1);
279 TRACE_REQUIRE_EQUAL(fps.front()->timestamp(), 2);
280 TRACE_REQUIRE_EQUAL(fps.front()->sequenceID(), 2);
283 testGen.StopCmd(1, 1);
284 sts = testGen.getNext(fps);
285 TRACE_REQUIRE_EQUAL(sts,
false);
286 TRACE_REQUIRE_EQUAL(fps.size(), 0u);
288 testGen.StartCmd(2, 1, 1);
289 sts = testGen.getNext(fps);
290 TRACE_REQUIRE_EQUAL(sts,
true);
291 TRACE_REQUIRE_EQUAL(fps.size(), 0u);
293 testGen.setFireCount(1);
294 sts = testGen.getNext(fps);
295 TRACE_REQUIRE_EQUAL(sts,
true);
296 TRACE_REQUIRE_EQUAL(fps.size(), 1u);
297 TRACE_REQUIRE_EQUAL(fps.front()->fragmentID(), 1);
298 TRACE_REQUIRE_EQUAL(fps.front()->timestamp(), 3);
299 TRACE_REQUIRE_EQUAL(fps.front()->sequenceID(), 1);
302 TLOG(TLVL_INFO) <<
"StateMachine test case END";
307 artdaq::configureMessageFacility(
"CommandableFragmentGenerator_t");
308 TLOG(TLVL_INFO) <<
"MultipleIDs test case BEGIN";
309 fhicl::ParameterSet ps;
310 ps.put<
int>(
"board_id", 1);
311 ps.put<std::vector<int>>(
"fragment_ids", {1, 2, 3});
312 ps.put<
int>(
"generated_fragments_per_event", 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.