Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-01-31 02:19:30

0001 /**
0002    \file
0003    test for ProductRegistry 
0004 
0005    \author Stefano ARGIRO
0006    \date 21 July 2005
0007 */
0008 
0009 #include <iostream>
0010 #include "cppunit/extensions/HelperMacros.h"
0011 #include <memory>
0012 #include "FWCore/Utilities/interface/GetPassID.h"
0013 #include "FWCore/Version/interface/GetReleaseVersion.h"
0014 
0015 #include "FWCore/Framework/interface/SignallingProductRegistry.h"
0016 #include "FWCore/Framework/interface/PreallocationConfiguration.h"
0017 
0018 #include "FWCore/Framework/interface/global/EDProducer.h"
0019 #include "FWCore/Framework/interface/ExceptionActions.h"
0020 #include "DataFormats/Provenance/interface/ProductRegistry.h"
0021 #include "DataFormats/Provenance/interface/ProcessConfiguration.h"
0022 #include "FWCore/Framework/interface/maker/WorkerMaker.h"
0023 #include "FWCore/Framework/interface/maker/MakeModuleParams.h"
0024 #include "FWCore/Framework/interface/maker/WorkerT.h"
0025 
0026 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0027 
0028 #include "FWCore/Utilities/interface/TypeID.h"
0029 
0030 class testEDProducerProductRegistryCallback : public CppUnit::TestFixture {
0031   CPPUNIT_TEST_SUITE(testEDProducerProductRegistryCallback);
0032 
0033   CPPUNIT_TEST_EXCEPTION(testCircularRef, cms::Exception);
0034   CPPUNIT_TEST_EXCEPTION(testCircularRef2, cms::Exception);
0035   CPPUNIT_TEST(testTwoListeners);
0036 
0037   CPPUNIT_TEST_SUITE_END();
0038 
0039 public:
0040   void setUp() {}
0041   void tearDown() {}
0042   void testCircularRef();
0043   void testCircularRef2();
0044   void testTwoListeners();
0045 };
0046 
0047 ///registration of the test so that the runner can find it
0048 CPPUNIT_TEST_SUITE_REGISTRATION(testEDProducerProductRegistryCallback);
0049 
0050 using namespace edm;
0051 
0052 namespace {
0053   class TestMod : public global::EDProducer<> {
0054   public:
0055     explicit TestMod(ParameterSet const& p);
0056 
0057     void produce(StreamID, Event& e, EventSetup const&) const override;
0058 
0059     void listen(ProductDescription const&);
0060   };
0061 
0062   TestMod::TestMod(ParameterSet const&) { produces<int>(); }
0063 
0064   void TestMod::produce(StreamID, Event&, EventSetup const&) const {}
0065 
0066   class ListenMod : public global::EDProducer<> {
0067   public:
0068     explicit ListenMod(ParameterSet const&);
0069     void produce(StreamID, Event& e, EventSetup const&) const override;
0070     void listen(ProductDescription const&);
0071   };
0072 
0073   ListenMod::ListenMod(ParameterSet const&) {
0074     callWhenNewProductsRegistered(
0075         [this](ProductDescription const& productDescription) { this->listen(productDescription); });
0076   }
0077   void ListenMod::produce(StreamID, Event&, EventSetup const&) const {}
0078 
0079   void ListenMod::listen(ProductDescription const& iDesc) {
0080     edm::TypeID intType(typeid(int));
0081     //std::cout << "see class " << iDesc.typeName() << std::endl;
0082     if (iDesc.friendlyClassName() == intType.friendlyClassName()) {
0083       produces<int>(iDesc.moduleLabel() + "-" + iDesc.productInstanceName());
0084       //std::cout << iDesc.moduleLabel() << "-" << iDesc.productInstanceName() << std::endl;
0085     }
0086   }
0087 
0088   class ListenFloatMod : public global::EDProducer<> {
0089   public:
0090     explicit ListenFloatMod(ParameterSet const&);
0091     void produce(StreamID, Event& e, EventSetup const&) const;
0092     void listen(ProductDescription const&);
0093   };
0094 
0095   ListenFloatMod::ListenFloatMod(ParameterSet const&) {
0096     callWhenNewProductsRegistered(
0097         [this](ProductDescription const& productDescription) { this->listen(productDescription); });
0098   }
0099   void ListenFloatMod::produce(StreamID, Event&, EventSetup const&) const {}
0100 
0101   void ListenFloatMod::listen(ProductDescription const& iDesc) {
0102     edm::TypeID intType(typeid(int));
0103     //std::cout <<"see class "<<iDesc.typeName()<<std::endl;
0104     if (iDesc.friendlyClassName() == intType.friendlyClassName()) {
0105       produces<float>(iDesc.moduleLabel() + "-" + iDesc.productInstanceName());
0106       //std::cout <<iDesc.moduleLabel()<<"-"<<iDesc.productInstanceName()<<std::endl;
0107     }
0108   }
0109 }  // namespace
0110 
0111 void testEDProducerProductRegistryCallback::testCircularRef() {
0112   using namespace edm;
0113 
0114   SignallingProductRegistry preg;
0115 
0116   std::unique_ptr<Maker> f = std::make_unique<WorkerMaker<TestMod>>();
0117 
0118   ParameterSet p1;
0119   p1.addParameter("@module_type", std::string("TestMod"));
0120   p1.addParameter("@module_label", std::string("t1"));
0121   p1.addParameter("@module_edm_type", std::string("EDProducer"));
0122   p1.registerIt();
0123 
0124   ParameterSet p2;
0125   p2.addParameter("@module_type", std::string("TestMod"));
0126   p2.addParameter("@module_label", std::string("t2"));
0127   p2.addParameter("@module_edm_type", std::string("EDProducer"));
0128   p2.registerIt();
0129 
0130   edm::ExceptionToActionTable table;
0131   edm::PreallocationConfiguration prealloc;
0132 
0133   edm::ParameterSet dummyProcessPset;
0134   dummyProcessPset.registerIt();
0135   auto pc =
0136       std::make_shared<ProcessConfiguration>("PROD", dummyProcessPset.id(), edm::getReleaseVersion(), edm::getPassID());
0137 
0138   edm::MakeModuleParams params1(&p1, preg, &prealloc, pc);
0139   edm::MakeModuleParams params2(&p2, preg, &prealloc, pc);
0140 
0141   std::unique_ptr<Maker> lM = std::make_unique<WorkerMaker<ListenMod>>();
0142   ParameterSet l1;
0143   l1.addParameter("@module_type", std::string("ListenMod"));
0144   l1.addParameter("@module_label", std::string("l1"));
0145   l1.addParameter("@module_edm_type", std::string("EDProducer"));
0146   l1.registerIt();
0147 
0148   ParameterSet l2;
0149   l2.addParameter("@module_type", std::string("ListenMod"));
0150   l2.addParameter("@module_label", std::string("l2"));
0151   l2.addParameter("@module_edm_type", std::string("EDProducer"));
0152   l2.registerIt();
0153 
0154   edm::MakeModuleParams paramsl1(&l1, preg, &prealloc, pc);
0155   edm::MakeModuleParams paramsl2(&l2, preg, &prealloc, pc);
0156 
0157   signalslot::Signal<void(const ModuleDescription&)> aSignal;
0158 
0159   auto m1 = f->makeModule(params1, aSignal, aSignal);
0160   std::unique_ptr<Worker> w1 = m1->makeWorker(&table);
0161   auto ml1 = lM->makeModule(paramsl1, aSignal, aSignal);
0162   std::unique_ptr<Worker> wl1 = ml1->makeWorker(&table);
0163   auto ml2 = lM->makeModule(paramsl2, aSignal, aSignal);
0164   std::unique_ptr<Worker> wl2 = ml2->makeWorker(&table);
0165   auto m2 = f->makeModule(params2, aSignal, aSignal);
0166   std::unique_ptr<Worker> w2 = m2->makeWorker(&table);
0167 
0168   //Should be 5 products
0169   // 1 from the module 't1'
0170   //    1 from 'l1' in response
0171   //       1 from 'l2' in response to 'l1'
0172   //    1 from 'l2' in response to 't1'
0173   //       1 from 'l1' in response to 'l2'
0174   // 1 from the module 't2'
0175   //    1 from 'l1' in response
0176   //       1 from 'l2' in response to 'l1'
0177   //    1 from 'l2' in response to 't2'
0178   //       1 from 'l1' in response to 'l2'
0179   //std::cout <<"# products "<<preg.size()<<std::endl;
0180   CPPUNIT_ASSERT(10 == preg.size());
0181 }
0182 
0183 void testEDProducerProductRegistryCallback::testCircularRef2() {
0184   using namespace edm;
0185 
0186   SignallingProductRegistry preg;
0187 
0188   std::unique_ptr<Maker> f = std::make_unique<WorkerMaker<TestMod>>();
0189 
0190   ParameterSet p1;
0191   p1.addParameter("@module_type", std::string("TestMod"));
0192   p1.addParameter("@module_label", std::string("t1"));
0193   p1.addParameter("@module_edm_type", std::string("EDProducer"));
0194   p1.registerIt();
0195 
0196   ParameterSet p2;
0197   p2.addParameter("@module_type", std::string("TestMod"));
0198   p2.addParameter("@module_label", std::string("t2"));
0199   p2.addParameter("@module_edm_type", std::string("EDProducer"));
0200   p2.registerIt();
0201 
0202   edm::ExceptionToActionTable table;
0203   edm::PreallocationConfiguration prealloc;
0204 
0205   edm::ParameterSet dummyProcessPset;
0206   dummyProcessPset.registerIt();
0207   auto pc =
0208       std::make_shared<ProcessConfiguration>("PROD", dummyProcessPset.id(), edm::getReleaseVersion(), edm::getPassID());
0209 
0210   edm::MakeModuleParams params1(&p1, preg, &prealloc, pc);
0211   edm::MakeModuleParams params2(&p2, preg, &prealloc, pc);
0212 
0213   std::unique_ptr<Maker> lM = std::make_unique<WorkerMaker<ListenMod>>();
0214   ParameterSet l1;
0215   l1.addParameter("@module_type", std::string("ListenMod"));
0216   l1.addParameter("@module_label", std::string("l1"));
0217   l1.addParameter("@module_edm_type", std::string("EDProducer"));
0218   l1.registerIt();
0219 
0220   ParameterSet l2;
0221   l2.addParameter("@module_type", std::string("ListenMod"));
0222   l2.addParameter("@module_label", std::string("l2"));
0223   l2.addParameter("@module_edm_type", std::string("EDProducer"));
0224   l2.registerIt();
0225 
0226   edm::MakeModuleParams paramsl1(&l1, preg, &prealloc, pc);
0227   edm::MakeModuleParams paramsl2(&l2, preg, &prealloc, pc);
0228 
0229   signalslot::Signal<void(const ModuleDescription&)> aSignal;
0230   auto ml1 = lM->makeModule(paramsl1, aSignal, aSignal);
0231   std::unique_ptr<Worker> wl1 = ml1->makeWorker(&table);
0232   auto ml2 = lM->makeModule(paramsl2, aSignal, aSignal);
0233   std::unique_ptr<Worker> wl2 = ml2->makeWorker(&table);
0234   auto m1 = f->makeModule(params1, aSignal, aSignal);
0235   std::unique_ptr<Worker> w1 = m1->makeWorker(&table);
0236   auto m2 = f->makeModule(params2, aSignal, aSignal);
0237   std::unique_ptr<Worker> w2 = m2->makeWorker(&table);
0238 
0239   //Would be 10 products
0240   // 1 from the module 't1'
0241   //    1 from 'l1' in response
0242   //       1 from 'l2' in response to 'l1' <-- circular
0243   //    1 from 'l2' in response to 't1'                  |
0244   //       1 from 'l1' in response to 'l2' <-- circular /
0245   // 1 from the module 't2'
0246   //    1 from 'l1' in response
0247   //       1 from 'l2' in response to 'l1'
0248   //    1 from 'l2' in response to 't2'
0249   //       1 from 'l1' in response to 'l2'
0250   //std::cout <<"# products "<<preg.size()<<std::endl;
0251   CPPUNIT_ASSERT(10 == preg.size());
0252 }
0253 
0254 void testEDProducerProductRegistryCallback::testTwoListeners() {
0255   using namespace edm;
0256 
0257   SignallingProductRegistry preg;
0258 
0259   std::unique_ptr<Maker> f = std::make_unique<WorkerMaker<TestMod>>();
0260 
0261   ParameterSet p1;
0262   p1.addParameter("@module_type", std::string("TestMod"));
0263   p1.addParameter("@module_label", std::string("t1"));
0264   p1.addParameter("@module_edm_type", std::string("EDProducer"));
0265   p1.registerIt();
0266 
0267   ParameterSet p2;
0268   p2.addParameter("@module_type", std::string("TestMod"));
0269   p2.addParameter("@module_label", std::string("t2"));
0270   p2.addParameter("@module_edm_type", std::string("EDProducer"));
0271   p2.registerIt();
0272 
0273   edm::ExceptionToActionTable table;
0274   edm::PreallocationConfiguration prealloc;
0275 
0276   edm::ParameterSet dummyProcessPset;
0277   dummyProcessPset.registerIt();
0278   auto pc =
0279       std::make_shared<ProcessConfiguration>("PROD", dummyProcessPset.id(), edm::getReleaseVersion(), edm::getPassID());
0280 
0281   edm::MakeModuleParams params1(&p1, preg, &prealloc, pc);
0282   edm::MakeModuleParams params2(&p2, preg, &prealloc, pc);
0283 
0284   std::unique_ptr<Maker> lM = std::make_unique<WorkerMaker<ListenMod>>();
0285   ParameterSet l1;
0286   l1.addParameter("@module_type", std::string("ListenMod"));
0287   l1.addParameter("@module_label", std::string("l1"));
0288   l1.addParameter("@module_edm_type", std::string("EDProducer"));
0289   l1.registerIt();
0290 
0291   std::unique_ptr<Maker> lFM = std::make_unique<WorkerMaker<ListenFloatMod>>();
0292   ParameterSet l2;
0293   l2.addParameter("@module_type", std::string("ListenMod"));
0294   l2.addParameter("@module_label", std::string("l2"));
0295   l2.addParameter("@module_edm_type", std::string("EDProducer"));
0296   l2.registerIt();
0297 
0298   edm::MakeModuleParams paramsl1(&l1, preg, &prealloc, pc);
0299   edm::MakeModuleParams paramsl2(&l2, preg, &prealloc, pc);
0300 
0301   signalslot::Signal<void(const ModuleDescription&)> aSignal;
0302   auto m1 = f->makeModule(params1, aSignal, aSignal);
0303   std::unique_ptr<Worker> w1 = m1->makeWorker(&table);
0304   auto ml1 = lM->makeModule(paramsl1, aSignal, aSignal);
0305   std::unique_ptr<Worker> wl1 = ml1->makeWorker(&table);
0306   auto ml2 = lFM->makeModule(paramsl2, aSignal, aSignal);
0307   std::unique_ptr<Worker> wl2 = ml2->makeWorker(&table);
0308   auto m2 = f->makeModule(params2, aSignal, aSignal);
0309   std::unique_ptr<Worker> w2 = m2->makeWorker(&table);
0310 
0311   //Should be 8 products
0312   // 1 from the module 't1'
0313   //    1 from 'l1' in response
0314   //       1 from 'l2' in response to 'l1'
0315   //    1 from 'l2' in response to 't1'
0316   // 1 from the module 't2'
0317   //    1 from 'l1' in response
0318   //       1 from 'l2' in response to 'l1'
0319   //    1 from 'l2' in response to 't2'
0320   //std::cout <<"# products "<<preg.size()<<std::endl;
0321   CPPUNIT_ASSERT(8 == preg.size());
0322 }