Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-04-20 23:56:38

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