LCOV - code coverage report
Current view: top level - appfwk/unittest - DAQModule_test.cxx (source / functions) Coverage Total Hit
Test: code.result Lines: 89.4 % 66 59
Test Date: 2025-12-21 13:07:08 Functions: 70.8 % 24 17

            Line data    Source code
       1              : /**
       2              :  * @file DAQModule_test.cxx DAQModule 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 "appfwk/ConfigurationManager.hpp"
      10              : #include "appfwk/DAQModule.hpp"
      11              : 
      12              : #define BOOST_TEST_MODULE DAQModule_test // NOLINT
      13              : 
      14              : #include "boost/test/unit_test.hpp"
      15              : #include "nlohmann/json.hpp"
      16              : 
      17              : #include <set>
      18              : #include <string>
      19              : #include <vector>
      20              : 
      21              : constexpr auto queue_timeout = std::chrono::milliseconds(10);
      22              : using namespace dunedaq::appfwk;
      23              : 
      24              : BOOST_AUTO_TEST_SUITE(DAQModule_test)
      25              : 
      26              : namespace daqmoduletest {
      27              : class BadDAQModule : public DAQModule
      28              : {
      29              : public:
      30            1 :   explicit BadDAQModule(std::string const& name)
      31            1 :     : DAQModule(name)
      32              :   {
      33            1 :     register_command("stuff", &BadDAQModule::do_stuff);
      34              : 
      35              :     // THIS WILL FAIL
      36            1 :     register_command("stuff", &BadDAQModule::do_other_stuff);
      37            1 :   }
      38              : 
      39            0 :   void init(std::shared_ptr<ConfigurationManager>) final {}
      40              : 
      41            0 :   void do_stuff(const CommandData_t& /*data*/) {}
      42            0 :   void do_other_stuff(const CommandData_t& /*data*/) {}
      43              : };
      44              : 
      45              : class RegisterCommandDAQModule : public DAQModule
      46              : {
      47              : public:
      48            1 :   explicit RegisterCommandDAQModule(std::string const& name)
      49            1 :     : DAQModule(name)
      50              :   {
      51            1 :     register_command("stuff", &RegisterCommandDAQModule::do_stuff);
      52            1 :   }
      53              : 
      54            0 :   void init(std::shared_ptr<ConfigurationManager>) final {}
      55              : 
      56            2 :   void try_register(std::string cmd) { register_command(cmd, &RegisterCommandDAQModule::do_stuff); }
      57              : 
      58            0 :   void do_stuff(const CommandData_t& /*data*/) {}
      59              : };
      60              : 
      61              : class GoodDAQModule : public DAQModule
      62              : {
      63              : public:
      64            2 :   explicit GoodDAQModule(std::string const& name)
      65            2 :     : DAQModule(name)
      66              :   {
      67            2 :     register_command("stuff", &GoodDAQModule::do_stuff);
      68            2 :   }
      69              : 
      70            0 :   void init(std::shared_ptr<ConfigurationManager>) final {}
      71              : 
      72            1 :   void do_stuff(const CommandData_t& /*data*/) {}
      73              : };
      74              : 
      75              : class AnyDAQModule : public DAQModule
      76              : {
      77              : public:
      78            1 :   explicit AnyDAQModule(std::string const& name)
      79            1 :     : DAQModule(name)
      80              :   {
      81            1 :     register_command("no_stuff", &AnyDAQModule::do_stuff);
      82            1 :     register_command("any_stuff", &AnyDAQModule::do_stuff);
      83            1 :     register_command("any_stuff_oops", &AnyDAQModule::do_stuff);
      84            1 :   }
      85              : 
      86            0 :   void init(std::shared_ptr<ConfigurationManager>) final {}
      87              : 
      88            6 :   void do_stuff(const CommandData_t& /*data*/) {}
      89              : };
      90              : } // namespace daqmoduletest
      91              : 
      92            2 : BOOST_AUTO_TEST_CASE(Construct)
      93              : {
      94            1 :   daqmoduletest::GoodDAQModule gdm("construct_test_good");
      95            3 :   BOOST_REQUIRE_THROW(daqmoduletest::BadDAQModule bdm("construct_test_bad"), CommandRegistrationFailedMessage);
      96            1 : }
      97              : 
      98            2 : BOOST_AUTO_TEST_CASE(Commands)
      99              : {
     100            1 :   daqmoduletest::GoodDAQModule gdm("command_test");
     101              : 
     102            1 :   BOOST_REQUIRE(gdm.has_command("stuff"));
     103            1 :   auto valid_commands = gdm.get_commands();
     104            1 :   BOOST_REQUIRE_EQUAL(valid_commands.size(), 1);
     105            1 :   BOOST_REQUIRE_EQUAL(valid_commands[0], "stuff");
     106              : 
     107            1 :   gdm.execute_command("stuff", {});
     108            5 :   BOOST_REQUIRE_THROW(gdm.execute_command("other_stuff", {}), UnknownCommand);
     109              : 
     110            1 :   daqmoduletest::AnyDAQModule adm("command_test");
     111            1 :   BOOST_REQUIRE(adm.has_command("any_stuff"));
     112            1 :   BOOST_REQUIRE(adm.has_command("no_stuff"));
     113            1 :   BOOST_REQUIRE(adm.has_command("any_stuff_oops"));
     114            1 :   valid_commands = adm.get_commands();
     115            1 :   BOOST_REQUIRE_EQUAL(valid_commands.size(), 3);
     116              : 
     117            1 :   adm.execute_command("any_stuff", {});
     118            1 :   adm.execute_command("any_stuff", {});
     119            1 :   adm.execute_command("no_stuff", {});
     120            1 :   adm.execute_command("no_stuff", {});
     121            1 :   adm.execute_command("any_stuff_oops", {});
     122            1 :   adm.execute_command("any_stuff_oops", {});
     123            9 : }
     124              : 
     125            2 : BOOST_AUTO_TEST_CASE(MakeModule)
     126              : {
     127            5 :   BOOST_REQUIRE_EXCEPTION(make_module("not_a_real_plugin_name", "error_test"),
     128              :                           DAQModuleCreationFailed,
     129              :                           [&](DAQModuleCreationFailed) { return true; });
     130            1 : }
     131              : 
     132            2 : BOOST_AUTO_TEST_CASE(RegisterCommand)
     133              : {
     134            1 :   daqmoduletest::RegisterCommandDAQModule rdm("register_command_test");
     135              : 
     136              :   // This is allowed
     137            1 :   rdm.try_register("before_inhibit");
     138            1 :   rdm.set_command_registration_allowed(false);
     139            4 :   BOOST_REQUIRE_EXCEPTION(rdm.try_register("after_inhibit"),
     140              :                           CommandRegistrationFailedMessage,
     141              :                           [&](CommandRegistrationFailedMessage) { return true; });
     142            1 :   BOOST_REQUIRE(rdm.has_command("stuff"));
     143            1 :   BOOST_REQUIRE(rdm.has_command("before_inhibit"));
     144            1 :   BOOST_REQUIRE(!rdm.has_command("after_inhibit"));
     145            1 : }
     146              : 
     147              : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1