From f041ee1891302fd245a9540fa4605f0eb6317d3f Mon Sep 17 00:00:00 2001 From: Nathan Brei Date: Sat, 16 Nov 2024 23:05:14 -0500 Subject: [PATCH] Test JOmniFactory/JService interaction --- src/libraries/JANA/Components/JOmniFactory.h | 2 +- src/programs/unit_tests/CMakeLists.txt | 1 + .../unit_tests/Components/JServiceTests.cc | 82 +++++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 src/programs/unit_tests/Components/JServiceTests.cc diff --git a/src/libraries/JANA/Components/JOmniFactory.h b/src/libraries/JANA/Components/JOmniFactory.h index ea08e4a11..30b41b47e 100644 --- a/src/libraries/JANA/Components/JOmniFactory.h +++ b/src/libraries/JANA/Components/JOmniFactory.h @@ -251,7 +251,7 @@ class JOmniFactory : public JMultifactory, public jana::components::JHasInputs { variadic_output_count += 1; } } - size_t variadic_output_collection_count = FindVariadicCollectionCount(m_outputs.size(), variadic_output_count, output_collection_names.size(), true); + size_t variadic_output_collection_count = FindVariadicCollectionCount(m_outputs.size(), variadic_output_count, output_collection_names.size(), false); // Set output collection names and create corresponding helper factories i = 0; diff --git a/src/programs/unit_tests/CMakeLists.txt b/src/programs/unit_tests/CMakeLists.txt index 720938748..108b9e074 100644 --- a/src/programs/unit_tests/CMakeLists.txt +++ b/src/programs/unit_tests/CMakeLists.txt @@ -22,6 +22,7 @@ set(TEST_SOURCES Components/JFactoryTests.cc Components/JFactoryGeneratorTests.cc Components/JMultiFactoryTests.cc + Components/JServiceTests.cc Components/UnfoldTests.cc Components/UserExceptionTests.cc diff --git a/src/programs/unit_tests/Components/JServiceTests.cc b/src/programs/unit_tests/Components/JServiceTests.cc new file mode 100644 index 000000000..c13b5ccae --- /dev/null +++ b/src/programs/unit_tests/Components/JServiceTests.cc @@ -0,0 +1,82 @@ + +#include +#include +#include +#include +#include + +namespace jana::jservicetests { + +class DummyService: public JService { + bool init_started = false; + bool init_finished = false; +public: + void Init() override { + LOG_INFO(GetLogger()) << "Starting DummyService::Init()" << LOG_END; + init_started = true; + throw std::runtime_error("Something goes wrong"); + init_finished = true; + LOG_INFO(GetLogger()) << "Finishing DummyService::Init()" << LOG_END; + } +}; + +TEST_CASE("JServiceTests_ExceptionInInit") { + JApplication app; + app.ProvideService(std::make_shared()); + try { + app.Initialize(); + REQUIRE(1 == 0); // Shouldn't be reachable + + auto sut = app.GetService(); + REQUIRE(1 == 0); // Definitely shouldn't be reachable + } + catch (JException& e) { + REQUIRE(e.GetMessage() == "Something goes wrong"); + REQUIRE(e.type_name == "jana::jservicetests::DummyService"); + REQUIRE(e.function_name == "JService::Init"); + } +} + +struct DummyData {int x;}; + +struct DummyOmniFactory: public jana::components::JOmniFactory { + + Service m_svc {this}; + Output m_output {this}; + + void Configure() { + } + + void ChangeRun(int32_t /*run_nr*/) { + } + + void Execute(int32_t /*run_nr*/, uint64_t /*evt_nr*/) { + m_output().push_back(new DummyData{22}); + } +}; + +TEST_CASE("JServiceTests_ExceptionInInit_Issue381") { + JApplication app; + app.ProvideService(std::make_shared()); + + auto gen = new components::JOmniFactoryGeneratorT(); + gen->AddWiring("dummy", {}, {"data"}); + app.Add(gen); + + try { + app.Initialize(); + REQUIRE(1 == 0); // Shouldn't be reachable + auto event = std::make_shared(&app); + auto data = event->Get("data"); + REQUIRE(1 == 0); // Definitely shouldn't be reachable + REQUIRE(data.at(0)->x == 22); + } + catch (JException& e) { + REQUIRE(e.GetMessage() == "Something goes wrong"); + REQUIRE(e.type_name == "jana::jservicetests::DummyService"); + REQUIRE(e.function_name == "JService::Init"); + } +} + +} // namespace jana::jservicetests +