Skip to content

Commit

Permalink
Make the service throw when asked for the same ID twice
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcarcell committed Oct 9, 2024
1 parent c8fb1e2 commit f0097d6
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
10 changes: 7 additions & 3 deletions k4FWCore/components/UniqueIDGenSvc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "UniqueIDGenSvc.h"

#include <cstdint>
#include <stdexcept>
#include <string>

UniqueIDGenSvc::UniqueIDGenSvc(const std::string& name, ISvcLocator* svcLoc) : base_class(name, svcLoc) {}
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions k4FWCore/components/UniqueIDGenSvc.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class UniqueIDGenSvc : public extends<Service, IUniqueIDGenSvc> {
Gaudi::Property<uint64_t> m_seed{this, "Seed", {123456789}};
mutable std::unordered_set<size_t, std::identity> m_uniqueIDs;
mutable std::mutex m_mutex;
Gaudi::Property<bool> m_throwIfDuplicate{this, "ThrowIfDuplicate", {true}};
};

#endif
4 changes: 2 additions & 2 deletions test/k4FWCoreTest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 11 additions & 6 deletions test/k4FWCoreTest/src/components/TestUniqueIDGenSvc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
#include "TestUniqueIDGenSvc.h"

#include <cstdint>

DECLARE_COMPONENT(TestUniqueIDGenSvc)

TestUniqueIDGenSvc::TestUniqueIDGenSvc(const std::string& aName, ISvcLocator* aSvcLoc)
Expand All @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions test/k4FWCoreTest/src/components/TestUniqueIDGenSvc.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
#define TEST_UNIQUEIDGENSVC_H

// GAUDI
#include <Gaudi/Algorithm.h>
#include "Gaudi/Accumulators.h"
#include "Gaudi/Algorithm.h"

#include "k4Interface/IUniqueIDGenSvc.h"

Expand All @@ -37,7 +38,8 @@ class TestUniqueIDGenSvc : public Gaudi::Algorithm {
StatusCode execute(const EventContext&) const final;

private:
SmartIF<IUniqueIDGenSvc> m_service;
SmartIF<IUniqueIDGenSvc> m_service;
mutable Gaudi::Accumulators::Counter<> m_counter{this, "EventCounter"};
};

#endif // TEST_UNIQUEIDGENSVC_H

0 comments on commit f0097d6

Please sign in to comment.