Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-16 09:14:50

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