LCOV - code coverage report
Current view: top level - opmonlib/unittest - monitorable_object_test.cxx (source / functions) Coverage Total Hit
Test: code.result Lines: 96.1 % 77 74
Test Date: 2026-07-12 15:23:06 Functions: 93.8 % 16 15

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

Generated by: LCOV version 2.0-1