Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:13:09

0001 #if !defined(TEST_WORKER)
0002 #define TEST_WORKER
0003 #include "FWCore/SharedMemory/interface/WorkerChannel.h"
0004 #include "FWCore/Utilities/interface/Exception.h"
0005 
0006 #include <memory>
0007 #include <string>
0008 #include <stdio.h>
0009 #include <cassert>
0010 #include <thread>
0011 
0012 enum class WorkerType { kStandard, kOKTimeout, kStartupTimeout };
0013 
0014 int worker(int argc, char** argv, WorkerType iType) {
0015   using namespace edm::shared_memory;
0016 
0017   using namespace std::chrono_literals;
0018   if (iType == WorkerType::kStartupTimeout) {
0019     //Take too long before openning the worker channel
0020     std::this_thread::sleep_for(20s);
0021   }
0022 
0023   assert(argc == 3);
0024   WorkerChannel channel(argv[1], argv[2]);
0025 
0026   if (iType == WorkerType::kOKTimeout) {
0027     //simulate long time setting up
0028     std::this_thread::sleep_for(20s);
0029   }
0030   //std::cerr<<"worker setup\n";
0031   channel.workerSetupDone();
0032 
0033   if (iType == WorkerType::kOKTimeout) {
0034     std::this_thread::sleep_for(20s);
0035   }
0036   int transitionCount = 0;
0037   channel.handleTransitions([&](edm::Transition iTransition, unsigned long long iTransitionID) {
0038     if (iType == WorkerType::kOKTimeout) {
0039       using namespace std::chrono_literals;
0040       std::this_thread::sleep_for(20s);
0041     }
0042     switch (transitionCount) {
0043       case 0: {
0044         if (iTransition != edm::Transition::Event) {
0045           throw cms::Exception("BadValue") << "wrong transition received " << static_cast<int>(iTransition);
0046         }
0047         if (iTransitionID != 2ULL) {
0048           throw cms::Exception("BadValue") << "wrong transitionID received " << static_cast<int>(iTransitionID);
0049         }
0050 
0051         if (channel.toWorkerBufferInfo()->index_ != 0) {
0052           throw cms::Exception("BadValue")
0053               << "wrong toWorkerBufferInfo index received " << static_cast<int>(channel.toWorkerBufferInfo()->index_);
0054         }
0055         if (channel.toWorkerBufferInfo()->identifier_ != 0) {
0056           throw cms::Exception("BadValue")
0057               << "wrong toWorkerBufferInfo identifier received " << channel.toWorkerBufferInfo()->identifier_;
0058         }
0059         *channel.fromWorkerBufferInfo() = {1, 1};
0060         channel.shouldKeepEvent(true);
0061         break;
0062       }
0063 
0064       case 1: {
0065         if (iTransition != edm::Transition::Event) {
0066           throw cms::Exception("BadValue") << "wrong transition received " << static_cast<int>(iTransition);
0067         }
0068         if (iTransitionID != 3ULL) {
0069           throw cms::Exception("BadValue") << "wrong transitionID received " << static_cast<int>(iTransitionID);
0070         }
0071 
0072         if (channel.toWorkerBufferInfo()->index_ != 1) {
0073           throw cms::Exception("BadValue")
0074               << "wrong toWorkerBufferInfo index received " << static_cast<int>(channel.toWorkerBufferInfo()->index_);
0075         }
0076         if (channel.toWorkerBufferInfo()->identifier_ != 1) {
0077           throw cms::Exception("BadValue")
0078               << "wrong toWorkerBufferInfo identifier received " << channel.toWorkerBufferInfo()->identifier_;
0079         }
0080         *channel.fromWorkerBufferInfo() = {2, 0};
0081         channel.shouldKeepEvent(false);
0082         break;
0083       }
0084 
0085       case 2: {
0086         if (iTransition != edm::Transition::EndLuminosityBlock) {
0087           throw cms::Exception("BadValue") << "wrong transition received " << static_cast<int>(iTransition);
0088         }
0089         if (iTransitionID != 1ULL) {
0090           throw cms::Exception("BadValue") << "wrong transitionID received " << static_cast<int>(iTransitionID);
0091         }
0092         break;
0093       }
0094       default: {
0095         throw cms::Exception("MissingStop") << "stopRequested not set";
0096       }
0097     }
0098     ++transitionCount;
0099   });
0100   if (transitionCount != 3) {
0101     throw cms::Exception("MissingStop") << "stop requested too soon " << transitionCount;
0102   }
0103   return 0;
0104 }
0105 #endif