Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:17:42

0001 
0002 // Copyright 2020, Jefferson Science Associates, LLC.
0003 // Subject to the terms in the LICENSE file found in the top-level directory.
0004 
0005 #include "catch.hpp"
0006 
0007 #include <JANA/JEventSource.h>
0008 
0009 
0010 struct NEventNSkipBoundedSource : public JEventSource {
0011 
0012     std::atomic_int event_count {0};
0013     int event_bound = 100;
0014     std::vector<int> events_emitted;
0015     std::atomic_int open_count{0};
0016     std::atomic_int close_count{0};
0017 
0018     NEventNSkipBoundedSource() {
0019         SetCallbackStyle(CallbackStyle::ExpertMode);
0020     }
0021 
0022     Result Emit(JEvent&) override {
0023         if (event_count >= event_bound) {
0024             return Result::FailureFinished;
0025         }
0026         event_count += 1;
0027         events_emitted.push_back(event_count);
0028         return Result::Success;
0029     }
0030 
0031     void Open() override {
0032         open_count++;
0033         LOG << "Opening source " << GetResourceName() << LOG_END;
0034     }
0035     void Close() override {
0036         close_count++;
0037         LOG << "Closing source " << GetResourceName() << LOG_END;
0038     }
0039 };
0040 
0041 
0042 TEST_CASE("NEventNSkipTests") {
0043 
0044     JApplication app;
0045     auto source = new NEventNSkipBoundedSource();
0046     app.Add(source);
0047 
0048     SECTION("[1..100] @ nskip=0, nevents=0 => [1..100]") {
0049 
0050         app.SetParameterValue("jana:nskip", 0);
0051         app.SetParameterValue("jana:nevents", 0);
0052         app.SetParameterValue("nthreads", 1);
0053         app.Run(true);
0054         REQUIRE(source->event_count == 100);
0055         REQUIRE(source->events_emitted.size() == 100);
0056         REQUIRE(source->events_emitted[0] == 1);
0057         REQUIRE(source->events_emitted[99] == 100);
0058     }
0059 
0060     SECTION("[1..100] @ nskip=0, nevents=22 => [1..22]") {
0061 
0062         app.SetParameterValue("jana:nskip", 0);
0063         app.SetParameterValue("jana:nevents", 22);
0064         app.SetParameterValue("nthreads", 1);
0065         app.Run(true);
0066         REQUIRE(source->event_count == 22);
0067         REQUIRE(source->events_emitted.size() == 22);
0068         REQUIRE(source->events_emitted[0] == 1);
0069         REQUIRE(source->events_emitted[21] == 22);
0070     }
0071 
0072     SECTION("[1..100] @ nskip=30, nevents=20 => [31..51]") {
0073 
0074         app.SetParameterValue("jana:nskip", 30);
0075         app.SetParameterValue("jana:nevents", 20);
0076         app.SetParameterValue("nthreads", 1);
0077         app.Run(true);
0078         REQUIRE(source->event_count == 50);
0079         // All 50 events get filled, but the first 30 are discarded without emitting
0080         // However, all 50 go into events_emitted
0081     }
0082 }
0083 
0084 TEST_CASE("JEventSourceArrow with multiple JEventSources") {
0085     JApplication app;
0086     auto source1 = new NEventNSkipBoundedSource();
0087     auto source2 = new NEventNSkipBoundedSource();
0088     auto source3 = new NEventNSkipBoundedSource();
0089     app.Add(source1);
0090     app.Add(source2);
0091     app.Add(source3);
0092 
0093     SECTION("All three event sources initialize, run, and finish") {
0094         source1->event_bound = 9;
0095         source2->event_bound = 13;
0096         source3->event_bound = 7;
0097 
0098         app.SetParameterValue("jana:nskip", 0);
0099         app.SetParameterValue("jana:nevents", 0);
0100         app.SetParameterValue("nthreads", 4);
0101         app.Run(true);
0102 
0103         REQUIRE(app.GetExitCode() == (int) JApplication::ExitCode::Success);
0104         REQUIRE(source1->GetStatus() == JEventSource::Status::Closed);
0105         REQUIRE(source2->GetStatus() == JEventSource::Status::Closed);
0106         REQUIRE(source3->GetStatus() == JEventSource::Status::Closed);
0107         REQUIRE(source1->open_count == 1);
0108         REQUIRE(source2->open_count == 1);
0109         REQUIRE(source3->open_count == 1);
0110         REQUIRE(source1->close_count == 1);
0111         REQUIRE(source2->close_count == 1);
0112         REQUIRE(source3->close_count == 1);
0113         REQUIRE(source1->GetEmittedEventCount() == 9);
0114         REQUIRE(source2->GetEmittedEventCount() == 13);
0115         REQUIRE(source3->GetEmittedEventCount() == 7);
0116         REQUIRE(app.GetNEventsProcessed() == 9+13+7);
0117     }
0118 
0119     SECTION("All three event sources initialize, run, and finish, each using the same nskip,nevents (self-terminated)") {
0120         source1->event_bound = 9;
0121         source2->event_bound = 13;
0122         source3->event_bound = 7;
0123 
0124         app.SetParameterValue("jana:nskip", 3);
0125         app.SetParameterValue("jana:nevents", 9);
0126         app.SetParameterValue("nthreads", 4);
0127         app.Run(true);
0128 
0129         REQUIRE(app.GetExitCode() == (int) JApplication::ExitCode::Success);
0130         REQUIRE(source1->GetStatus() == JEventSource::Status::Closed);
0131         REQUIRE(source2->GetStatus() == JEventSource::Status::Closed);
0132         REQUIRE(source3->GetStatus() == JEventSource::Status::Closed);
0133         REQUIRE(source1->open_count == 1);
0134         REQUIRE(source2->open_count == 1);
0135         REQUIRE(source3->open_count == 1);
0136         REQUIRE(source1->close_count == 1);
0137         REQUIRE(source2->close_count == 1);
0138         REQUIRE(source3->close_count == 1);
0139         REQUIRE(source1->GetEmittedEventCount() == 9);   // 3 dropped, 6 emitted
0140         REQUIRE(source2->GetEmittedEventCount() == 12);  // 3 dropped, 9 emitted
0141         REQUIRE(source3->GetEmittedEventCount() == 7);   // 3 dropped, 4 emitted
0142         REQUIRE(app.GetNEventsProcessed() == 19);
0143     }
0144 
0145 
0146     SECTION("All three event sources initialize, run, and finish, using individualized nskip,nevents (nevents-terminated)") {
0147         source1->event_bound = 9;
0148         source2->event_bound = 13;
0149         source3->event_bound = 7;
0150         source1->SetNSkip(2);
0151         source1->SetNEvents(4);
0152         source3->SetNEvents(4);
0153 
0154         app.SetParameterValue("nthreads", 4);
0155         app.Run(true);
0156 
0157         REQUIRE(app.GetExitCode() == (int) JApplication::ExitCode::Success);
0158         REQUIRE(source1->GetStatus() == JEventSource::Status::Closed);
0159         REQUIRE(source2->GetStatus() == JEventSource::Status::Closed);
0160         REQUIRE(source3->GetStatus() == JEventSource::Status::Closed);
0161         REQUIRE(source1->open_count == 1);
0162         REQUIRE(source2->open_count == 1);
0163         REQUIRE(source3->open_count == 1);
0164         REQUIRE(source1->close_count == 1);
0165         REQUIRE(source2->close_count == 1);
0166         REQUIRE(source3->close_count == 1);
0167         REQUIRE(source1->GetEmittedEventCount() == 6);   // 2 dropped, 4 emitted
0168         REQUIRE(source2->GetEmittedEventCount() == 13);  // 13 emitted
0169         REQUIRE(source3->GetEmittedEventCount() == 4);   // 4 emitted
0170         REQUIRE(app.GetNEventsProcessed() == 21);
0171     }
0172 
0173 }
0174