Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #if !defined(TEST_CONTROLLER)
0002 #define TEST_CONTROLLER
0003 #include "FWCore/SharedMemory/interface/ControllerChannel.h"
0004 #include "FWCore/Utilities/interface/Exception.h"
0005 
0006 #include <memory>
0007 #include <string>
0008 #include <stdio.h>
0009 int controller(int argc, char** argv, unsigned int iTimeout) {
0010   using namespace edm::shared_memory;
0011 
0012   ControllerChannel channel("TestChannel", 0, iTimeout);
0013 
0014   //Pipe has to close AFTER we tell the worker to stop
0015   auto closePipe = [](FILE* iFile) { pclose(iFile); };
0016   std::unique_ptr<FILE, decltype(closePipe)> pipe(nullptr, closePipe);
0017 
0018   auto stopWorkerCmd = [](ControllerChannel* iChannel) { iChannel->stopWorker(); };
0019   std::unique_ptr<ControllerChannel, decltype(stopWorkerCmd)> stopWorkerGuard(&channel, stopWorkerCmd);
0020 
0021   {
0022     std::string command(argv[0]);
0023     command += " ";
0024     command += channel.sharedMemoryName();
0025     command += " ";
0026     command += channel.uniqueID();
0027     //make sure output is flushed before popen does any writing
0028     fflush(stdout);
0029     fflush(stderr);
0030 
0031     channel.setupWorker([&]() {
0032       pipe.reset(popen(command.c_str(), "w"));
0033 
0034       if (not pipe) {
0035         throw cms::Exception("PipeFailed") << "pipe failed to open " << command;
0036       }
0037     });
0038   }
0039   {
0040     *channel.toWorkerBufferInfo() = {0, 0};
0041     auto result = channel.doTransition(
0042         [&]() {
0043           if (channel.fromWorkerBufferInfo()->index_ != 1) {
0044             throw cms::Exception("BadValue") << "wrong index value of fromWorkerBufferInfo "
0045                                              << static_cast<int>(channel.fromWorkerBufferInfo()->index_);
0046           }
0047           if (channel.fromWorkerBufferInfo()->identifier_ != 1) {
0048             throw cms::Exception("BadValue")
0049                 << "wrong identifier value of fromWorkerBufferInfo " << channel.fromWorkerBufferInfo()->identifier_;
0050           }
0051           if (not channel.shouldKeepEvent()) {
0052             throw cms::Exception("BadValue") << "told not to keep event";
0053           }
0054         },
0055         edm::Transition::Event,
0056         2);
0057     if (not result) {
0058       throw cms::Exception("TimeOut") << "doTransition timed out";
0059     }
0060   }
0061   {
0062     *channel.toWorkerBufferInfo() = {1, 1};
0063     auto result = channel.doTransition(
0064         [&]() {
0065           if (channel.fromWorkerBufferInfo()->index_ != 0) {
0066             throw cms::Exception("BadValue") << "wrong index value of fromWorkerBufferInfo "
0067                                              << static_cast<int>(channel.fromWorkerBufferInfo()->index_);
0068           }
0069           if (channel.fromWorkerBufferInfo()->identifier_ != 2) {
0070             throw cms::Exception("BadValue")
0071                 << "wrong identifier value of fromWorkerBufferInfo " << channel.fromWorkerBufferInfo()->identifier_;
0072           }
0073           if (channel.shouldKeepEvent()) {
0074             throw cms::Exception("BadValue") << "told to keep event";
0075           }
0076         },
0077         edm::Transition::Event,
0078         3);
0079     if (not result) {
0080       throw cms::Exception("TimeOut") << "doTransition timed out";
0081     }
0082   }
0083 
0084   {
0085     auto result = channel.doTransition([&]() {}, edm::Transition::EndLuminosityBlock, 1);
0086     if (not result) {
0087       throw cms::Exception("TimeOut") << "doTransition timed out";
0088     }
0089   }
0090 
0091   //std::cout <<"controller going to stop"<<std::endl;
0092   return 0;
0093 }
0094 
0095 #endif