Back to home page

EIC code displayed by LXR

 
 

    


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

0001 
0002 // Copyright 2021, Jefferson Science Associates, LLC.
0003 // Subject to the terms in the LICENSE file found in the top-level directory.
0004 
0005 
0006 #ifndef JANA2_TIMEOUTTESTS_H
0007 #define JANA2_TIMEOUTTESTS_H
0008 
0009 #include <JANA/JEventSource.h>
0010 #include <JANA/JEventProcessor.h>
0011 
0012 struct SourceWithTimeout : public JEventSource {
0013 
0014     int timeout_on_event_nr = -1;
0015     int first_event_delay_ms = 0;
0016     std::atomic_int event_count {0};
0017 
0018     SourceWithTimeout(int timeout_on_event_nr=-1,
0019                       int first_delay_ms=0 )
0020 
0021         : timeout_on_event_nr(timeout_on_event_nr)
0022         , first_event_delay_ms(first_delay_ms)
0023     { 
0024         SetCallbackStyle(CallbackStyle::ExpertMode);
0025     }
0026 
0027     void Open() override {
0028     }
0029 
0030     Result Emit(JEvent&) override {
0031         event_count += 1;
0032         std::cout << "Processing event # " << event_count << std::endl;
0033         std::flush(std::cout);
0034 
0035         if (event_count == 1) {
0036             std::this_thread::sleep_for(std::chrono::milliseconds(first_event_delay_ms));
0037         }
0038 
0039         if (event_count == 100) {
0040             return Result::FailureFinished;
0041         }
0042 
0043         if (event_count == timeout_on_event_nr) {
0044             while (true) {
0045                 std::this_thread::sleep_for(std::chrono::milliseconds(100));
0046             }; // Endless loop
0047         }
0048         return Result::Success;
0049     }
0050 };
0051 
0052 
0053 
0054 struct ProcessorWithTimeout : public JEventProcessor {
0055 
0056     std::atomic_int processed_count {0};
0057     int timeout_on_event_nr;
0058     int first_event_delay_ms;
0059 
0060     explicit ProcessorWithTimeout(int timeout_on_event_nr=-1,
0061                                   int first_event_delay_ms = 0)
0062         : timeout_on_event_nr(timeout_on_event_nr)
0063         , first_event_delay_ms(first_event_delay_ms)
0064     { 
0065         SetCallbackStyle(CallbackStyle::ExpertMode);
0066     }
0067 
0068     void Init() override {}
0069 
0070     void Process(const JEvent&) override {
0071         processed_count += 1;
0072         if (processed_count == 1) {
0073             std::this_thread::sleep_for(std::chrono::milliseconds(first_event_delay_ms));
0074         }
0075         if (processed_count == timeout_on_event_nr) {
0076             while (true) {
0077                 std::this_thread::sleep_for(std::chrono::milliseconds(100));
0078             }; // Endless loop
0079         }
0080     }
0081 
0082     void Finish() override {}
0083 };
0084 
0085 #endif //JANA2_TIMEOUTTESTS_H