Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-01 08:58:17

0001 
0002 #include "catch.hpp"
0003 
0004 #include <JANA/JEventSource.h>
0005 
0006 struct MyEventSource : public JEventSource {
0007     int open_count = 0;
0008     int emit_count = 0;
0009     int close_count = 0;
0010     int finish_event_count = 0;
0011     size_t events_in_file = 5;
0012     int events_per_barrier = 0;
0013 
0014     MyEventSource() {
0015         SetTypeName("MyEventSource");
0016         EnableFinishEvent();
0017     }
0018 
0019     void Open() override {
0020         REQUIRE(GetApplication() != nullptr);
0021         LOG_INFO(GetLogger()) << "Open() called" << LOG_END;
0022         open_count++;
0023     }
0024     Result Emit(JEvent& event) override {
0025         emit_count++;
0026         if ((events_per_barrier != 0) && ((emit_count % events_per_barrier) == 0)) {
0027             event.SetSequential(true);
0028         }
0029         REQUIRE(GetApplication() != nullptr);
0030         if (emit_count > (int) events_in_file) {
0031             LOG_INFO(GetLogger()) << "Emit() called, iteration " << emit_count << ", returning FailureFinished" << LOG_END;
0032             return Result::FailureFinished;
0033         }
0034         LOG_INFO(GetLogger()) << "Emit() called, iteration " << emit_count << ", returning Success" << LOG_END;
0035         return Result::Success;
0036     }
0037     void GetEvent(std::shared_ptr<JEvent> event) override {
0038         emit_count++;
0039         if ((events_per_barrier != 0) && ((emit_count % events_per_barrier) == 0)) {
0040             event->SetSequential(true);
0041         }
0042         REQUIRE(GetApplication() != nullptr);
0043         if (emit_count > (int) events_in_file) {
0044             LOG_INFO(GetLogger()) << "GetEvent() called, iteration " << emit_count << ", returning FailureFinished" << LOG_END;
0045             throw JEventSource::RETURN_STATUS::kNO_MORE_EVENTS;
0046         }
0047         LOG_INFO(GetLogger()) << "GetEvent() called, iteration " << emit_count << ", returning Success" << LOG_END;
0048     }
0049     void Close() override {
0050         REQUIRE(GetApplication() != nullptr);
0051         LOG_INFO(GetLogger()) << "Close() called" << LOG_END;
0052         close_count++;
0053     }
0054     void FinishEvent(JEvent&) override {
0055         LOG_INFO(GetLogger()) << "FinishEvent() called" << LOG_END;
0056         finish_event_count++;
0057     }
0058 };
0059 
0060 TEST_CASE("JEventSource_EmitCount") {
0061 
0062     auto sut = new MyEventSource;
0063     JApplication app;
0064     app.SetParameterValue("jana:loglevel", "off");
0065     app.Add(sut);
0066 
0067     SECTION("ShutsSelfOff_ExpertMode_NoBarriers") {
0068         LOG << "Running test: JEventSource_EmitCount :: ShutsSelfOff_ExpertMode_NoBarriers" << LOG_END;
0069         sut->SetCallbackStyle(MyEventSource::CallbackStyle::ExpertMode);
0070         app.Run();
0071         REQUIRE(sut->open_count == 1);
0072         REQUIRE(sut->emit_count == 6);       // Emit called 5 times successfully and fails on the 6th
0073         REQUIRE(sut->GetEmittedEventCount() == 5);  // Emits 5 events successfully
0074         REQUIRE(sut->close_count == 1);
0075         REQUIRE(sut->finish_event_count == 5);  // All emitted events were finished
0076         REQUIRE(sut->GetProcessedEventCount() == 5); // This is reflected in the finished count
0077     }
0078 
0079     SECTION("LimitedByNEvents_ExpertMode_NoBarriers") {
0080         LOG << "Running test: JEventSource_EmitCount :: LimitedByNEvents_ExpertMode_NoBarriers" << LOG_END;
0081         sut->SetCallbackStyle(MyEventSource::CallbackStyle::ExpertMode);
0082 
0083         app.SetParameterValue("jana:nevents", 3);
0084         app.Run();
0085         REQUIRE(sut->open_count == 1);
0086         REQUIRE(sut->emit_count == 3);        // Emit called 3 times successfully
0087         REQUIRE(sut->GetEmittedEventCount() == 3);   // Nevents limit discovered outside Emit
0088         REQUIRE(sut->close_count == 1);
0089         REQUIRE(sut->finish_event_count == 3);
0090     }
0091 
0092     SECTION("LimitedByNSkip_ExpertMode_NoBarriers") {
0093         LOG << "Running test: JEventSource_EmitCount :: LimitedByNSkip_ExpertMode_NoBarriers" << LOG_END;
0094         sut->SetCallbackStyle(MyEventSource::CallbackStyle::ExpertMode);
0095 
0096         app.SetParameterValue("jana:nskip", 3);
0097         app.Run();
0098         REQUIRE(sut->open_count == 1);
0099         REQUIRE(sut->emit_count == 6);                // Emit was called 5 times successfully and failed on the 6th
0100         REQUIRE(sut->GetEmittedEventCount() == 2);    // The first 3 were skipped, so only 2 entered the topology
0101         REQUIRE(sut->close_count == 1);
0102         REQUIRE(sut->finish_event_count == 5);        // FinishEvent() was called for both emitted and skipped events
0103         REQUIRE(sut->GetProcessedEventCount() == 2);  // The 2 emitted events were both successfully processed
0104         REQUIRE(sut->GetSkippedEventCount() == 3);    // The first 3 events were skipped, out of 5 total
0105     }
0106 
0107     SECTION("ShutsSelfOff_LegacyMode_NoBarriers") {
0108         LOG << "Running test: JEventSource_EmitCount :: ShutsSelfOff_LegacyMode_NoBarriers" << LOG_END;
0109         sut->SetCallbackStyle(MyEventSource::CallbackStyle::LegacyMode);
0110         app.Run();
0111         REQUIRE(sut->open_count == 1);
0112         REQUIRE(sut->emit_count == 6);       // Emit called 5 times successfully and fails on the 6th
0113         REQUIRE(sut->GetEmittedEventCount() == 5);  // Emits 5 events successfully
0114         REQUIRE(sut->close_count == 1);
0115         REQUIRE(sut->finish_event_count == 5);  // All emitted events were finished
0116         REQUIRE(sut->GetProcessedEventCount() == 5); // This is reflected in the finished count
0117     }
0118 
0119     SECTION("LimitedByNEvents_LegacyMode_NoBarriers") {
0120         LOG << "Running test: JEventSource_EmitCount :: LimitedByNEvents_LegacyMode_NoBarriers" << LOG_END;
0121         sut->SetCallbackStyle(MyEventSource::CallbackStyle::LegacyMode);
0122         app.SetParameterValue("jana:nevents", 3);
0123         app.Run();
0124         REQUIRE(sut->open_count == 1);
0125         REQUIRE(sut->emit_count == 3);        // Emit called 3 times successfully
0126         REQUIRE(sut->GetEmittedEventCount() == 3);   // Nevents limit discovered outside Emit
0127         REQUIRE(sut->close_count == 1);
0128         REQUIRE(sut->finish_event_count == 3);
0129     }
0130 
0131     SECTION("LimitedByNSkip_LegacyMode_NoBarriers") {
0132         LOG << "Running test: JEventSource_EmitCount :: LimitedByNSkip_LegacyMode_NoBarriers" << LOG_END;
0133         sut->SetCallbackStyle(MyEventSource::CallbackStyle::LegacyMode);
0134         app.SetParameterValue("jana:nskip", 3);
0135         app.Run();
0136         REQUIRE(sut->open_count == 1);
0137         REQUIRE(sut->emit_count == 6);                // Emit was called 5 times successfully and failed on the 6th
0138         REQUIRE(sut->GetEmittedEventCount() == 2);    // The first 3 were skipped, so only 2 entered the topology
0139         REQUIRE(sut->close_count == 1);
0140         REQUIRE(sut->finish_event_count == 5);        // FinishEvent() was called for both emitted and skipped events
0141         REQUIRE(sut->GetProcessedEventCount() == 2);  // The 2 emitted events were both successfully processed
0142         REQUIRE(sut->GetSkippedEventCount() == 3);    // The first 3 events were skipped, out of 5 total
0143     }
0144 
0145     SECTION("ShutsSelfOff_ExpertMode_Barriers") {
0146         LOG << "Running test: JEventSource_EmitCount :: ShutsSelfOff_ExpertMode_Barriers" << LOG_END;
0147         sut->SetCallbackStyle(MyEventSource::CallbackStyle::ExpertMode);
0148         sut->events_per_barrier = 4;
0149         sut->events_in_file = 50;
0150         app.SetParameterValue("nthreads", 8);
0151         app.Run();
0152         REQUIRE(sut->open_count == 1);
0153         REQUIRE(sut->emit_count == 51);               // Emit called 50 times successfully and fails on the 51st
0154         REQUIRE(sut->GetEmittedEventCount() == 50);   // Emits 50 events successfully
0155         REQUIRE(sut->close_count == 1);
0156         REQUIRE(sut->finish_event_count == 50);       // All emitted events were finished
0157         REQUIRE(sut->GetProcessedEventCount() == 50); // This is reflected in the finished count
0158     }
0159 
0160     SECTION("LimitedByNEvents_ExpertMode_Barriers") {
0161         LOG << "Running test: JEventSource_EmitCount :: LimitedByNEvents_ExpertMode_Barriers" << LOG_END;
0162         sut->SetCallbackStyle(MyEventSource::CallbackStyle::ExpertMode);
0163         sut->events_per_barrier = 4;
0164         sut->events_in_file = 50;
0165         app.SetParameterValue("nthreads", 8);
0166         app.SetParameterValue("jana:nevents", 30);
0167         app.Run();
0168         REQUIRE(sut->open_count == 1);
0169         REQUIRE(sut->emit_count == 30);               // Emit called 30 times successfully
0170         REQUIRE(sut->GetEmittedEventCount() ==  30);  // Nevents limit discovered outside Emit
0171         REQUIRE(sut->close_count == 1);
0172         REQUIRE(sut->finish_event_count == 30);
0173         REQUIRE(sut->GetProcessedEventCount() == 30);
0174     }
0175 
0176     SECTION("LimitedByNSkip_ExpertMode_Barriers") {
0177         LOG << "Running test: JEventSource_EmitCount :: LimitedByNSkip_ExpertMode_Barriers" << LOG_END;
0178         sut->SetCallbackStyle(MyEventSource::CallbackStyle::ExpertMode);
0179         sut->events_per_barrier = 4;
0180         sut->events_in_file = 50;
0181         app.SetParameterValue("nthreads", 8);
0182         app.SetParameterValue("jana:nskip", 10);
0183         app.Run();
0184         REQUIRE(sut->open_count == 1);
0185         REQUIRE(sut->emit_count == 51);                // GetEvent was called 50 times successfully and failed on the 51st
0186         REQUIRE(sut->GetEmittedEventCount() == 40);    // The first 10 were skipped, so only 40 entered the topology
0187         REQUIRE(sut->close_count == 1);
0188         REQUIRE(sut->finish_event_count == 50);        // FinishEvent() was called for both emitted and skipped events
0189         REQUIRE(sut->GetProcessedEventCount() == 40);  // The 40 emitted events were successfully processed
0190         REQUIRE(sut->GetSkippedEventCount() == 10);    // The first 10 events were skipped, out of 50 total
0191     }
0192 
0193     SECTION("ShutsSelfOff_LegacyMode_Barriers") {
0194         LOG << "Running test: JEventSource_EmitCount :: ShutsSelfOff_LegacyMode_Barriers" << LOG_END;
0195         sut->SetCallbackStyle(MyEventSource::CallbackStyle::LegacyMode);
0196         sut->events_per_barrier = 4;
0197         sut->events_in_file = 50;
0198         app.SetParameterValue("nthreads", 8);
0199         app.Run();
0200         REQUIRE(sut->open_count == 1);
0201         REQUIRE(sut->emit_count == 51);               // Emit called 50 times successfully and fails on the 51st
0202         REQUIRE(sut->GetEmittedEventCount() == 50);   // Emits 50 events successfully
0203         REQUIRE(sut->close_count == 1);
0204         REQUIRE(sut->finish_event_count == 50);       // All emitted events were finished
0205         REQUIRE(sut->GetProcessedEventCount() == 50); // This is reflected in the finished count
0206     }
0207 
0208     SECTION("LimitedByNEvents_LegacyMode_Barriers") {
0209         LOG << "Running test: JEventSource_EmitCount :: LimitedByNEvents_LegacyMode_Barriers" << LOG_END;
0210         sut->SetCallbackStyle(MyEventSource::CallbackStyle::LegacyMode);
0211         sut->events_per_barrier = 4;
0212         sut->events_in_file = 50;
0213         app.SetParameterValue("nthreads", 8);
0214         app.SetParameterValue("jana:nevents", 30);
0215         app.Run();
0216         REQUIRE(sut->open_count == 1);
0217         REQUIRE(sut->emit_count == 30);               // Emit called 30 times successfully
0218         REQUIRE(sut->GetEmittedEventCount() ==  30);  // Nevents limit discovered outside Emit
0219         REQUIRE(sut->close_count == 1);
0220         REQUIRE(sut->finish_event_count == 30);
0221         REQUIRE(sut->GetProcessedEventCount() == 30);
0222     }
0223 
0224     SECTION("LimitedByNSkip_LegacyMode_Barriers") {
0225         LOG << "Running test: JEventSource_EmitCount :: LimitedByNSkip_LegacyMode_Barriers" << LOG_END;
0226         sut->SetCallbackStyle(MyEventSource::CallbackStyle::LegacyMode);
0227         sut->events_per_barrier = 4;
0228         sut->events_in_file = 50;
0229         app.SetParameterValue("nthreads", 8);
0230         app.SetParameterValue("jana:nskip", 10);
0231         app.Run();
0232         REQUIRE(sut->open_count == 1);
0233         REQUIRE(sut->emit_count == 51);                // GetEvent was called 50 times successfully and failed on the 51st
0234         REQUIRE(sut->GetEmittedEventCount() == 40);    // The first 10 were skipped, so only 40 entered the topology
0235         REQUIRE(sut->close_count == 1);
0236         REQUIRE(sut->finish_event_count == 50);        // FinishEvent() was called for both emitted and skipped events
0237         REQUIRE(sut->GetProcessedEventCount() == 40);  // The 40 emitted events were successfully processed
0238         REQUIRE(sut->GetSkippedEventCount() == 10);    // The first 10 events were skipped, out of 50 total
0239     }
0240 }
0241