Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-06-17 01:30:22

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 
0013 #include "FWCore/Framework/interface/SignallingProductRegistryFiller.h"
0014 #include "FWCore/Framework/interface/PreallocationConfiguration.h"
0015 
0016 #include "FWCore/Framework/interface/global/EDProducer.h"
0017 #include "FWCore/Framework/interface/ExceptionActions.h"
0018 #include "DataFormats/Provenance/interface/ProductRegistry.h"
0019 #include "DataFormats/Provenance/interface/ProcessConfiguration.h"
0020 #include "FWCore/Framework/interface/maker/ModuleMaker.h"
0021 #include "FWCore/Framework/interface/maker/MakeModuleParams.h"
0022 #include "FWCore/Framework/interface/maker/WorkerT.h"
0023 
0024 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0025 
0026 #include "FWCore/Utilities/interface/TypeID.h"
0027 
0028 #include "makeDummyProcessConfiguration.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   SignallingProductRegistryFiller preg;
0115 
0116   std::unique_ptr<ModuleMakerBase> f = std::make_unique<ModuleMaker<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 = edmtest::makeSharedDummyProcessConfiguration("PROD", dummyProcessPset.id());
0136 
0137   edm::MakeModuleParams params1(&p1, preg, &prealloc, pc);
0138   edm::MakeModuleParams params2(&p2, preg, &prealloc, pc);
0139 
0140   std::unique_ptr<ModuleMakerBase> lM = std::make_unique<ModuleMaker<ListenMod>>();
0141   ParameterSet l1;
0142   l1.addParameter("@module_type", std::string("ListenMod"));
0143   l1.addParameter("@module_label", std::string("l1"));
0144   l1.addParameter("@module_edm_type", std::string("EDProducer"));
0145   l1.registerIt();
0146 
0147   ParameterSet l2;
0148   l2.addParameter("@module_type", std::string("ListenMod"));
0149   l2.addParameter("@module_label", std::string("l2"));
0150   l2.addParameter("@module_edm_type", std::string("EDProducer"));
0151   l2.registerIt();
0152 
0153   edm::MakeModuleParams paramsl1(&l1, preg, &prealloc, pc);
0154   edm::MakeModuleParams paramsl2(&l2, preg, &prealloc, pc);
0155 
0156   signalslot::Signal<void(const ModuleDescription&)> aSignal;
0157 
0158   auto m1 = f->makeModule(params1, aSignal, aSignal);
0159   std::unique_ptr<Worker> w1 = m1->makeWorker(&table);
0160   auto ml1 = lM->makeModule(paramsl1, aSignal, aSignal);
0161   std::unique_ptr<Worker> wl1 = ml1->makeWorker(&table);
0162   auto ml2 = lM->makeModule(paramsl2, aSignal, aSignal);
0163   std::unique_ptr<Worker> wl2 = ml2->makeWorker(&table);
0164   auto m2 = f->makeModule(params2, aSignal, aSignal);
0165   std::unique_ptr<Worker> w2 = m2->makeWorker(&table);
0166 
0167   //Should be 5 products
0168   // 1 from the module 't1'
0169   //    1 from 'l1' in response
0170   //       1 from 'l2' in response to 'l1'
0171   //    1 from 'l2' in response to 't1'
0172   //       1 from 'l1' in response to 'l2'
0173   // 1 from the module 't2'
0174   //    1 from 'l1' in response
0175   //       1 from 'l2' in response to 'l1'
0176   //    1 from 'l2' in response to 't2'
0177   //       1 from 'l1' in response to 'l2'
0178   //std::cout <<"# products "<<preg.size()<<std::endl;
0179   CPPUNIT_ASSERT(10 == preg.registry().size());
0180 }
0181 
0182 void testEDProducerProductRegistryCallback::testCircularRef2() {
0183   using namespace edm;
0184 
0185   SignallingProductRegistryFiller preg;
0186 
0187   std::unique_ptr<ModuleMakerBase> f = std::make_unique<ModuleMaker<TestMod>>();
0188 
0189   ParameterSet p1;
0190   p1.addParameter("@module_type", std::string("TestMod"));
0191   p1.addParameter("@module_label", std::string("t1"));
0192   p1.addParameter("@module_edm_type", std::string("EDProducer"));
0193   p1.registerIt();
0194 
0195   ParameterSet p2;
0196   p2.addParameter("@module_type", std::string("TestMod"));
0197   p2.addParameter("@module_label", std::string("t2"));
0198   p2.addParameter("@module_edm_type", std::string("EDProducer"));
0199   p2.registerIt();
0200 
0201   edm::ExceptionToActionTable table;
0202   edm::PreallocationConfiguration prealloc;
0203 
0204   edm::ParameterSet dummyProcessPset;
0205   dummyProcessPset.registerIt();
0206   auto pc = edmtest::makeSharedDummyProcessConfiguration("PROD", dummyProcessPset.id());
0207 
0208   edm::MakeModuleParams params1(&p1, preg, &prealloc, pc);
0209   edm::MakeModuleParams params2(&p2, preg, &prealloc, pc);
0210 
0211   std::unique_ptr<ModuleMakerBase> lM = std::make_unique<ModuleMaker<ListenMod>>();
0212   ParameterSet l1;
0213   l1.addParameter("@module_type", std::string("ListenMod"));
0214   l1.addParameter("@module_label", std::string("l1"));
0215   l1.addParameter("@module_edm_type", std::string("EDProducer"));
0216   l1.registerIt();
0217 
0218   ParameterSet l2;
0219   l2.addParameter("@module_type", std::string("ListenMod"));
0220   l2.addParameter("@module_label", std::string("l2"));
0221   l2.addParameter("@module_edm_type", std::string("EDProducer"));
0222   l2.registerIt();
0223 
0224   edm::MakeModuleParams paramsl1(&l1, preg, &prealloc, pc);
0225   edm::MakeModuleParams paramsl2(&l2, preg, &prealloc, pc);
0226 
0227   signalslot::Signal<void(const ModuleDescription&)> aSignal;
0228   auto ml1 = lM->makeModule(paramsl1, aSignal, aSignal);
0229   std::unique_ptr<Worker> wl1 = ml1->makeWorker(&table);
0230   auto ml2 = lM->makeModule(paramsl2, aSignal, aSignal);
0231   std::unique_ptr<Worker> wl2 = ml2->makeWorker(&table);
0232   auto m1 = f->makeModule(params1, aSignal, aSignal);
0233   std::unique_ptr<Worker> w1 = m1->makeWorker(&table);
0234   auto m2 = f->makeModule(params2, aSignal, aSignal);
0235   std::unique_ptr<Worker> w2 = m2->makeWorker(&table);
0236 
0237   //Would be 10 products
0238   // 1 from the module 't1'
0239   //    1 from 'l1' in response
0240   //       1 from 'l2' in response to 'l1' <-- circular
0241   //    1 from 'l2' in response to 't1'                  |
0242   //       1 from 'l1' in response to 'l2' <-- circular /
0243   // 1 from the module 't2'
0244   //    1 from 'l1' in response
0245   //       1 from 'l2' in response to 'l1'
0246   //    1 from 'l2' in response to 't2'
0247   //       1 from 'l1' in response to 'l2'
0248   //std::cout <<"# products "<<preg.size()<<std::endl;
0249   CPPUNIT_ASSERT(10 == preg.registry().size());
0250 }
0251 
0252 void testEDProducerProductRegistryCallback::testTwoListeners() {
0253   using namespace edm;
0254 
0255   SignallingProductRegistryFiller preg;
0256 
0257   std::unique_ptr<ModuleMakerBase> f = std::make_unique<ModuleMaker<TestMod>>();
0258 
0259   ParameterSet p1;
0260   p1.addParameter("@module_type", std::string("TestMod"));
0261   p1.addParameter("@module_label", std::string("t1"));
0262   p1.addParameter("@module_edm_type", std::string("EDProducer"));
0263   p1.registerIt();
0264 
0265   ParameterSet p2;
0266   p2.addParameter("@module_type", std::string("TestMod"));
0267   p2.addParameter("@module_label", std::string("t2"));
0268   p2.addParameter("@module_edm_type", std::string("EDProducer"));
0269   p2.registerIt();
0270 
0271   edm::ExceptionToActionTable table;
0272   edm::PreallocationConfiguration prealloc;
0273 
0274   edm::ParameterSet dummyProcessPset;
0275   dummyProcessPset.registerIt();
0276   auto pc = edmtest::makeSharedDummyProcessConfiguration("PROD", dummyProcessPset.id());
0277 
0278   edm::MakeModuleParams params1(&p1, preg, &prealloc, pc);
0279   edm::MakeModuleParams params2(&p2, preg, &prealloc, pc);
0280 
0281   std::unique_ptr<ModuleMakerBase> lM = std::make_unique<ModuleMaker<ListenMod>>();
0282   ParameterSet l1;
0283   l1.addParameter("@module_type", std::string("ListenMod"));
0284   l1.addParameter("@module_label", std::string("l1"));
0285   l1.addParameter("@module_edm_type", std::string("EDProducer"));
0286   l1.registerIt();
0287 
0288   std::unique_ptr<ModuleMakerBase> lFM = std::make_unique<ModuleMaker<ListenFloatMod>>();
0289   ParameterSet l2;
0290   l2.addParameter("@module_type", std::string("ListenMod"));
0291   l2.addParameter("@module_label", std::string("l2"));
0292   l2.addParameter("@module_edm_type", std::string("EDProducer"));
0293   l2.registerIt();
0294 
0295   edm::MakeModuleParams paramsl1(&l1, preg, &prealloc, pc);
0296   edm::MakeModuleParams paramsl2(&l2, preg, &prealloc, pc);
0297 
0298   signalslot::Signal<void(const ModuleDescription&)> aSignal;
0299   auto m1 = f->makeModule(params1, aSignal, aSignal);
0300   std::unique_ptr<Worker> w1 = m1->makeWorker(&table);
0301   auto ml1 = lM->makeModule(paramsl1, aSignal, aSignal);
0302   std::unique_ptr<Worker> wl1 = ml1->makeWorker(&table);
0303   auto ml2 = lFM->makeModule(paramsl2, aSignal, aSignal);
0304   std::unique_ptr<Worker> wl2 = ml2->makeWorker(&table);
0305   auto m2 = f->makeModule(params2, aSignal, aSignal);
0306   std::unique_ptr<Worker> w2 = m2->makeWorker(&table);
0307 
0308   //Should be 8 products
0309   // 1 from the module 't1'
0310   //    1 from 'l1' in response
0311   //       1 from 'l2' in response to 'l1'
0312   //    1 from 'l2' in response to 't1'
0313   // 1 from the module 't2'
0314   //    1 from 'l1' in response
0315   //       1 from 'l2' in response to 'l1'
0316   //    1 from 'l2' in response to 't2'
0317   //std::cout <<"# products "<<preg.size()<<std::endl;
0318   CPPUNIT_ASSERT(8 == preg.registry().size());
0319 }