Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:47:16

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