CMS 3D CMS Logo

PathsAndConsumesOfModulesTestService.cc
Go to the documentation of this file.
6 
7 #include <set>
8 #include <string>
9 #include <utility>
10 #include <vector>
11 
12 namespace edmtest {
14  public:
16  : modulesConsumes_(pset.getParameter<decltype(modulesConsumes_)>("modulesAndConsumes")) {
18  }
19 
20  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
22 
24  validator.add<std::string>("key");
25  validator.add<std::vector<std::string>>("value");
26  desc.addVPSet("modulesAndConsumes", validator, std::vector<edm::ParameterSet>());
27 
28  descriptions.addWithDefaultLabel(desc);
29  descriptions.setComment("This service is intended to be used in framework tests.");
30  }
31 
32  void preBeginJob(edm::PathsAndConsumesOfModulesBase const& pathsAndConsumes, edm::ProcessContext const&) const {
33  auto const& allModules = pathsAndConsumes.allModules();
34  for (auto const& moduleToCheck : modulesConsumes_) {
35  auto found =
36  std::find_if(allModules.begin(), allModules.end(), [&moduleToCheck](edm::ModuleDescription const* desc) {
37  return desc->moduleLabel() == moduleToCheck.first;
38  });
39  if (found == allModules.end()) {
40  cms::Exception ex("TestFailure");
41  ex << "Module " << moduleToCheck.first
42  << " not found in PathsAndConsumesOfModulesBase, that has the following modules:\n";
43  for (edm::ModuleDescription const* desc : allModules) {
44  if (desc) {
45  ex << " " << desc->moduleLabel() << "\n";
46  } else {
47  ex << " nullptr\n";
48  }
49  }
50  throw ex;
51  }
52 
53  std::set<std::string> tocheck(moduleToCheck.second.begin(), moduleToCheck.second.end());
54  for (edm::ModuleDescription const* desc : pathsAndConsumes.modulesWhoseProductsAreConsumedBy((*found)->id())) {
55  auto found = tocheck.find(desc->moduleLabel());
56  if (found == tocheck.end()) {
57  cms::Exception ex("TestFailure");
58  ex << "Module " << moduleToCheck.first << " consumes " << desc->moduleLabel()
59  << " that was not one of the expected modules:\n";
60  for (auto const& m : moduleToCheck.second) {
61  ex << " " << m << "\n";
62  }
63  throw ex;
64  }
65  tocheck.erase(found);
66  }
67  if (not tocheck.empty()) {
68  cms::Exception ex("TestFailure");
69  ex << "Module " << moduleToCheck.first << " was expected to consume the following modules, but it did not\n";
70  for (auto const& m : tocheck) {
71  ex << " " << m << "\n";
72  }
73  throw ex;
74  }
75  }
76  }
77 
78  private:
79  std::vector<std::pair<std::string, std::vector<std::string>>> modulesConsumes_;
80  };
81 } // namespace edmtest
82 
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
std::vector< ModuleDescription const * > const & allModules() const
std::vector< std::pair< std::string, std::vector< std::string > > > modulesConsumes_
ParameterDescriptionBase * add(U const &iLabel, T const &value)
void preBeginJob(edm::PathsAndConsumesOfModulesBase const &pathsAndConsumes, edm::ProcessContext const &) const
#define DEFINE_FWK_SERVICE(type)
Definition: ServiceMaker.h:97
void setComment(std::string const &value)
PathsAndConsumesOfModulesTestService(edm::ParameterSet const &pset, edm::ActivityRegistry &iRegistry)
void watchPreBeginJob(PreBeginJob::slot_type const &iSlot)
convenience function for attaching to signal
std::vector< ModuleDescription const * > const & modulesWhoseProductsAreConsumedBy(unsigned int moduleID, BranchType branchType=InEvent) const
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)