File indexing completed on 2025-02-23 10:24:46
0001
0002 #include <catch.hpp>
0003 #include <JANA/JApplication.h>
0004 #include <JANA/JService.h>
0005 #include <JANA/Components/JOmniFactory.h>
0006 #include <JANA/Components/JOmniFactoryGeneratorT.h>
0007
0008 namespace jana::jservicetests {
0009
0010 class DummyService: public JService {
0011 bool init_started = false;
0012 bool init_finished = false;
0013 public:
0014 void Init() override {
0015 LOG_INFO(GetLogger()) << "Starting DummyService::Init()" << LOG_END;
0016 init_started = true;
0017 throw std::runtime_error("Something goes wrong");
0018 init_finished = true;
0019 LOG_INFO(GetLogger()) << "Finishing DummyService::Init()" << LOG_END;
0020 }
0021 };
0022
0023 TEST_CASE("JServiceTests_ExceptionInInit") {
0024 JApplication app;
0025 app.ProvideService(std::make_shared<DummyService>());
0026 try {
0027 app.Initialize();
0028 REQUIRE(1 == 0);
0029
0030 auto sut = app.GetService<DummyService>();
0031 REQUIRE(1 == 0);
0032 }
0033 catch (JException& e) {
0034 REQUIRE(e.GetMessage() == "Something goes wrong");
0035 REQUIRE(e.type_name == "jana::jservicetests::DummyService");
0036 REQUIRE(e.function_name == "JService::Init");
0037 }
0038 }
0039
0040 struct DummyData {int x;};
0041
0042 struct DummyOmniFactory: public jana::components::JOmniFactory<DummyOmniFactory> {
0043
0044 Service<DummyService> m_svc {this};
0045 Output<DummyData> m_output {this};
0046
0047 void Configure() {
0048 }
0049
0050 void ChangeRun(int32_t ) {
0051 }
0052
0053 void Execute(int32_t , uint64_t ) {
0054 m_output().push_back(new DummyData{22});
0055 }
0056 };
0057
0058 TEST_CASE("JServiceTests_ExceptionInInit_Issue381") {
0059 JApplication app;
0060 app.ProvideService(std::make_shared<DummyService>());
0061
0062 auto gen = new components::JOmniFactoryGeneratorT<DummyOmniFactory>();
0063 gen->AddWiring("dummy", {}, {"data"});
0064 app.Add(gen);
0065
0066 try {
0067 app.Initialize();
0068 REQUIRE(1 == 0);
0069 auto event = std::make_shared<JEvent>(&app);
0070 auto data = event->Get<DummyData>("data");
0071 REQUIRE(1 == 0);
0072 REQUIRE(data.at(0)->x == 22);
0073 }
0074 catch (JException& e) {
0075 REQUIRE(e.GetMessage() == "Something goes wrong");
0076 REQUIRE(e.type_name == "jana::jservicetests::DummyService");
0077 REQUIRE(e.function_name == "JService::Init");
0078 }
0079 }
0080
0081 }
0082