From f0097d66ae54ddaff6c7131bff56cb3ef4a0058e Mon Sep 17 00:00:00 2001 From: jmcarcell Date: Wed, 9 Oct 2024 18:43:24 +0200 Subject: [PATCH] Make the service throw when asked for the same ID twice --- k4FWCore/components/UniqueIDGenSvc.cpp | 10 +++++++--- k4FWCore/components/UniqueIDGenSvc.h | 1 + test/k4FWCoreTest/CMakeLists.txt | 4 ++-- .../src/components/TestUniqueIDGenSvc.cpp | 17 +++++++++++------ .../src/components/TestUniqueIDGenSvc.h | 6 ++++-- 5 files changed, 25 insertions(+), 13 deletions(-) diff --git a/k4FWCore/components/UniqueIDGenSvc.cpp b/k4FWCore/components/UniqueIDGenSvc.cpp index b28561b1..55ff6abd 100644 --- a/k4FWCore/components/UniqueIDGenSvc.cpp +++ b/k4FWCore/components/UniqueIDGenSvc.cpp @@ -19,6 +19,7 @@ #include "UniqueIDGenSvc.h" #include +#include #include UniqueIDGenSvc::UniqueIDGenSvc(const std::string& name, ISvcLocator* svcLoc) : base_class(name, svcLoc) {} @@ -55,9 +56,12 @@ size_t UniqueIDGenSvc::getUniqueID(uint32_t evt_num, uint32_t run_num, const std std::tie(std::ignore, inserted) = m_uniqueIDs.insert(hash); } if (!inserted) { - warning() << "Event number " << evt_num << ", run number " << run_num << " and algorithm name \"" << name - << "\" have already been used. Please check the uniqueness of the event number, run number and name." - << endmsg; + error() << "Event number " << evt_num << ", run number " << run_num << " and algorithm name \"" << name + << "\" have already been used. Please check the uniqueness of the event number, run number and name." + << endmsg; + if (m_throwIfDuplicate) { + throw std::runtime_error("Duplicate event number, run number and algorithm name"); + } } return hash; diff --git a/k4FWCore/components/UniqueIDGenSvc.h b/k4FWCore/components/UniqueIDGenSvc.h index e3976fd5..ff202370 100644 --- a/k4FWCore/components/UniqueIDGenSvc.h +++ b/k4FWCore/components/UniqueIDGenSvc.h @@ -42,6 +42,7 @@ class UniqueIDGenSvc : public extends { Gaudi::Property m_seed{this, "Seed", {123456789}}; mutable std::unordered_set m_uniqueIDs; mutable std::mutex m_mutex; + Gaudi::Property m_throwIfDuplicate{this, "ThrowIfDuplicate", {true}}; }; #endif diff --git a/test/k4FWCoreTest/CMakeLists.txt b/test/k4FWCoreTest/CMakeLists.txt index 0f44272c..6d48d076 100644 --- a/test/k4FWCoreTest/CMakeLists.txt +++ b/test/k4FWCoreTest/CMakeLists.txt @@ -125,8 +125,8 @@ add_test(NAME checkKeepDropSwitch COMMAND python scripts/check_KeepDropSwitch.py ${PROJECT_BINARY_DIR}/test/k4FWCoreTest/output_k4test_exampledata_2.root) set_test_env(checkKeepDropSwitch) set_property(TEST checkKeepDropSwitch APPEND PROPERTY DEPENDS ReadExampleEventData) -add_test_with_env(TestUniqueIDGenSvc options/TestUniqueIDGenSvc.py) -add_test_with_env(TestUniqueIDGenSvcRepeated options/TestUniqueIDGenSvc.py PROPERTIES PASS_REGULAR_EXPRESSION "WARNING *Event number 4, run number 3 and algorithm name \"Some algorithm name\" have already been used. Please check the uniqueness of the event number, run number and name.") +add_test_with_env(TestUniqueIDGenSvc options/TestUniqueIDGenSvc.py -n 1) +add_test_with_env(TestUniqueIDGenSvcRepeated options/TestUniqueIDGenSvc.py -n 2 PROPERTIES PASS_REGULAR_EXPRESSION "Duplicate event number, run number and algorithm name") add_test_with_env(TestEventHeaderFiller options/createEventHeader.py) add_test_with_env(EventHeaderCheck options/runEventHeaderCheck.py PROPERTIES DEPENDS TestEventHeaderFiller) add_test(NAME TestExec WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} COMMAND python options/TestExec.py) diff --git a/test/k4FWCoreTest/src/components/TestUniqueIDGenSvc.cpp b/test/k4FWCoreTest/src/components/TestUniqueIDGenSvc.cpp index fb3c6add..bc39ab1b 100644 --- a/test/k4FWCoreTest/src/components/TestUniqueIDGenSvc.cpp +++ b/test/k4FWCoreTest/src/components/TestUniqueIDGenSvc.cpp @@ -18,6 +18,8 @@ */ #include "TestUniqueIDGenSvc.h" +#include + DECLARE_COMPONENT(TestUniqueIDGenSvc) TestUniqueIDGenSvc::TestUniqueIDGenSvc(const std::string& aName, ISvcLocator* aSvcLoc) @@ -33,16 +35,19 @@ StatusCode TestUniqueIDGenSvc::initialize() { return StatusCode::SUCCESS; } +// This is meant to run up to two times +// For the first event, check that when giving two different event numbers, the unique IDs are different +// For the second event, the service throws when trying to get the same ID twice StatusCode TestUniqueIDGenSvc::execute(const EventContext&) const { - uint evt_num = 4; - uint run_num = 3; + ++m_counter; + uint32_t evt_num = 4; + uint32_t run_num = 3 + m_counter.sum(); std::string name = "Some algorithm name"; auto uid = m_service->getUniqueID(evt_num, run_num, name); - auto uid_again = m_service->getUniqueID(evt_num, run_num, name); - - if (uid != uid_again) { - return StatusCode::FAILURE; + auto uid_again = m_service->getUniqueID(evt_num + (m_counter.sum() % 2), run_num, name); + if (uid == uid_again) { + throw std::runtime_error("Unique IDs are the same"); } return StatusCode::SUCCESS; diff --git a/test/k4FWCoreTest/src/components/TestUniqueIDGenSvc.h b/test/k4FWCoreTest/src/components/TestUniqueIDGenSvc.h index b0bfa656..5658c6b7 100644 --- a/test/k4FWCoreTest/src/components/TestUniqueIDGenSvc.h +++ b/test/k4FWCoreTest/src/components/TestUniqueIDGenSvc.h @@ -20,7 +20,8 @@ #define TEST_UNIQUEIDGENSVC_H // GAUDI -#include +#include "Gaudi/Accumulators.h" +#include "Gaudi/Algorithm.h" #include "k4Interface/IUniqueIDGenSvc.h" @@ -37,7 +38,8 @@ class TestUniqueIDGenSvc : public Gaudi::Algorithm { StatusCode execute(const EventContext&) const final; private: - SmartIF m_service; + SmartIF m_service; + mutable Gaudi::Accumulators::Counter<> m_counter{this, "EventCounter"}; }; #endif // TEST_UNIQUEIDGENSVC_H