Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-03-13 02:31:56

0001 /**
0002    \file
0003    test for ProductRegistry
0004 
0005    \author Stefano ARGIRO
0006    \date 21 July 2005
0007 */
0008 
0009 #include "DataFormats/Provenance/interface/ProductDescription.h"
0010 #include "DataFormats/Provenance/interface/ProcessConfiguration.h"
0011 #include "DataFormats/TestObjects/interface/ToyProducts.h"
0012 #include "FWCore/Framework/interface/EventProcessor.h"
0013 #include "FWCore/Framework/interface/SignallingProductRegistryFiller.h"
0014 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0015 #include "FWCore/PluginManager/interface/ProblemTracker.h"
0016 #include "FWCore/Utilities/interface/Exception.h"
0017 #include "FWCore/Reflection/interface/TypeWithDict.h"
0018 #include "FWCore/ParameterSetReader/interface/ParameterSetReader.h"
0019 
0020 #include "cppunit/extensions/HelperMacros.h"
0021 
0022 #include <memory>
0023 
0024 #include <iostream>
0025 
0026 class testProductRegistry : public CppUnit::TestFixture {
0027   CPPUNIT_TEST_SUITE(testProductRegistry);
0028 
0029   CPPUNIT_TEST(testSignal);
0030   CPPUNIT_TEST(testWatch);
0031   CPPUNIT_TEST_EXCEPTION(testCircular, cms::Exception);
0032 
0033   CPPUNIT_TEST(testProductRegistration);
0034   CPPUNIT_TEST(testAddAlias);
0035 
0036   CPPUNIT_TEST_SUITE_END();
0037 
0038 public:
0039   testProductRegistry();
0040   void setUp();
0041   void tearDown();
0042   void testSignal();
0043   void testWatch();
0044   void testCircular();
0045   void testProductRegistration();
0046   void testAddAlias();
0047 
0048 private:
0049   std::shared_ptr<edm::ProductDescription> intBranch_;
0050   std::shared_ptr<edm::ProductDescription> floatBranch_;
0051   std::shared_ptr<edm::ProductDescription> intVecBranch_;
0052   std::shared_ptr<edm::ProductDescription> simpleVecBranch_;
0053   std::shared_ptr<edm::ProductDescription> simpleDerivedVecBranch_;
0054 };
0055 
0056 ///registration of the test so that the runner can find it
0057 CPPUNIT_TEST_SUITE_REGISTRATION(testProductRegistry);
0058 
0059 namespace {
0060   struct Listener {
0061     int* heard_;
0062     Listener(int& hear) : heard_(&hear) {}
0063     void operator()(edm::ProductDescription const&) { ++(*heard_); }
0064   };
0065 
0066   struct Responder {
0067     std::string name_;
0068     edm::SignallingProductRegistryFiller* reg_;
0069     Responder(std::string const& iName, edm::SignallingProductRegistryFiller& iReg) : name_(iName), reg_(&iReg) {
0070       iReg.watchProductAdditions(this, &Responder::respond);
0071     }
0072     void respond(edm::ProductDescription const& iDesc) {
0073       edm::ParameterSet dummyProcessPset;
0074       dummyProcessPset.registerIt();
0075       auto pc = std::make_shared<edm::ProcessConfiguration>();
0076       pc->setParameterSetID(dummyProcessPset.id());
0077 
0078       edm::ProductDescription prod(iDesc.branchType(),
0079                                    name_,
0080                                    iDesc.processName(),
0081                                    iDesc.fullClassName(),
0082                                    iDesc.friendlyClassName(),
0083                                    iDesc.productInstanceName() + "-" + name_,
0084                                    iDesc.unwrappedType());
0085       reg_->addProduct(prod);
0086     }
0087   };
0088 }  // namespace
0089 
0090 testProductRegistry::testProductRegistry() {}
0091 
0092 void testProductRegistry::setUp() {
0093   edm::ParameterSet dummyProcessPset;
0094   dummyProcessPset.registerIt();
0095   auto processConfiguration = std::make_shared<edm::ProcessConfiguration>();
0096   processConfiguration->setParameterSetID(dummyProcessPset.id());
0097 
0098   edm::ParameterSet pset;
0099   pset.registerIt();
0100   intBranch_ = std::make_shared<edm::ProductDescription>(
0101       edm::InEvent, "labeli", "PROD", "int", "int", "int", edm::TypeWithDict(typeid(int)));
0102 
0103   floatBranch_ = std::make_shared<edm::ProductDescription>(
0104       edm::InEvent, "labelf", "PROD", "float", "float", "float", edm::TypeWithDict(typeid(float)));
0105 
0106   intVecBranch_ = std::make_shared<edm::ProductDescription>(
0107       edm::InEvent, "labelvi", "PROD", "std::vector<int>", "ints", "vint", edm::TypeWithDict(typeid(std::vector<int>)));
0108 
0109   simpleVecBranch_ =
0110       std::make_shared<edm::ProductDescription>(edm::InEvent,
0111                                                 "labelovsimple",
0112                                                 "PROD",
0113                                                 "edm::OwnVector<edmtest::Simple>",
0114                                                 "edmtestSimplesOwned",
0115                                                 "ovsimple",
0116                                                 edm::TypeWithDict(typeid(edm::OwnVector<edmtest::Simple>)));
0117   simpleDerivedVecBranch_ =
0118       std::make_shared<edm::ProductDescription>(edm::InEvent,
0119                                                 "labelovsimplederived",
0120                                                 "PROD",
0121                                                 "edm::OwnVector<edmtest::SimpleDerived>",
0122                                                 "edmtestSimpleDerivedsOwned",
0123                                                 "ovsimplederived",
0124                                                 edm::TypeWithDict(typeid(edm::OwnVector<edmtest::SimpleDerived>)));
0125 }
0126 
0127 namespace {
0128   template <class T>
0129   void kill_and_clear(std::shared_ptr<T>& p) {
0130     p.reset();
0131   }
0132 }  // namespace
0133 
0134 void testProductRegistry::tearDown() {
0135   kill_and_clear(floatBranch_);
0136   kill_and_clear(intBranch_);
0137 }
0138 
0139 void testProductRegistry::testSignal() {
0140   using namespace edm;
0141   SignallingProductRegistryFiller reg;
0142 
0143   int hear = 0;
0144   Listener listening(hear);
0145   reg.productAddedSignal_.connect(listening);
0146 
0147   //ProductDescription prod(InEvent, "label", "PROD", "int", "int", "int", md);
0148 
0149   //   reg.addProduct(prod);
0150   reg.addProduct(*intBranch_);
0151   CPPUNIT_ASSERT(1 == hear);
0152 }
0153 
0154 void testProductRegistry::testWatch() {
0155   using namespace edm;
0156   SignallingProductRegistryFiller reg;
0157 
0158   int hear = 0;
0159   Listener listening(hear);
0160   reg.watchProductAdditions(listening);
0161   reg.watchProductAdditions(listening, &Listener::operator());
0162 
0163   Responder one("one", reg);
0164 
0165   //ProductDescription prod(InEvent, "label", "PROD", "int", "int", "int");
0166   //reg.addProduct(prod);
0167   reg.addProduct(*intBranch_);
0168 
0169   //ProductDescription prod2(InEvent, "label", "PROD", "float", "float", "float");
0170   //   reg.addProduct(prod2);
0171   reg.addProduct(*floatBranch_);
0172 
0173   //Should be 4 products
0174   // 1 from the 'int' in this routine
0175   // 1 from 'one' responding to this call
0176   // 1 from the 'float'
0177   // 1 from 'one' responding to the original call
0178   CPPUNIT_ASSERT(4 * 2 == hear);
0179   CPPUNIT_ASSERT(4 == reg.registry().size());
0180 }
0181 void testProductRegistry::testCircular() {
0182   using namespace edm;
0183   SignallingProductRegistryFiller reg;
0184 
0185   int hear = 0;
0186   Listener listening(hear);
0187   reg.watchProductAdditions(listening);
0188   reg.watchProductAdditions(listening, &Listener::operator());
0189 
0190   Responder one("one", reg);
0191   Responder two("two", reg);
0192 
0193   //ProductDescription prod(InEvent, "label","PROD","int","int","int");
0194   //reg.addProduct(prod);
0195   reg.addProduct(*intBranch_);
0196 
0197   //Should be 5 products
0198   // 1 from the original 'add' in this routine
0199   // 1 from 'one' responding to this call
0200   // 1 from 'two' responding to 'one'
0201   // 1 from 'two' responding to the original call
0202   // 1 from 'one' responding to 'two'
0203   CPPUNIT_ASSERT(5 * 2 == hear);
0204   CPPUNIT_ASSERT(5 == reg.registry().size());
0205 }
0206 
0207 void testProductRegistry::testProductRegistration() {
0208   edm::AssertHandler ah;
0209 
0210   std::string configuration(
0211       "import FWCore.ParameterSet.Config as cms\n"
0212       "process = cms.Process('TEST')\n"
0213       "process.maxEvents = cms.untracked.PSet(\n"
0214       "  input = cms.untracked.int32(-1))\n"
0215       "process.source = cms.Source('EmptySource')\n"
0216       "process.m1 = cms.EDProducer('TestPRegisterModule1')\n"
0217       "process.m2 = cms.EDProducer('TestPRegisterModule2')\n"
0218       "process.p = cms.Path(process.m1*process.m2)\n");
0219   try {
0220     edm::EventProcessor proc(edm::getPSetFromConfig(configuration));
0221   } catch (cms::Exception const& iException) {
0222     std::cout << "caught " << iException.explainSelf() << std::endl;
0223     throw;
0224   }
0225 }
0226 
0227 void testProductRegistry::testAddAlias() {
0228   edm::SignallingProductRegistryFiller reg;
0229 
0230   reg.addProduct(*intBranch_);
0231   reg.addLabelAlias(*intBranch_, "aliasi", "instanceAlias");
0232 
0233   reg.addProduct(*floatBranch_);
0234   reg.addLabelAlias(*floatBranch_, "aliasf", "instanceAlias");
0235 
0236   reg.addProduct(*intVecBranch_);
0237   reg.addLabelAlias(*intVecBranch_, "aliasvi", "instanceAlias");
0238 
0239   reg.addProduct(*simpleVecBranch_);
0240   reg.addLabelAlias(*simpleVecBranch_, "aliasovsimple", "instanceAlias");
0241 
0242   reg.addProduct(*simpleDerivedVecBranch_);
0243   reg.addLabelAlias(*simpleDerivedVecBranch_, "aliasovsimple", "instanceAlias");
0244 
0245   std::set<edm::TypeID> productTypesConsumed{intBranch_->unwrappedTypeID(),
0246                                              floatBranch_->unwrappedTypeID(),
0247                                              intVecBranch_->unwrappedTypeID(),
0248                                              simpleVecBranch_->unwrappedTypeID(),
0249                                              simpleDerivedVecBranch_->unwrappedTypeID()};
0250   std::set<edm::TypeID> elementTypesConsumed{intBranch_->unwrappedTypeID(), edm::TypeID(typeid(edmtest::Simple))};
0251   reg.setFrozen(productTypesConsumed, elementTypesConsumed, "TEST");
0252   {
0253     auto notFound =
0254         reg.registry().aliasToModules(edm::PRODUCT_TYPE, intBranch_->unwrappedTypeID(), "alias", "instance");
0255     CPPUNIT_ASSERT(notFound.empty());
0256   }
0257   {
0258     auto found =
0259         reg.registry().aliasToModules(edm::PRODUCT_TYPE, intBranch_->unwrappedTypeID(), "aliasi", "instanceAlias");
0260     CPPUNIT_ASSERT(found.size() == 1);
0261     CPPUNIT_ASSERT(found[0] == "labeli");
0262   }
0263   {
0264     auto found =
0265         reg.registry().aliasToModules(edm::PRODUCT_TYPE, floatBranch_->unwrappedTypeID(), "aliasf", "instanceAlias");
0266     CPPUNIT_ASSERT(found.size() == 1);
0267     CPPUNIT_ASSERT(found[0] == "labelf");
0268   }
0269   {
0270     auto found =
0271         reg.registry().aliasToModules(edm::PRODUCT_TYPE, intVecBranch_->unwrappedTypeID(), "aliasvi", "instanceAlias");
0272     CPPUNIT_ASSERT(found.size() == 1);
0273     CPPUNIT_ASSERT(found[0] == "labelvi");
0274   }
0275   {
0276     auto found =
0277         reg.registry().aliasToModules(edm::ELEMENT_TYPE, intBranch_->unwrappedTypeID(), "aliasvi", "instanceAlias");
0278     CPPUNIT_ASSERT(found.size() == 1);
0279     CPPUNIT_ASSERT(found[0] == "labelvi");
0280   }
0281   {
0282     auto found = reg.registry().aliasToModules(
0283         edm::ELEMENT_TYPE, edm::TypeID(typeid(edmtest::Simple)), "aliasovsimple", "instanceAlias");
0284     CPPUNIT_ASSERT(found.size() == 2);
0285     CPPUNIT_ASSERT(std::find(found.begin(), found.end(), "labelovsimple") != found.end());
0286     CPPUNIT_ASSERT(std::find(found.begin(), found.end(), "labelovsimplederived") != found.end());
0287   }
0288 }