File indexing completed on 2025-04-13 22:49:50
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.watchLookupInitializationComplete(this,
0018 &PathsAndConsumesOfModulesTestService::lookupInitializationComplete);
0019 }
0020
0021 static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0022 edm::ParameterSetDescription desc;
0023
0024 edm::ParameterSetDescription validator;
0025 validator.add<std::string>("key");
0026 validator.add<std::vector<std::string>>("value");
0027 desc.addVPSet("modulesAndConsumes", validator, std::vector<edm::ParameterSet>());
0028
0029 descriptions.addWithDefaultLabel(desc);
0030 descriptions.setComment("This service is intended to be used in framework tests.");
0031 }
0032
0033 void lookupInitializationComplete(edm::PathsAndConsumesOfModulesBase const& pathsAndConsumes,
0034 edm::ProcessContext const&) const {
0035 auto const& allModules = pathsAndConsumes.allModules();
0036 for (auto const& moduleToCheck : modulesConsumes_) {
0037 auto found =
0038 std::find_if(allModules.begin(), allModules.end(), [&moduleToCheck](edm::ModuleDescription const* desc) {
0039 return desc->moduleLabel() == moduleToCheck.first;
0040 });
0041 if (found == allModules.end()) {
0042 cms::Exception ex("TestFailure");
0043 ex << "Module " << moduleToCheck.first
0044 << " not found in PathsAndConsumesOfModulesBase, that has the following modules:\n";
0045 for (edm::ModuleDescription const* desc : allModules) {
0046 if (desc) {
0047 ex << " " << desc->moduleLabel() << "\n";
0048 } else {
0049 ex << " nullptr\n";
0050 }
0051 }
0052 throw ex;
0053 }
0054
0055 std::set<std::string> tocheck(moduleToCheck.second.begin(), moduleToCheck.second.end());
0056 for (edm::ModuleDescription const* desc : pathsAndConsumes.modulesWhoseProductsAreConsumedBy((*found)->id())) {
0057 auto found = tocheck.find(desc->moduleLabel());
0058 if (found == tocheck.end()) {
0059 cms::Exception ex("TestFailure");
0060 ex << "Module " << moduleToCheck.first << " consumes " << desc->moduleLabel()
0061 << " that was not one of the expected modules:\n";
0062 for (auto const& m : moduleToCheck.second) {
0063 ex << " " << m << "\n";
0064 }
0065 throw ex;
0066 }
0067 tocheck.erase(found);
0068 }
0069 if (not tocheck.empty()) {
0070 cms::Exception ex("TestFailure");
0071 ex << "Module " << moduleToCheck.first << " was expected to consume the following modules, but it did not\n";
0072 for (auto const& m : tocheck) {
0073 ex << " " << m << "\n";
0074 }
0075 throw ex;
0076 }
0077 }
0078 }
0079
0080 private:
0081 std::vector<std::pair<std::string, std::vector<std::string>>> modulesConsumes_;
0082 };
0083 }
0084
0085 using edmtest::PathsAndConsumesOfModulesTestService;
0086 DEFINE_FWK_SERVICE(PathsAndConsumesOfModulesTestService);