Skip to content

Commit

Permalink
Test JOmniFactory/JService interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanwbrei committed Jan 30, 2025
1 parent 165a8fd commit f041ee1
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/libraries/JANA/Components/JOmniFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/programs/unit_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
82 changes: 82 additions & 0 deletions src/programs/unit_tests/Components/JServiceTests.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@

#include <catch.hpp>
#include <JANA/JApplication.h>
#include <JANA/JService.h>
#include <JANA/Components/JOmniFactory.h>
#include <JANA/Components/JOmniFactoryGeneratorT.h>

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<DummyService>());
try {
app.Initialize();
REQUIRE(1 == 0); // Shouldn't be reachable

auto sut = app.GetService<DummyService>();
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<DummyOmniFactory> {

Service<DummyService> m_svc {this};
Output<DummyData> 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<DummyService>());

auto gen = new components::JOmniFactoryGeneratorT<DummyOmniFactory>();
gen->AddWiring("dummy", {}, {"data"});
app.Add(gen);

try {
app.Initialize();
REQUIRE(1 == 0); // Shouldn't be reachable
auto event = std::make_shared<JEvent>(&app);
auto data = event->Get<DummyData>("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

0 comments on commit f041ee1

Please sign in to comment.