File indexing completed on 2024-04-06 12:12:34
0001 #include "DataFormats/Provenance/interface/ModuleDescription.h"
0002 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0003 #include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"
0004 #include "FWCore/ServiceRegistry/interface/PathsAndConsumesOfModulesBase.h"
0005 #include "FWCore/ServiceRegistry/interface/ServiceMaker.h"
0006
0007 #include <set>
0008 #include <string>
0009 #include <utility>
0010 #include <vector>
0011
0012 namespace edmtest {
0013 class PathsAndConsumesOfModulesTestService {
0014 public:
0015 PathsAndConsumesOfModulesTestService(edm::ParameterSet const& pset, edm::ActivityRegistry& iRegistry)
0016 : modulesConsumes_(pset.getParameter<decltype(modulesConsumes_)>("modulesAndConsumes")) {
0017 iRegistry.watchPreBeginJob(this, &PathsAndConsumesOfModulesTestService::preBeginJob);
0018 }
0019
0020 static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0021 edm::ParameterSetDescription desc;
0022
0023 edm::ParameterSetDescription validator;
0024 validator.add<std::string>("key");
0025 validator.add<std::vector<std::string>>("value");
0026 desc.addVPSet("modulesAndConsumes", validator, std::vector<edm::ParameterSet>());
0027
0028 descriptions.addWithDefaultLabel(desc);
0029 descriptions.setComment("This service is intended to be used in framework tests.");
0030 }
0031
0032 void preBeginJob(edm::PathsAndConsumesOfModulesBase const& pathsAndConsumes, edm::ProcessContext const&) const {
0033 auto const& allModules = pathsAndConsumes.allModules();
0034 for (auto const& moduleToCheck : modulesConsumes_) {
0035 auto found =
0036 std::find_if(allModules.begin(), allModules.end(), [&moduleToCheck](edm::ModuleDescription const* desc) {
0037 return desc->moduleLabel() == moduleToCheck.first;
0038 });
0039 if (found == allModules.end()) {
0040 cms::Exception ex("TestFailure");
0041 ex << "Module " << moduleToCheck.first
0042 << " not found in PathsAndConsumesOfModulesBase, that has the following modules:\n";
0043 for (edm::ModuleDescription const* desc : allModules) {
0044 if (desc) {
0045 ex << " " << desc->moduleLabel() << "\n";
0046 } else {
0047 ex << " nullptr\n";
0048 }
0049 }
0050 throw ex;
0051 }
0052
0053 std::set<std::string> tocheck(moduleToCheck.second.begin(), moduleToCheck.second.end());
0054 for (edm::ModuleDescription const* desc : pathsAndConsumes.modulesWhoseProductsAreConsumedBy((*found)->id())) {
0055 auto found = tocheck.find(desc->moduleLabel());
0056 if (found == tocheck.end()) {
0057 cms::Exception ex("TestFailure");
0058 ex << "Module " << moduleToCheck.first << " consumes " << desc->moduleLabel()
0059 << " that was not one of the expected modules:\n";
0060 for (auto const& m : moduleToCheck.second) {
0061 ex << " " << m << "\n";
0062 }
0063 throw ex;
0064 }
0065 tocheck.erase(found);
0066 }
0067 if (not tocheck.empty()) {
0068 cms::Exception ex("TestFailure");
0069 ex << "Module " << moduleToCheck.first << " was expected to consume the following modules, but it did not\n";
0070 for (auto const& m : tocheck) {
0071 ex << " " << m << "\n";
0072 }
0073 throw ex;
0074 }
0075 }
0076 }
0077
0078 private:
0079 std::vector<std::pair<std::string, std::vector<std::string>>> modulesConsumes_;
0080 };
0081 }
0082
0083 using edmtest::PathsAndConsumesOfModulesTestService;
0084 DEFINE_FWK_SERVICE(PathsAndConsumesOfModulesTestService);