Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-09-16 00:05:37

0001 #include "catch.hpp"
0002 
0003 #include "FWCore/Framework/src/Factory.h"
0004 #include "FWCore/Framework/interface/global/EDProducer.h"
0005 #include "FWCore/Framework/interface/MakerMacros.h"
0006 #include "FWCore/Framework/interface/PreallocationConfiguration.h"
0007 #include "FWCore/PluginManager/interface/PluginManager.h"
0008 #include "FWCore/PluginManager/interface/standard.h"
0009 #include "DataFormats/Provenance/interface/ProductRegistry.h"
0010 
0011 #include "TestTypeResolvers.h"
0012 
0013 #include <iostream>
0014 
0015 namespace edm::test {
0016   class FactoryTestAProd : public edm::global::EDProducer<> {
0017   public:
0018     explicit FactoryTestAProd(edm::ParameterSet const&) { ++count_; }
0019     void produce(StreamID, edm::Event&, edm::EventSetup const&) const final {}
0020     static int count_;
0021   };
0022   int FactoryTestAProd::count_ = 0;
0023 
0024   namespace cpu {
0025     class FactoryTestAProd : public edm::global::EDProducer<> {
0026     public:
0027       explicit FactoryTestAProd(edm::ParameterSet const&) { ++count_; }
0028       void produce(StreamID, edm::Event&, edm::EventSetup const&) const final {}
0029       static int count_;
0030     };
0031     int FactoryTestAProd::count_ = 0;
0032   }  // namespace cpu
0033   namespace other {
0034     class FactoryTestAProd : public edm::global::EDProducer<> {
0035     public:
0036       explicit FactoryTestAProd(edm::ParameterSet const&) { ++count_; }
0037       void produce(StreamID, edm::Event&, edm::EventSetup const&) const final {}
0038       static int count_;
0039     };
0040     int FactoryTestAProd::count_ = 0;
0041   }  // namespace other
0042 }  // namespace edm::test
0043 
0044 DEFINE_FWK_MODULE(edm::test::FactoryTestAProd);
0045 namespace edm::test {
0046   using FactoryTestBProd = FactoryTestAProd;
0047 }
0048 DEFINE_FWK_MODULE(edm::test::FactoryTestBProd);
0049 DEFINE_FWK_MODULE(edm::test::cpu::FactoryTestAProd);
0050 DEFINE_FWK_MODULE(edm::test::other::FactoryTestAProd);
0051 namespace edm::test::cpu {
0052   using FactoryTestCProd = FactoryTestAProd;
0053 }
0054 DEFINE_FWK_MODULE(edm::test::cpu::FactoryTestCProd);
0055 
0056 static bool called = false;
0057 using namespace edm;
0058 TEST_CASE("test edm::Factory", "[Factory]") {
0059   signalslot::Signal<void(const ModuleDescription&)> pre;
0060   signalslot::Signal<void(const ModuleDescription&)> post;
0061   ProductRegistry prodReg;
0062   PreallocationConfiguration preallocConfig;
0063   std::shared_ptr<ProcessConfiguration const> procConfig = std::make_shared<ProcessConfiguration>();
0064   if (not called) {
0065     edmplugin::PluginManager::configure(edmplugin::standard::config());
0066     called = true;
0067   }
0068 
0069   SECTION("test missing plugin") {
0070     auto factory = Factory::get();
0071     ParameterSet pset;
0072     pset.addParameter<std::string>("@module_type", "DoesNotExistModule");
0073     pset.addParameter<std::string>("@module_edm_type", "EDProducer");
0074 
0075     CHECK_THROWS(
0076         factory->makeModule(MakeModuleParams(&pset, prodReg, &preallocConfig, procConfig), nullptr, pre, post));
0077     try {
0078       factory->makeModule(MakeModuleParams(&pset, prodReg, &preallocConfig, procConfig), nullptr, pre, post);
0079     } catch (cms::Exception const& iE) {
0080       REQUIRE(std::string(iE.what()).find("DoesNotExistModule") != std::string::npos);
0081     }
0082   }
0083   SECTION("test missing plugin with simple resolver") {
0084     auto factory = Factory::get();
0085     ParameterSet pset;
0086     pset.addParameter<std::string>("@module_type", "DoesNotExistModule");
0087     pset.addParameter<std::string>("@module_edm_type", "EDProducer");
0088     edm::test::SimpleTestTypeResolver resolver;
0089     using Catch::Matchers::Contains;
0090     CHECK_THROWS_WITH(
0091         factory->makeModule(MakeModuleParams(&pset, prodReg, &preallocConfig, procConfig), &resolver, pre, post),
0092         Contains("DoesNotExistModule"));
0093   }
0094   SECTION("test missing plugin with complex resolver") {
0095     auto factory = Factory::get();
0096     ParameterSet pset;
0097     pset.addParameter<std::string>("@module_type", "generic::DoesNotExistModule");
0098     pset.addParameter<std::string>("@module_edm_type", "EDProducer");
0099     edm::test::ComplexTestTypeResolver resolver;
0100     using Catch::Matchers::Contains;
0101     CHECK_THROWS_WITH(
0102         factory->makeModule(MakeModuleParams(&pset, prodReg, &preallocConfig, procConfig), &resolver, pre, post),
0103         Contains("generic::DoesNotExistModule") && Contains("edm::test::other::DoesNotExistModule") &&
0104             Contains("edm::test::cpu::DoesNotExistModule"));
0105   }
0106 
0107   SECTION("test found plugin") {
0108     auto factory = Factory::get();
0109     ParameterSet pset;
0110     pset.addParameter<std::string>("@module_type", "edm::test::FactoryTestAProd");
0111     pset.addParameter<std::string>("@module_label", "a");
0112     pset.addParameter<std::string>("@module_edm_type", "EDProducer");
0113 
0114     REQUIRE(edm::test::FactoryTestAProd::count_ == 0);
0115     REQUIRE(factory->makeModule(MakeModuleParams(&pset, prodReg, &preallocConfig, procConfig), nullptr, pre, post));
0116     REQUIRE(edm::test::FactoryTestAProd::count_ == 1);
0117     edm::test::FactoryTestAProd::count_ = 0;
0118   }
0119   SECTION("test found plugin with simple resolver") {
0120     auto factory = Factory::get();
0121     ParameterSet pset;
0122     pset.addParameter<std::string>("@module_type", "edm::test::FactoryTestBProd");
0123     pset.addParameter<std::string>("@module_label", "b");
0124     pset.addParameter<std::string>("@module_edm_type", "EDProducer");
0125     edm::test::SimpleTestTypeResolver resolver;
0126     REQUIRE(edm::test::FactoryTestBProd::count_ == 0);
0127     REQUIRE(factory->makeModule(MakeModuleParams(&pset, prodReg, &preallocConfig, procConfig), &resolver, pre, post));
0128     REQUIRE(edm::test::FactoryTestBProd::count_ == 1);
0129     edm::test::FactoryTestBProd::count_ = 0;
0130   }
0131   SECTION("test found plugin with complex resolver") {
0132     SECTION("find other") {
0133       auto factory = Factory::get();
0134       ParameterSet pset;
0135       pset.addParameter<std::string>("@module_type", "generic::FactoryTestAProd");
0136       pset.addParameter<std::string>("@module_label", "gen");
0137       pset.addParameter<std::string>("@module_edm_type", "EDProducer");
0138       edm::test::ComplexTestTypeResolver resolver;
0139       REQUIRE(edm::test::cpu::FactoryTestAProd::count_ == 0);
0140       REQUIRE(edm::test::other::FactoryTestAProd::count_ == 0);
0141       REQUIRE(factory->makeModule(MakeModuleParams(&pset, prodReg, &preallocConfig, procConfig), &resolver, pre, post));
0142       REQUIRE(edm::test::cpu::FactoryTestAProd::count_ == 0);
0143       REQUIRE(edm::test::other::FactoryTestAProd::count_ == 1);
0144       edm::test::other::FactoryTestAProd::count_ = 0;
0145     }
0146     SECTION("find cpu") {
0147       auto factory = Factory::get();
0148       ParameterSet pset;
0149       pset.addParameter<std::string>("@module_type", "generic::FactoryTestCProd");
0150       pset.addParameter<std::string>("@module_label", "cgen");
0151       pset.addParameter<std::string>("@module_edm_type", "EDProducer");
0152       edm::test::ComplexTestTypeResolver resolver;
0153       REQUIRE(edm::test::cpu::FactoryTestCProd::count_ == 0);
0154       REQUIRE(factory->makeModule(MakeModuleParams(&pset, prodReg, &preallocConfig, procConfig), &resolver, pre, post));
0155       REQUIRE(edm::test::cpu::FactoryTestCProd::count_ == 1);
0156       edm::test::cpu::FactoryTestCProd::count_ = 0;
0157     }
0158   }
0159 }