Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:12:29

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::SimpleTestTypeResolverMaker 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::ComplexTestTypeResolverMaker 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   SECTION("test missing plugin with configurable resolver") {
0107     auto factory = Factory::get();
0108     ParameterSet pset;
0109     pset.addParameter<std::string>("@module_type", "generic::DoesNotExistModule");
0110     pset.addParameter<std::string>("@module_edm_type", "EDProducer");
0111     SECTION("default behavior") {
0112       edm::test::ConfigurableTestTypeResolverMaker resolver;
0113       using Catch::Matchers::Contains;
0114       CHECK_THROWS_WITH(
0115           factory->makeModule(MakeModuleParams(&pset, prodReg, &preallocConfig, procConfig), &resolver, pre, post),
0116           Contains("generic::DoesNotExistModule") && Contains("edm::test::other::DoesNotExistModule") &&
0117               Contains("edm::test::cpu::DoesNotExistModule"));
0118     }
0119     SECTION("set variant to other") {
0120       pset.addUntrackedParameter<std::string>("variant", "other");
0121       edm::test::ConfigurableTestTypeResolverMaker resolver;
0122       using Catch::Matchers::Contains;
0123       CHECK_THROWS_WITH(
0124           factory->makeModule(MakeModuleParams(&pset, prodReg, &preallocConfig, procConfig), &resolver, pre, post),
0125           Contains("generic::DoesNotExistModule") && Contains("edm::test::other::DoesNotExistModule") &&
0126               not Contains("edm::test::cpu::DoesNotExistModule"));
0127     }
0128     SECTION("set variant to cpu") {
0129       pset.addUntrackedParameter<std::string>("variant", "cpu");
0130       edm::test::ConfigurableTestTypeResolverMaker resolver;
0131       using Catch::Matchers::Contains;
0132       CHECK_THROWS_WITH(
0133           factory->makeModule(MakeModuleParams(&pset, prodReg, &preallocConfig, procConfig), &resolver, pre, post),
0134           Contains("generic::DoesNotExistModule") && not Contains("edm::test::other::DoesNotExistModule") &&
0135               Contains("edm::test::cpu::DoesNotExistModule"));
0136     }
0137   }
0138 
0139   SECTION("test found plugin") {
0140     auto factory = Factory::get();
0141     ParameterSet pset;
0142     pset.addParameter<std::string>("@module_type", "edm::test::FactoryTestAProd");
0143     pset.addParameter<std::string>("@module_label", "a");
0144     pset.addParameter<std::string>("@module_edm_type", "EDProducer");
0145 
0146     REQUIRE(edm::test::FactoryTestAProd::count_ == 0);
0147     REQUIRE(factory->makeModule(MakeModuleParams(&pset, prodReg, &preallocConfig, procConfig), nullptr, pre, post));
0148     REQUIRE(edm::test::FactoryTestAProd::count_ == 1);
0149     edm::test::FactoryTestAProd::count_ = 0;
0150   }
0151   SECTION("test found plugin with simple resolver") {
0152     auto factory = Factory::get();
0153     ParameterSet pset;
0154     pset.addParameter<std::string>("@module_type", "edm::test::FactoryTestBProd");
0155     pset.addParameter<std::string>("@module_label", "b");
0156     pset.addParameter<std::string>("@module_edm_type", "EDProducer");
0157     edm::test::SimpleTestTypeResolverMaker resolver;
0158     REQUIRE(edm::test::FactoryTestBProd::count_ == 0);
0159     REQUIRE(factory->makeModule(MakeModuleParams(&pset, prodReg, &preallocConfig, procConfig), &resolver, pre, post));
0160     REQUIRE(edm::test::FactoryTestBProd::count_ == 1);
0161     edm::test::FactoryTestBProd::count_ = 0;
0162   }
0163   SECTION("test found plugin with complex resolver") {
0164     SECTION("find other") {
0165       auto factory = Factory::get();
0166       ParameterSet pset;
0167       pset.addParameter<std::string>("@module_type", "generic::FactoryTestAProd");
0168       pset.addParameter<std::string>("@module_label", "gen");
0169       pset.addParameter<std::string>("@module_edm_type", "EDProducer");
0170       edm::test::ComplexTestTypeResolverMaker resolver;
0171       REQUIRE(edm::test::cpu::FactoryTestAProd::count_ == 0);
0172       REQUIRE(edm::test::other::FactoryTestAProd::count_ == 0);
0173       REQUIRE(factory->makeModule(MakeModuleParams(&pset, prodReg, &preallocConfig, procConfig), &resolver, pre, post));
0174       REQUIRE(edm::test::cpu::FactoryTestAProd::count_ == 0);
0175       REQUIRE(edm::test::other::FactoryTestAProd::count_ == 1);
0176       edm::test::other::FactoryTestAProd::count_ = 0;
0177     }
0178     SECTION("find cpu") {
0179       auto factory = Factory::get();
0180       ParameterSet pset;
0181       pset.addParameter<std::string>("@module_type", "generic::FactoryTestCProd");
0182       pset.addParameter<std::string>("@module_label", "cgen");
0183       pset.addParameter<std::string>("@module_edm_type", "EDProducer");
0184       edm::test::ComplexTestTypeResolverMaker resolver;
0185       REQUIRE(edm::test::cpu::FactoryTestCProd::count_ == 0);
0186       REQUIRE(factory->makeModule(MakeModuleParams(&pset, prodReg, &preallocConfig, procConfig), &resolver, pre, post));
0187       REQUIRE(edm::test::cpu::FactoryTestCProd::count_ == 1);
0188       edm::test::cpu::FactoryTestCProd::count_ = 0;
0189     }
0190   }
0191   SECTION("test found plugin with configurable resolver") {
0192     auto factory = Factory::get();
0193     ParameterSet pset;
0194     pset.addParameter<std::string>("@module_type", "generic::FactoryTestAProd");
0195     pset.addParameter<std::string>("@module_label", "gen");
0196     pset.addParameter<std::string>("@module_edm_type", "EDProducer");
0197     SECTION("default behavior") {
0198       edm::test::ConfigurableTestTypeResolverMaker resolver;
0199       REQUIRE(edm::test::cpu::FactoryTestAProd::count_ == 0);
0200       REQUIRE(edm::test::other::FactoryTestAProd::count_ == 0);
0201       REQUIRE(factory->makeModule(MakeModuleParams(&pset, prodReg, &preallocConfig, procConfig), &resolver, pre, post));
0202       REQUIRE(edm::test::cpu::FactoryTestAProd::count_ == 0);
0203       REQUIRE(edm::test::other::FactoryTestAProd::count_ == 1);
0204       edm::test::other::FactoryTestAProd::count_ = 0;
0205     }
0206     SECTION("set variant to cpu") {
0207       pset.addUntrackedParameter<std::string>("variant", "cpu");
0208       edm::test::ConfigurableTestTypeResolverMaker resolver;
0209       REQUIRE(edm::test::cpu::FactoryTestAProd::count_ == 0);
0210       REQUIRE(edm::test::other::FactoryTestAProd::count_ == 0);
0211       REQUIRE(factory->makeModule(MakeModuleParams(&pset, prodReg, &preallocConfig, procConfig), &resolver, pre, post));
0212       REQUIRE(edm::test::cpu::FactoryTestAProd::count_ == 1);
0213       REQUIRE(edm::test::other::FactoryTestAProd::count_ == 0);
0214       edm::test::cpu::FactoryTestAProd::count_ = 0;
0215     }
0216     SECTION("set variant to other") {
0217       pset.addUntrackedParameter<std::string>("variant", "other");
0218       edm::test::ConfigurableTestTypeResolverMaker resolver;
0219       REQUIRE(edm::test::cpu::FactoryTestAProd::count_ == 0);
0220       REQUIRE(edm::test::other::FactoryTestAProd::count_ == 0);
0221       REQUIRE(factory->makeModule(MakeModuleParams(&pset, prodReg, &preallocConfig, procConfig), &resolver, pre, post));
0222       REQUIRE(edm::test::cpu::FactoryTestAProd::count_ == 0);
0223       REQUIRE(edm::test::other::FactoryTestAProd::count_ == 1);
0224       edm::test::other::FactoryTestAProd::count_ = 0;
0225     }
0226   }
0227 }