Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-01-13 01:43:38

0001 /*
0002  *  eventsetup_t.cc
0003  *  EDMProto
0004  *
0005  *  Created by Chris Jones on 3/24/05.
0006  *  Changed by Viji Sundararajan on 24-Jun-05.
0007  *
0008  */
0009 
0010 // Note that repeatedly in this test we add the modules directly to
0011 // the EventSetupProvider instead of having the controller use the
0012 // plugin system and ModuleFactory to add the modules. This works OK
0013 // in tests as long as there are no SubProcesses, otherwise this needs
0014 // to be done as in a real job where the modules are added as plugins
0015 // through the parameter set passed to the controller.
0016 
0017 #include "DataFormats/Provenance/interface/EventID.h"
0018 #include "DataFormats/Provenance/interface/Timestamp.h"
0019 #include "FWCore/Framework/interface/ComponentDescription.h"
0020 #include "FWCore/Framework/interface/DataProxyProvider.h"
0021 #include "FWCore/Framework/interface/EDConsumerBase.h"
0022 #include "FWCore/Framework/interface/ConsumesCollector.h"
0023 #include "FWCore/Framework/interface/ESHandle.h"
0024 #include "FWCore/Framework/interface/ESTransientHandle.h"
0025 #include "FWCore/Framework/interface/ESRecordsToProxyIndices.h"
0026 #include "FWCore/Framework/interface/EventSetupImpl.h"
0027 #include "FWCore/Framework/interface/EventSetupProvider.h"
0028 #include "FWCore/Framework/interface/EventSetupRecordIntervalFinder.h"
0029 #include "FWCore/Framework/interface/IOVSyncValue.h"
0030 #include "FWCore/Framework/interface/RecordDependencyRegister.h"
0031 #include "FWCore/Framework/interface/NoRecordException.h"
0032 #include "FWCore/Framework/interface/MakeDataException.h"
0033 #include "FWCore/Framework/interface/ValidityInterval.h"
0034 
0035 #include "FWCore/Framework/src/SynchronousEventSetupsController.h"
0036 
0037 #include "FWCore/Framework/test/DummyData.h"
0038 #include "FWCore/Framework/test/DummyEventSetupRecordRetriever.h"
0039 #include "FWCore/Framework/test/DummyProxyProvider.h"
0040 #include "FWCore/Framework/test/DummyRecord.h"
0041 
0042 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0043 #include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"
0044 #include "FWCore/Utilities/interface/Exception.h"
0045 #include "FWCore/Utilities/interface/ESProductTag.h"
0046 #include "FWCore/Concurrency/interface/ThreadsController.h"
0047 
0048 #include "cppunit/extensions/HelperMacros.h"
0049 
0050 #include <memory>
0051 #include <optional>
0052 #include <string>
0053 #include <vector>
0054 
0055 using namespace edm;
0056 using namespace edm::eventsetup;
0057 using edm::eventsetup::test::DummyData;
0058 using edm::eventsetup::test::DummyProxyProvider;
0059 
0060 namespace {
0061 
0062   bool non_null(const void* iPtr) { return iPtr != nullptr; }
0063 
0064   edm::ParameterSet createDummyPset() {
0065     edm::ParameterSet pset;
0066     std::vector<std::string> emptyVStrings;
0067     pset.addParameter<std::vector<std::string>>("@all_esprefers", emptyVStrings);
0068     pset.addParameter<std::vector<std::string>>("@all_essources", emptyVStrings);
0069     pset.addParameter<std::vector<std::string>>("@all_esmodules", emptyVStrings);
0070     return pset;
0071   }
0072 }  // namespace
0073 
0074 class testEventsetup : public CppUnit::TestFixture {
0075   CPPUNIT_TEST_SUITE(testEventsetup);
0076 
0077   CPPUNIT_TEST(constructTest);
0078   CPPUNIT_TEST(getTest);
0079   CPPUNIT_TEST(tryToGetTest);
0080   CPPUNIT_TEST_EXCEPTION(getExcTest, edm::eventsetup::NoRecordException<DummyRecord>);
0081   CPPUNIT_TEST(provenanceTest);
0082   CPPUNIT_TEST(getDataWithESGetTokenTest);
0083   CPPUNIT_TEST(getHandleWithESGetTokenTest);
0084   CPPUNIT_TEST(getTransientHandleWithESGetTokenTest);
0085   CPPUNIT_TEST(recordValidityTest);
0086   CPPUNIT_TEST_EXCEPTION(recordValidityExcTest, edm::eventsetup::NoRecordException<DummyRecord>);
0087   CPPUNIT_TEST(recordValidityNoFinderTest);
0088   CPPUNIT_TEST_EXCEPTION(recordValidityNoFinderExcTest, edm::eventsetup::NoRecordException<DummyRecord>);
0089   CPPUNIT_TEST(recordValidityProxyNoFinderTest);
0090   CPPUNIT_TEST_EXCEPTION(recordValidityProxyNoFinderExcTest, edm::eventsetup::NoRecordException<DummyRecord>);
0091 
0092   CPPUNIT_TEST_EXCEPTION(producerConflictTest, cms::Exception);
0093   CPPUNIT_TEST_EXCEPTION(sourceConflictTest, cms::Exception);
0094   CPPUNIT_TEST(twoSourceTest);
0095   CPPUNIT_TEST(sourceProducerResolutionTest);
0096   CPPUNIT_TEST(preferTest);
0097 
0098   CPPUNIT_TEST(introspectionTest);
0099 
0100   CPPUNIT_TEST(iovExtensionTest);
0101   CPPUNIT_TEST(resetProxiesTest);
0102 
0103   CPPUNIT_TEST_SUITE_END();
0104 
0105 public:
0106   void setUp() { m_scheduler = std::make_unique<edm::ThreadsController>(1); }
0107   void tearDown() {}
0108 
0109   void constructTest();
0110   void getTest();
0111   void tryToGetTest();
0112   void getExcTest();
0113   void recordValidityTest();
0114   void recordValidityExcTest();
0115   void recordValidityNoFinderTest();
0116   void recordValidityNoFinderExcTest();
0117   void recordValidityProxyNoFinderTest();
0118   void recordValidityProxyNoFinderExcTest();
0119   void provenanceTest();
0120   void getDataWithESGetTokenTest();
0121   void getHandleWithESGetTokenTest();
0122   void getTransientHandleWithESGetTokenTest();
0123 
0124   void producerConflictTest();
0125   void sourceConflictTest();
0126   void twoSourceTest();
0127   void sourceProducerResolutionTest();
0128   void preferTest();
0129 
0130   void introspectionTest();
0131 
0132   void iovExtensionTest();
0133   void resetProxiesTest();
0134 
0135 private:
0136   edm::propagate_const<std::unique_ptr<edm::ThreadsController>> m_scheduler;
0137 
0138   DummyData kGood{1};
0139   DummyData kBad{0};
0140 };
0141 
0142 ///registration of the test so that the runner can find it
0143 CPPUNIT_TEST_SUITE_REGISTRATION(testEventsetup);
0144 
0145 namespace {
0146   edm::ActivityRegistry activityRegistry;
0147 }
0148 
0149 void testEventsetup::constructTest() {
0150   eventsetup::EventSetupProvider provider(&activityRegistry);
0151   const Timestamp time(1);
0152   const IOVSyncValue timestamp(time);
0153   bool newEventSetupImpl = false;
0154   auto eventSetupImpl = provider.eventSetupForInstance(timestamp, newEventSetupImpl);
0155   CPPUNIT_ASSERT(non_null(eventSetupImpl.get()));
0156   edm::ESParentContext pc;
0157   const edm::EventSetup eventSetup(provider.eventSetupImpl(), 0, nullptr, pc);
0158 }
0159 
0160 // Note there is a similar test in dependentrecord_t.cppunit.cc
0161 // named getTest() that tests get and tryToGet using an EventSetupProvider.
0162 // No need to repeat that test here. The next two just test EventSetupImpl
0163 // at the lowest level.
0164 
0165 void testEventsetup::getTest() {
0166   EventSetupImpl eventSetupImpl;
0167   std::vector<eventsetup::EventSetupRecordKey> keys;
0168   EventSetupRecordKey key = EventSetupRecordKey::makeKey<DummyRecord>();
0169   keys.push_back(key);
0170   eventSetupImpl.setKeyIters(keys.begin(), keys.end());
0171   EventSetupRecordImpl dummyRecordImpl{key, &activityRegistry};
0172   eventSetupImpl.addRecordImpl(dummyRecordImpl);
0173   edm::ESParentContext pc;
0174   const edm::EventSetup eventSetup(eventSetupImpl, 0, nullptr, pc);
0175   const DummyRecord& gottenRecord = eventSetup.get<DummyRecord>();
0176   CPPUNIT_ASSERT(&dummyRecordImpl == gottenRecord.impl_);
0177 }
0178 
0179 void testEventsetup::tryToGetTest() {
0180   EventSetupImpl eventSetupImpl;
0181   std::vector<eventsetup::EventSetupRecordKey> keys;
0182   EventSetupRecordKey key = EventSetupRecordKey::makeKey<DummyRecord>();
0183   keys.push_back(key);
0184   eventSetupImpl.setKeyIters(keys.begin(), keys.end());
0185   EventSetupRecordImpl dummyRecordImpl{key, &activityRegistry};
0186   eventSetupImpl.addRecordImpl(dummyRecordImpl);
0187   edm::ESParentContext pc;
0188   const edm::EventSetup eventSetup(eventSetupImpl, 0, nullptr, pc);
0189   std::optional<DummyRecord> gottenRecord = eventSetup.tryToGet<DummyRecord>();
0190   CPPUNIT_ASSERT(gottenRecord);
0191   CPPUNIT_ASSERT(&dummyRecordImpl == gottenRecord.value().impl_);
0192 }
0193 
0194 void testEventsetup::getExcTest() {
0195   SynchronousEventSetupsController controller;
0196   edm::ParameterSet pset = createDummyPset();
0197   EventSetupProvider& provider = *controller.makeProvider(pset, &activityRegistry);
0198   controller.eventSetupForInstance(edm::IOVSyncValue(edm::EventID(1, 1, 1), edm::Timestamp(1)));
0199   edm::ESParentContext pc;
0200   const edm::EventSetup eventSetup(provider.eventSetupImpl(), 0, nullptr, pc);
0201   eventSetup.get<DummyRecord>();
0202 }
0203 
0204 class DummyFinder : public EventSetupRecordIntervalFinder {
0205 public:
0206   DummyFinder() : EventSetupRecordIntervalFinder(), interval_() { this->findingRecord<DummyRecord>(); }
0207 
0208   void setInterval(const ValidityInterval& iInterval) { interval_ = iInterval; }
0209 
0210 protected:
0211   virtual void setIntervalFor(const eventsetup::EventSetupRecordKey&,
0212                               const IOVSyncValue& iTime,
0213                               ValidityInterval& iInterval) {
0214     if (interval_.validFor(iTime)) {
0215       iInterval = interval_;
0216     } else {
0217       iInterval = ValidityInterval();
0218     }
0219   }
0220 
0221 private:
0222   ValidityInterval interval_;
0223 };
0224 
0225 void testEventsetup::recordValidityTest() {
0226   SynchronousEventSetupsController controller;
0227   edm::ParameterSet pset = createDummyPset();
0228   EventSetupProvider& provider = *controller.makeProvider(pset, &activityRegistry);
0229 
0230   std::shared_ptr<DummyFinder> dummyFinder = std::make_shared<DummyFinder>();
0231 
0232   // Note this manner of adding finders works OK in tests as long as there
0233   // are no SubProcesses, otherwise this needs to be done as in a real
0234   // job where they are added as plugins through the pset passed to the controller.
0235   provider.add(std::shared_ptr<edm::EventSetupRecordIntervalFinder>(dummyFinder));
0236 
0237   Timestamp time_1(1);
0238   controller.eventSetupForInstance(IOVSyncValue(time_1));
0239   edm::ESParentContext pc;
0240   const edm::EventSetup eventSetup1(provider.eventSetupImpl(), 0, nullptr, pc);
0241   CPPUNIT_ASSERT(!eventSetup1.tryToGet<DummyRecord>().has_value());
0242 
0243   const Timestamp time_2(2);
0244   dummyFinder->setInterval(ValidityInterval(IOVSyncValue(time_2), IOVSyncValue(Timestamp(3))));
0245   {
0246     controller.eventSetupForInstance(IOVSyncValue(time_2));
0247     edm::ESParentContext pc;
0248     const edm::EventSetup eventSetup(provider.eventSetupImpl(), 0, nullptr, pc);
0249     eventSetup.get<DummyRecord>();
0250     CPPUNIT_ASSERT(eventSetup.tryToGet<DummyRecord>().has_value());
0251   }
0252 
0253   {
0254     controller.eventSetupForInstance(IOVSyncValue(Timestamp(3)));
0255     edm::ESParentContext pc;
0256     const edm::EventSetup eventSetup(provider.eventSetupImpl(), 0, nullptr, pc);
0257     eventSetup.get<DummyRecord>();
0258     CPPUNIT_ASSERT(eventSetup.tryToGet<DummyRecord>().has_value());
0259   }
0260   {
0261     controller.eventSetupForInstance(IOVSyncValue(Timestamp(4)));
0262     edm::ESParentContext pc;
0263     const edm::EventSetup eventSetup(provider.eventSetupImpl(), 0, nullptr, pc);
0264     CPPUNIT_ASSERT(!eventSetup.tryToGet<DummyRecord>().has_value());
0265   }
0266 }
0267 
0268 void testEventsetup::recordValidityExcTest() {
0269   SynchronousEventSetupsController controller;
0270   edm::ParameterSet pset = createDummyPset();
0271   EventSetupProvider& provider = *controller.makeProvider(pset, &activityRegistry);
0272 
0273   std::shared_ptr<DummyFinder> dummyFinder = std::make_shared<DummyFinder>();
0274 
0275   // Note this manner of adding finders works OK in tests as long as there
0276   // are no SubProcesses, otherwise this needs to be done as in a real
0277   // job where they are added as plugins through the pset passed to the controller.
0278   provider.add(std::shared_ptr<edm::EventSetupRecordIntervalFinder>(dummyFinder));
0279 
0280   Timestamp time_1(1);
0281   controller.eventSetupForInstance(IOVSyncValue(time_1));
0282   edm::ESParentContext pc;
0283   const edm::EventSetup eventSetup(provider.eventSetupImpl(), 0, nullptr, pc);
0284   eventSetup.get<DummyRecord>();
0285 }
0286 
0287 void testEventsetup::recordValidityNoFinderTest() {
0288   SynchronousEventSetupsController controller;
0289   edm::ParameterSet pset = createDummyPset();
0290   EventSetupProvider& provider = *controller.makeProvider(pset, &activityRegistry);
0291 
0292   Timestamp time_1(1);
0293   controller.eventSetupForInstance(IOVSyncValue(time_1));
0294   edm::ESParentContext pc;
0295   const edm::EventSetup eventSetup(provider.eventSetupImpl(), 0, nullptr, pc);
0296   CPPUNIT_ASSERT(!eventSetup.tryToGet<DummyRecord>().has_value());
0297 }
0298 
0299 void testEventsetup::recordValidityNoFinderExcTest() {
0300   SynchronousEventSetupsController controller;
0301   edm::ParameterSet pset = createDummyPset();
0302   EventSetupProvider& provider = *controller.makeProvider(pset, &activityRegistry);
0303 
0304   Timestamp time_1(1);
0305   controller.eventSetupForInstance(IOVSyncValue(time_1));
0306   edm::ESParentContext pc;
0307   const edm::EventSetup eventSetup(provider.eventSetupImpl(), 0, nullptr, pc);
0308   eventSetup.get<DummyRecord>();
0309 }
0310 
0311 //create an instance of the register
0312 static eventsetup::RecordDependencyRegister<DummyRecord> s_factory;
0313 
0314 void testEventsetup::recordValidityProxyNoFinderTest() {
0315   SynchronousEventSetupsController controller;
0316   edm::ParameterSet pset = createDummyPset();
0317   EventSetupProvider& provider = *controller.makeProvider(pset, &activityRegistry);
0318 
0319   provider.add(std::make_shared<DummyProxyProvider>());
0320 
0321   Timestamp time_1(1);
0322   controller.eventSetupForInstance(IOVSyncValue(time_1));
0323   edm::ESParentContext pc;
0324   const edm::EventSetup eventSetup(provider.eventSetupImpl(), 0, nullptr, pc);
0325   CPPUNIT_ASSERT(!eventSetup.tryToGet<DummyRecord>().has_value());
0326 }
0327 
0328 void testEventsetup::recordValidityProxyNoFinderExcTest() {
0329   SynchronousEventSetupsController controller;
0330   edm::ParameterSet pset = createDummyPset();
0331   EventSetupProvider& provider = *controller.makeProvider(pset, &activityRegistry);
0332 
0333   provider.add(std::make_shared<DummyProxyProvider>());
0334 
0335   Timestamp time_1(1);
0336   controller.eventSetupForInstance(IOVSyncValue(time_1));
0337   edm::ESParentContext pc;
0338   const edm::EventSetup eventSetup(provider.eventSetupImpl(), 0, nullptr, pc);
0339   eventSetup.get<DummyRecord>();
0340 }
0341 
0342 void testEventsetup::producerConflictTest() {
0343   SynchronousEventSetupsController controller;
0344   edm::ParameterSet pset = createDummyPset();
0345   EventSetupProvider& provider = *controller.makeProvider(pset, &activityRegistry);
0346 
0347   edm::eventsetup::ComponentDescription description("DummyProxyProvider", "", 0, false);
0348 
0349   {
0350     auto dummyProv = std::make_shared<DummyProxyProvider>();
0351     dummyProv->setDescription(description);
0352     provider.add(dummyProv);
0353   }
0354   {
0355     auto dummyProv = std::make_shared<DummyProxyProvider>();
0356     dummyProv->setDescription(description);
0357     provider.add(dummyProv);
0358   }
0359   //checking for conflicts is delayed until first eventSetupForInstance
0360   controller.eventSetupForInstance(IOVSyncValue::invalidIOVSyncValue());
0361 }
0362 
0363 void testEventsetup::sourceConflictTest() {
0364   SynchronousEventSetupsController controller;
0365   edm::ParameterSet pset = createDummyPset();
0366   EventSetupProvider& provider = *controller.makeProvider(pset, &activityRegistry);
0367 
0368   edm::eventsetup::ComponentDescription description("DummyProxyProvider", "", 0, true);
0369 
0370   {
0371     auto dummyProv = std::make_shared<DummyProxyProvider>();
0372     dummyProv->setDescription(description);
0373     provider.add(dummyProv);
0374   }
0375   {
0376     auto dummyProv = std::make_shared<DummyProxyProvider>();
0377     dummyProv->setDescription(description);
0378     provider.add(dummyProv);
0379   }
0380   //checking for conflicts is delayed until first eventSetupForInstance
0381   controller.eventSetupForInstance(IOVSyncValue::invalidIOVSyncValue());
0382 }
0383 
0384 void testEventsetup::twoSourceTest() {
0385   SynchronousEventSetupsController controller;
0386   edm::ParameterSet pset = createDummyPset();
0387   EventSetupProvider& provider = *controller.makeProvider(pset, &activityRegistry);
0388 
0389   edm::eventsetup::ComponentDescription description("DummyProxyProvider", "", 0, true);
0390   {
0391     auto dummyProv = std::make_shared<DummyProxyProvider>();
0392     dummyProv->setDescription(description);
0393     provider.add(dummyProv);
0394   }
0395   {
0396     auto dummyProv = std::make_shared<edm::DummyEventSetupRecordRetriever>();
0397     std::shared_ptr<eventsetup::DataProxyProvider> providerPtr(dummyProv);
0398     std::shared_ptr<edm::EventSetupRecordIntervalFinder> finderPtr(dummyProv);
0399     edm::eventsetup::ComponentDescription description2("DummyEventSetupRecordRetriever", "", 0, true);
0400     dummyProv->setDescription(description2);
0401     provider.add(providerPtr);
0402     provider.add(finderPtr);
0403   }
0404   //checking for conflicts is delayed until first eventSetupForInstance
0405   edm::ESParentContext pc;
0406   controller.eventSetupForInstance(IOVSyncValue::invalidIOVSyncValue());
0407   const edm::EventSetup eventSetup3(provider.eventSetupImpl(), 0, nullptr, pc);
0408   CPPUNIT_ASSERT(!eventSetup3.tryToGet<DummyRecord>().has_value());
0409   CPPUNIT_ASSERT(!eventSetup3.tryToGet<DummyEventSetupRecord>().has_value());
0410   controller.eventSetupForInstance(IOVSyncValue(Timestamp(3)));
0411   const edm::EventSetup eventSetup4(provider.eventSetupImpl(), 0, nullptr, pc);
0412   CPPUNIT_ASSERT(!eventSetup4.tryToGet<DummyRecord>().has_value());
0413   CPPUNIT_ASSERT(eventSetup4.tryToGet<DummyEventSetupRecord>().has_value());
0414   eventSetup4.get<DummyEventSetupRecord>();
0415 }
0416 
0417 namespace {
0418   struct DummyDataConsumer : public EDConsumerBase {
0419     explicit DummyDataConsumer(ESInputTag const& iTag) : m_token{esConsumes(iTag)} {}
0420 
0421     void prefetch(edm::EventSetupImpl const& iImpl) const {
0422       auto const& recs = this->esGetTokenRecordIndicesVector(edm::Transition::Event);
0423       auto const& proxies = this->esGetTokenIndicesVector(edm::Transition::Event);
0424       for (size_t i = 0; i != proxies.size(); ++i) {
0425         auto rec = iImpl.findImpl(recs[i]);
0426         if (rec) {
0427           edm::FinalWaitingTask waitTask;
0428           oneapi::tbb::task_group group;
0429           rec->prefetchAsync(
0430               WaitingTaskHolder(group, &waitTask), proxies[i], &iImpl, edm::ServiceToken{}, edm::ESParentContext{});
0431           do {
0432             group.wait();
0433           } while (not waitTask.done());
0434           if (waitTask.exceptionPtr()) {
0435             std::rethrow_exception(*waitTask.exceptionPtr());
0436           }
0437         }
0438       }
0439     }
0440 
0441     ESGetToken<edm::eventsetup::test::DummyData, edm::DefaultRecord> m_token;
0442     ESGetToken<edm::eventsetup::test::DummyData, edm::DefaultRecord> m_tokenUninitialized;
0443   };
0444 }  // namespace
0445 
0446 void testEventsetup::provenanceTest() {
0447   SynchronousEventSetupsController controller;
0448   edm::ParameterSet pset = createDummyPset();
0449   EventSetupProvider& provider = *controller.makeProvider(pset, &activityRegistry);
0450 
0451   std::shared_ptr<DummyFinder> dummyFinder = std::make_shared<DummyFinder>();
0452   dummyFinder->setInterval(ValidityInterval(IOVSyncValue(Timestamp(2)), IOVSyncValue(Timestamp(3))));
0453   provider.add(std::shared_ptr<edm::EventSetupRecordIntervalFinder>(dummyFinder));
0454 
0455   try {
0456     {
0457       edm::eventsetup::ComponentDescription description("DummyProxyProvider", "", 0, true);
0458       edm::ParameterSet ps;
0459       ps.addParameter<std::string>("name", "test11");
0460       ps.registerIt();
0461       description.pid_ = ps.id();
0462       auto dummyProv = std::make_shared<DummyProxyProvider>(kBad);
0463       dummyProv->setDescription(description);
0464       provider.add(dummyProv);
0465     }
0466     {
0467       edm::eventsetup::ComponentDescription description("DummyProxyProvider", "testLabel", 1, false);
0468       edm::ParameterSet ps;
0469       ps.addParameter<std::string>("name", "test22");
0470       ps.registerIt();
0471       description.pid_ = ps.id();
0472       auto dummyProv = std::make_shared<DummyProxyProvider>(kGood);
0473       dummyProv->setDescription(description);
0474       provider.add(dummyProv);
0475     }
0476     controller.eventSetupForInstance(IOVSyncValue(Timestamp(2)));
0477     DummyDataConsumer consumer{edm::ESInputTag("", "")};
0478     consumer.updateLookup(provider.recordsToProxyIndices());
0479     consumer.prefetch(provider.eventSetupImpl());
0480     edm::ESParentContext pc;
0481     const edm::EventSetup eventSetup(provider.eventSetupImpl(),
0482                                      static_cast<unsigned int>(edm::Transition::Event),
0483                                      consumer.esGetTokenIndices(edm::Transition::Event),
0484                                      pc);
0485     edm::ESHandle<DummyData> data = eventSetup.getHandle(consumer.m_token);
0486 
0487     CPPUNIT_ASSERT(kGood.value_ == data->value_);
0488     const edm::eventsetup::ComponentDescription* desc = data.description();
0489     CPPUNIT_ASSERT(desc->label_ == "testLabel");
0490   } catch (const cms::Exception& iException) {
0491     std::cout << "caught " << iException.explainSelf() << std::endl;
0492     throw;
0493   }
0494 }
0495 
0496 namespace {
0497   //This just tests that the constructs will properly compile
0498   class [[maybe_unused]] EDConsumesCollectorConsumer : public edm::EDConsumerBase {
0499     EDConsumesCollectorConsumer() {
0500       using edm::eventsetup::test::DummyData;
0501       {
0502         [[maybe_unused]] edm::ESGetToken<DummyData, edm::DefaultRecord> token1(
0503             consumesCollector().esConsumes<DummyData, edm::DefaultRecord>());
0504         [[maybe_unused]] edm::ESGetToken<DummyData, edm::DefaultRecord> token2(
0505             consumesCollector().esConsumes<DummyData, edm::DefaultRecord>(edm::ESInputTag("Blah")));
0506         [[maybe_unused]] edm::ESGetToken<DummyData, edm::DefaultRecord> token3(
0507             consumesCollector().esConsumes<DummyData, edm::DefaultRecord, edm::Transition::BeginRun>());
0508         [[maybe_unused]] edm::ESGetToken<DummyData, edm::DefaultRecord> token4(
0509             consumesCollector().esConsumes<DummyData, edm::DefaultRecord, edm::Transition::BeginRun>(
0510                 edm::ESInputTag("Blah")));
0511       }
0512       {
0513         [[maybe_unused]] edm::ESGetToken<DummyData, edm::DefaultRecord> token1(consumesCollector().esConsumes());
0514         [[maybe_unused]] edm::ESGetToken<DummyData, edm::DefaultRecord> token2(
0515             consumesCollector().esConsumes(edm::ESInputTag("Blah")));
0516         [[maybe_unused]] edm::ESGetToken<DummyData, edm::DefaultRecord> token3(
0517             consumesCollector().esConsumes<edm::Transition::BeginRun>());
0518         [[maybe_unused]] edm::ESGetToken<DummyData, edm::DefaultRecord> token4(
0519             consumesCollector().esConsumes<edm::Transition::BeginRun>(edm::ESInputTag("Blah")));
0520       }
0521       {
0522         [[maybe_unused]] edm::ESGetToken<DummyData, edm::DefaultRecord> token1;
0523         token1 = consumesCollector().esConsumes();
0524         [[maybe_unused]] edm::ESGetToken<DummyData, edm::DefaultRecord> token2;
0525         token2 = consumesCollector().esConsumes(edm::ESInputTag("Blah"));
0526         [[maybe_unused]] edm::ESGetToken<DummyData, edm::DefaultRecord> token3;
0527         token3 = consumesCollector().esConsumes<edm::Transition::BeginRun>();
0528         [[maybe_unused]] edm::ESGetToken<DummyData, edm::DefaultRecord> token4;
0529         token4 = consumesCollector().esConsumes<edm::Transition::BeginRun>(edm::ESInputTag("Blah"));
0530       }
0531 
0532     }  // namespace
0533   };
0534 
0535   class ConsumesProducer : public ESProducer {
0536   public:
0537     ConsumesProducer() : token_{setWhatProduced(this, "consumes").consumes<edm::eventsetup::test::DummyData>()} {}
0538     std::unique_ptr<edm::eventsetup::test::DummyData> produce(const DummyRecord& iRecord) {
0539       auto const& data = iRecord.get(token_);
0540       return std::make_unique<edm::eventsetup::test::DummyData>(data);
0541     }
0542 
0543   private:
0544     edm::ESGetToken<edm::eventsetup::test::DummyData, DummyRecord> token_;
0545   };
0546 
0547   class ConsumesFromProducer : public ESProducer {
0548   public:
0549     ConsumesFromProducer()
0550         : token_{setWhatProduced(this, "consumesFrom").consumesFrom<edm::eventsetup::test::DummyData, DummyRecord>()} {}
0551     std::unique_ptr<edm::eventsetup::test::DummyData> produce(const DummyRecord& iRecord) {
0552       auto const& data = iRecord.get(token_);
0553       return std::make_unique<edm::eventsetup::test::DummyData>(data);
0554     }
0555 
0556   private:
0557     edm::ESGetToken<edm::eventsetup::test::DummyData, DummyRecord> token_;
0558   };
0559 
0560   //This is used only to test compilation
0561   class [[maybe_unused]] ESConsumesCollectorProducer : public ESProducer {
0562   public:
0563     struct Helper {
0564       Helper(ESConsumesCollector iCollector) {
0565         using edm::eventsetup::test::DummyData;
0566         {
0567           [[maybe_unused]] edm::ESGetToken<DummyData, edm::DefaultRecord> token1(
0568               iCollector.consumesFrom<DummyData, edm::DefaultRecord>());
0569           [[maybe_unused]] edm::ESGetToken<DummyData, edm::DefaultRecord> token2(
0570               iCollector.consumesFrom<DummyData, edm::DefaultRecord>(edm::ESInputTag("Blah")));
0571         }
0572         {
0573           [[maybe_unused]] edm::ESGetToken<DummyData, edm::DefaultRecord> token1(iCollector.consumes());
0574           [[maybe_unused]] edm::ESGetToken<DummyData, edm::DefaultRecord> token2(
0575               iCollector.consumes(edm::ESInputTag("Blah")));
0576         }
0577       }
0578     };
0579 
0580     ESConsumesCollectorProducer() : helper_(setWhatProduced(this, "consumesCollector")) {}
0581 
0582     std::unique_ptr<edm::eventsetup::test::DummyData> produce(const DummyRecord& iRecord) {
0583       return std::unique_ptr<edm::eventsetup::test::DummyData>();
0584     }
0585 
0586   private:
0587     Helper helper_;
0588   };
0589 
0590   class SetMayConsumeProducer : public ESProducer {
0591   public:
0592     SetMayConsumeProducer(bool iSucceed) : succeed_(iSucceed) {
0593       setWhatProduced(this, label(iSucceed))
0594           .setMayConsume(
0595               token_,
0596               [iSucceed](auto& get, edm::ESTransientHandle<edm::eventsetup::test::DummyData> const& handle) {
0597                 if (iSucceed) {
0598                   return get("", "");
0599                 }
0600                 return get.nothing();
0601               },
0602               edm::ESProductTag<edm::eventsetup::test::DummyData, DummyRecord>("", ""));
0603     }
0604     std::unique_ptr<edm::eventsetup::test::DummyData> produce(const DummyRecord& iRecord) {
0605       auto const& data = iRecord.getHandle(token_);
0606       CPPUNIT_ASSERT(data.isValid() == succeed_);
0607       if (data.isValid()) {
0608         return std::make_unique<edm::eventsetup::test::DummyData>(*data);
0609       }
0610       return std::unique_ptr<edm::eventsetup::test::DummyData>();
0611     }
0612 
0613   private:
0614     static const char* label(bool iSucceed) noexcept {
0615       if (iSucceed) {
0616         return "setMayConsumeSucceed";
0617       }
0618       return "setMayConsumeFail";
0619     }
0620 
0621     edm::ESGetToken<edm::eventsetup::test::DummyData, DummyRecord> token_;
0622     bool succeed_;
0623   };
0624 
0625 }  // namespace
0626 
0627 void testEventsetup::getDataWithESGetTokenTest() {
0628   SynchronousEventSetupsController controller;
0629   edm::ParameterSet pset = createDummyPset();
0630   EventSetupProvider& provider = *controller.makeProvider(pset, &activityRegistry);
0631 
0632   std::shared_ptr<DummyFinder> dummyFinder = std::make_shared<DummyFinder>();
0633   dummyFinder->setInterval(ValidityInterval(IOVSyncValue(Timestamp(2)), IOVSyncValue(Timestamp(3))));
0634   provider.add(std::shared_ptr<edm::EventSetupRecordIntervalFinder>(dummyFinder));
0635 
0636   try {
0637     {
0638       edm::eventsetup::ComponentDescription description("DummyProxyProvider", "testOne", 0, true);
0639       edm::ParameterSet ps;
0640       ps.addParameter<std::string>("name", "test11");
0641       ps.registerIt();
0642       description.pid_ = ps.id();
0643       auto dummyProv = std::make_shared<DummyProxyProvider>(kBad);
0644       dummyProv->setDescription(description);
0645       provider.add(dummyProv);
0646     }
0647     {
0648       edm::eventsetup::ComponentDescription description("DummyProxyProvider", "testTwo", 1, false);
0649       edm::ParameterSet ps;
0650       ps.addParameter<std::string>("name", "test22");
0651       ps.addParameter<std::string>("appendToDataLabel", "blah");
0652       ps.registerIt();
0653       description.pid_ = ps.id();
0654       auto dummyProv = std::make_shared<DummyProxyProvider>(kGood);
0655       dummyProv->setDescription(description);
0656       dummyProv->setAppendToDataLabel(ps);
0657       provider.add(dummyProv);
0658     }
0659     {
0660       edm::eventsetup::ComponentDescription description("ConsumesProducer", "consumes", 2, false);
0661       edm::ParameterSet ps;
0662       ps.addParameter<std::string>("name", "consumes");
0663       ps.registerIt();
0664       description.pid_ = ps.id();
0665       auto dummyProv = std::make_shared<ConsumesProducer>();
0666       dummyProv->setDescription(description);
0667       dummyProv->setAppendToDataLabel(ps);
0668       provider.add(dummyProv);
0669     }
0670     {
0671       edm::eventsetup::ComponentDescription description("ConsumesFromProducer", "consumesFrom", 3, false);
0672       edm::ParameterSet ps;
0673       ps.addParameter<std::string>("name", "consumesFrom");
0674       ps.registerIt();
0675       description.pid_ = ps.id();
0676       auto dummyProv = std::make_shared<ConsumesFromProducer>();
0677       dummyProv->setDescription(description);
0678       dummyProv->setAppendToDataLabel(ps);
0679       provider.add(dummyProv);
0680     }
0681     {
0682       edm::eventsetup::ComponentDescription description("SetMayConsumeProducer", "setMayConsumeSuceed", 4, false);
0683       edm::ParameterSet ps;
0684       ps.addParameter<std::string>("name", "setMayConsumeSuceed");
0685       ps.registerIt();
0686       description.pid_ = ps.id();
0687       auto dummyProv = std::make_shared<SetMayConsumeProducer>(true);
0688       dummyProv->setDescription(description);
0689       dummyProv->setAppendToDataLabel(ps);
0690       provider.add(dummyProv);
0691     }
0692     {
0693       edm::eventsetup::ComponentDescription description("SetMayConsumeProducer", "setMayConsumeFail", 5, false);
0694       edm::ParameterSet ps;
0695       ps.addParameter<std::string>("name", "setMayConsumeFail");
0696       ps.registerIt();
0697       description.pid_ = ps.id();
0698       auto dummyProv = std::make_shared<SetMayConsumeProducer>(false);
0699       dummyProv->setDescription(description);
0700       dummyProv->setAppendToDataLabel(ps);
0701       provider.add(dummyProv);
0702     }
0703 
0704     edm::ESParentContext pc;
0705     controller.eventSetupForInstance(IOVSyncValue(Timestamp(2)));
0706     {
0707       DummyDataConsumer consumer{edm::ESInputTag("", "blah")};
0708       consumer.updateLookup(provider.recordsToProxyIndices());
0709       consumer.prefetch(provider.eventSetupImpl());
0710       EventSetup eventSetup{provider.eventSetupImpl(),
0711                             static_cast<unsigned int>(edm::Transition::Event),
0712                             consumer.esGetTokenIndices(edm::Transition::Event),
0713                             pc};
0714       auto const& data = eventSetup.getData(consumer.m_token);
0715       CPPUNIT_ASSERT(kGood.value_ == data.value_);
0716       bool uninitializedTokenThrewException = false;
0717       try {
0718         (void)eventSetup.getData(consumer.m_tokenUninitialized);
0719       } catch (cms::Exception& ex) {
0720         uninitializedTokenThrewException = true;
0721         CPPUNIT_ASSERT(ex.category() == "InvalidESGetToken");
0722       }
0723       CPPUNIT_ASSERT(uninitializedTokenThrewException);
0724     }
0725 
0726     {
0727       DummyDataConsumer consumer{edm::ESInputTag("", "")};
0728       consumer.updateLookup(provider.recordsToProxyIndices());
0729       consumer.prefetch(provider.eventSetupImpl());
0730       EventSetup eventSetup{provider.eventSetupImpl(),
0731                             static_cast<unsigned int>(edm::Transition::Event),
0732                             consumer.esGetTokenIndices(edm::Transition::Event),
0733                             pc};
0734       const DummyData& data = eventSetup.getData(consumer.m_token);
0735       CPPUNIT_ASSERT(kBad.value_ == data.value_);
0736     }
0737 
0738     {
0739       DummyDataConsumer consumer{edm::ESInputTag("testTwo", "blah")};
0740       consumer.updateLookup(provider.recordsToProxyIndices());
0741       consumer.prefetch(provider.eventSetupImpl());
0742       EventSetup eventSetup{provider.eventSetupImpl(),
0743                             static_cast<unsigned int>(edm::Transition::Event),
0744                             consumer.esGetTokenIndices(edm::Transition::Event),
0745                             pc};
0746       auto const& data = eventSetup.getData(consumer.m_token);
0747       CPPUNIT_ASSERT(kGood.value_ == data.value_);
0748     }
0749 
0750     {
0751       DummyDataConsumer consumer{edm::ESInputTag("DoesNotExist", "blah")};
0752       consumer.updateLookup(provider.recordsToProxyIndices());
0753       consumer.prefetch(provider.eventSetupImpl());
0754       EventSetup eventSetup{provider.eventSetupImpl(),
0755                             static_cast<unsigned int>(edm::Transition::Event),
0756                             consumer.esGetTokenIndices(edm::Transition::Event),
0757                             pc};
0758       CPPUNIT_ASSERT_THROW(eventSetup.getData(consumer.m_token), cms::Exception);
0759     }
0760 
0761     {
0762       DummyDataConsumer consumer{edm::ESInputTag("", "consumes")};
0763       consumer.updateLookup(provider.recordsToProxyIndices());
0764       consumer.prefetch(provider.eventSetupImpl());
0765       EventSetup eventSetup{provider.eventSetupImpl(),
0766                             static_cast<unsigned int>(edm::Transition::Event),
0767                             consumer.esGetTokenIndices(edm::Transition::Event),
0768                             pc};
0769       const DummyData& data = eventSetup.getData(consumer.m_token);
0770       CPPUNIT_ASSERT(kBad.value_ == data.value_);
0771     }
0772     {
0773       DummyDataConsumer consumer{edm::ESInputTag("", "consumesFrom")};
0774       consumer.updateLookup(provider.recordsToProxyIndices());
0775       consumer.prefetch(provider.eventSetupImpl());
0776       EventSetup eventSetup{provider.eventSetupImpl(),
0777                             static_cast<unsigned int>(edm::Transition::Event),
0778                             consumer.esGetTokenIndices(edm::Transition::Event),
0779                             pc};
0780       const DummyData& data = eventSetup.getData(consumer.m_token);
0781       CPPUNIT_ASSERT(kBad.value_ == data.value_);
0782     }
0783     {
0784       DummyDataConsumer consumer{edm::ESInputTag("", "setMayConsumeFail")};
0785       consumer.updateLookup(provider.recordsToProxyIndices());
0786       consumer.prefetch(provider.eventSetupImpl());
0787       EventSetup eventSetup{provider.eventSetupImpl(),
0788                             static_cast<unsigned int>(edm::Transition::Event),
0789                             consumer.esGetTokenIndices(edm::Transition::Event),
0790                             pc};
0791       CPPUNIT_ASSERT_THROW(eventSetup.getData(consumer.m_token), edm::eventsetup::MakeDataException);
0792     }
0793     {
0794       DummyDataConsumer consumer{edm::ESInputTag("", "setMayConsumeSucceed")};
0795       consumer.updateLookup(provider.recordsToProxyIndices());
0796       consumer.prefetch(provider.eventSetupImpl());
0797       EventSetup eventSetup{provider.eventSetupImpl(),
0798                             static_cast<unsigned int>(edm::Transition::Event),
0799                             consumer.esGetTokenIndices(edm::Transition::Event),
0800                             pc};
0801       const DummyData& data = eventSetup.getData(consumer.m_token);
0802       CPPUNIT_ASSERT(kBad.value_ == data.value_);
0803     }
0804 
0805   } catch (const cms::Exception& iException) {
0806     std::cout << "caught " << iException.explainSelf() << std::endl;
0807     throw;
0808   }
0809 }
0810 
0811 void testEventsetup::getHandleWithESGetTokenTest() {
0812   SynchronousEventSetupsController controller;
0813   edm::ParameterSet pset = createDummyPset();
0814   EventSetupProvider& provider = *controller.makeProvider(pset, &activityRegistry);
0815 
0816   std::shared_ptr<DummyFinder> dummyFinder = std::make_shared<DummyFinder>();
0817   dummyFinder->setInterval(ValidityInterval(IOVSyncValue(Timestamp(2)), IOVSyncValue(Timestamp(3))));
0818   provider.add(std::shared_ptr<edm::EventSetupRecordIntervalFinder>(dummyFinder));
0819 
0820   edm::ESParentContext pc;
0821   try {
0822     {
0823       edm::eventsetup::ComponentDescription description("DummyProxyProvider", "testOne", 0, true);
0824       edm::ParameterSet ps;
0825       ps.addParameter<std::string>("name", "test11");
0826       ps.registerIt();
0827       description.pid_ = ps.id();
0828       auto dummyProv = std::make_shared<DummyProxyProvider>(kBad);
0829       dummyProv->setDescription(description);
0830       provider.add(dummyProv);
0831     }
0832     {
0833       edm::eventsetup::ComponentDescription description("DummyProxyProvider", "testTwo", 1, false);
0834       edm::ParameterSet ps;
0835       ps.addParameter<std::string>("name", "test22");
0836       ps.addParameter<std::string>("appendToDataLabel", "blah");
0837       ps.registerIt();
0838       description.pid_ = ps.id();
0839       auto dummyProv = std::make_shared<DummyProxyProvider>(kGood);
0840       dummyProv->setDescription(description);
0841       dummyProv->setAppendToDataLabel(ps);
0842       provider.add(dummyProv);
0843     }
0844     controller.eventSetupForInstance(IOVSyncValue(Timestamp(2)));
0845     {
0846       DummyDataConsumer consumer{edm::ESInputTag("", "blah")};
0847       consumer.updateLookup(provider.recordsToProxyIndices());
0848       consumer.prefetch(provider.eventSetupImpl());
0849 
0850       EventSetup eventSetup{provider.eventSetupImpl(),
0851                             static_cast<unsigned int>(edm::Transition::Event),
0852                             consumer.esGetTokenIndices(edm::Transition::Event),
0853                             pc};
0854       edm::ESHandle<DummyData> data = eventSetup.getHandle(consumer.m_token);
0855       CPPUNIT_ASSERT(kGood.value_ == data->value_);
0856       const edm::eventsetup::ComponentDescription* desc = data.description();
0857       CPPUNIT_ASSERT(desc->label_ == "testTwo");
0858     }
0859 
0860     {
0861       DummyDataConsumer consumer{edm::ESInputTag("", "")};
0862       consumer.updateLookup(provider.recordsToProxyIndices());
0863       consumer.prefetch(provider.eventSetupImpl());
0864       EventSetup eventSetup{provider.eventSetupImpl(),
0865                             static_cast<unsigned int>(edm::Transition::Event),
0866                             consumer.esGetTokenIndices(edm::Transition::Event),
0867                             pc};
0868       edm::ESHandle<DummyData> data = eventSetup.getHandle(consumer.m_token);
0869       CPPUNIT_ASSERT(kBad.value_ == data->value_);
0870       const edm::eventsetup::ComponentDescription* desc = data.description();
0871       CPPUNIT_ASSERT(desc->label_ == "testOne");
0872     }
0873 
0874     {
0875       DummyDataConsumer consumer{edm::ESInputTag("testTwo", "blah")};
0876       consumer.updateLookup(provider.recordsToProxyIndices());
0877       consumer.prefetch(provider.eventSetupImpl());
0878       EventSetup eventSetup{provider.eventSetupImpl(),
0879                             static_cast<unsigned int>(edm::Transition::Event),
0880                             consumer.esGetTokenIndices(edm::Transition::Event),
0881                             pc};
0882       edm::ESHandle<DummyData> data = eventSetup.getHandle(consumer.m_token);
0883       CPPUNIT_ASSERT(kGood.value_ == data->value_);
0884       const edm::eventsetup::ComponentDescription* desc = data.description();
0885       CPPUNIT_ASSERT(desc->label_ == "testTwo");
0886     }
0887 
0888     {
0889       DummyDataConsumer consumer{edm::ESInputTag("DoesNotExist", "blah")};
0890       consumer.updateLookup(provider.recordsToProxyIndices());
0891       consumer.prefetch(provider.eventSetupImpl());
0892       EventSetup eventSetup{provider.eventSetupImpl(),
0893                             static_cast<unsigned int>(edm::Transition::Event),
0894                             consumer.esGetTokenIndices(edm::Transition::Event),
0895                             pc};
0896       CPPUNIT_ASSERT(not eventSetup.getHandle(consumer.m_token));
0897       CPPUNIT_ASSERT_THROW(*eventSetup.getHandle(consumer.m_token), cms::Exception);
0898     }
0899 
0900   } catch (const cms::Exception& iException) {
0901     std::cout << "caught " << iException.explainSelf() << std::endl;
0902     throw;
0903   }
0904 }
0905 
0906 void testEventsetup::getTransientHandleWithESGetTokenTest() {
0907   using edm::eventsetup::test::DummyData;
0908   using edm::eventsetup::test::DummyProxyProvider;
0909   DummyData kGood{1};
0910   DummyData kBad{0};
0911 
0912   SynchronousEventSetupsController controller;
0913   edm::ParameterSet pset = createDummyPset();
0914   EventSetupProvider& provider = *controller.makeProvider(pset, &activityRegistry);
0915 
0916   edm::ESParentContext pc;
0917   try {
0918     {
0919       edm::eventsetup::ComponentDescription description("DummyProxyProvider", "testOne", 0, true);
0920       edm::ParameterSet ps;
0921       ps.addParameter<std::string>("name", "test11");
0922       ps.registerIt();
0923       description.pid_ = ps.id();
0924       auto dummyProv = std::make_shared<DummyProxyProvider>(kBad);
0925       dummyProv->setDescription(description);
0926       provider.add(dummyProv);
0927     }
0928     {
0929       edm::eventsetup::ComponentDescription description("DummyProxyProvider", "testTwo", 1, false);
0930       edm::ParameterSet ps;
0931       ps.addParameter<std::string>("name", "test22");
0932       ps.addParameter<std::string>("appendToDataLabel", "blah");
0933       ps.registerIt();
0934       description.pid_ = ps.id();
0935       auto dummyProv = std::make_shared<DummyProxyProvider>(kGood);
0936       dummyProv->setDescription(description);
0937       dummyProv->setAppendToDataLabel(ps);
0938       provider.add(dummyProv);
0939     }
0940     std::shared_ptr<DummyFinder> dummyFinder = std::make_shared<DummyFinder>();
0941     dummyFinder->setInterval(
0942         edm::ValidityInterval(edm::IOVSyncValue(edm::EventID(1, 1, 1)), edm::IOVSyncValue(edm::EventID(1, 1, 3))));
0943     provider.add(std::shared_ptr<edm::EventSetupRecordIntervalFinder>(dummyFinder));
0944 
0945     controller.eventSetupForInstance(edm::IOVSyncValue(edm::EventID(1, 1, 1)));
0946     {
0947       DummyDataConsumer consumer{edm::ESInputTag("", "blah")};
0948       consumer.updateLookup(provider.recordsToProxyIndices());
0949       consumer.prefetch(provider.eventSetupImpl());
0950       const edm::EventSetup eventSetup{provider.eventSetupImpl(),
0951                                        static_cast<unsigned int>(edm::Transition::Event),
0952                                        consumer.esGetTokenIndices(edm::Transition::Event),
0953                                        pc};
0954       edm::ESTransientHandle<DummyData> data = eventSetup.getTransientHandle(consumer.m_token);
0955       CPPUNIT_ASSERT(kGood.value_ == data->value_);
0956       const edm::eventsetup::ComponentDescription* desc = data.description();
0957       CPPUNIT_ASSERT(desc->label_ == "testTwo");
0958     }
0959 
0960     {
0961       DummyDataConsumer consumer{edm::ESInputTag("", "")};
0962       consumer.updateLookup(provider.recordsToProxyIndices());
0963       consumer.prefetch(provider.eventSetupImpl());
0964       const edm::EventSetup eventSetup{provider.eventSetupImpl(),
0965                                        static_cast<unsigned int>(edm::Transition::Event),
0966                                        consumer.esGetTokenIndices(edm::Transition::Event),
0967                                        pc};
0968       edm::ESTransientHandle<DummyData> data = eventSetup.getTransientHandle(consumer.m_token);
0969       CPPUNIT_ASSERT(kBad.value_ == data->value_);
0970       const edm::eventsetup::ComponentDescription* desc = data.description();
0971       CPPUNIT_ASSERT(desc->label_ == "testOne");
0972     }
0973 
0974     {
0975       DummyDataConsumer consumer{edm::ESInputTag("testTwo", "blah")};
0976       consumer.updateLookup(provider.recordsToProxyIndices());
0977       consumer.prefetch(provider.eventSetupImpl());
0978       const edm::EventSetup eventSetup{provider.eventSetupImpl(),
0979                                        static_cast<unsigned int>(edm::Transition::Event),
0980                                        consumer.esGetTokenIndices(edm::Transition::Event),
0981                                        pc};
0982       edm::ESTransientHandle<DummyData> data = eventSetup.getTransientHandle(consumer.m_token);
0983       CPPUNIT_ASSERT(kGood.value_ == data->value_);
0984       const edm::eventsetup::ComponentDescription* desc = data.description();
0985       CPPUNIT_ASSERT(desc->label_ == "testTwo");
0986     }
0987 
0988     {
0989       DummyDataConsumer consumer{edm::ESInputTag("DoesNotExist", "blah")};
0990       consumer.updateLookup(provider.recordsToProxyIndices());
0991       consumer.prefetch(provider.eventSetupImpl());
0992       const edm::EventSetup eventSetup{provider.eventSetupImpl(),
0993                                        static_cast<unsigned int>(edm::Transition::Event),
0994                                        consumer.esGetTokenIndices(edm::Transition::Event),
0995                                        pc};
0996       edm::ESTransientHandle<DummyData> data = eventSetup.getTransientHandle(consumer.m_token);
0997       CPPUNIT_ASSERT(not data);
0998       CPPUNIT_ASSERT_THROW(*data, cms::Exception);
0999     }
1000 
1001   } catch (const cms::Exception& iException) {
1002     std::cout << "caught " << iException.explainSelf() << std::endl;
1003     throw;
1004   }
1005 }
1006 
1007 void testEventsetup::sourceProducerResolutionTest() {
1008   {
1009     SynchronousEventSetupsController controller;
1010     edm::ParameterSet pset = createDummyPset();
1011     EventSetupProvider& provider = *controller.makeProvider(pset, &activityRegistry);
1012 
1013     std::shared_ptr<DummyFinder> dummyFinder = std::make_shared<DummyFinder>();
1014     dummyFinder->setInterval(ValidityInterval(IOVSyncValue(Timestamp(2)), IOVSyncValue(Timestamp(3))));
1015     provider.add(std::shared_ptr<edm::EventSetupRecordIntervalFinder>(dummyFinder));
1016 
1017     edm::ESParentContext pc;
1018     {
1019       edm::eventsetup::ComponentDescription description("DummyProxyProvider", "", 0, true);
1020       auto dummyProv = std::make_shared<DummyProxyProvider>(kBad);
1021       dummyProv->setDescription(description);
1022       provider.add(dummyProv);
1023     }
1024     {
1025       edm::eventsetup::ComponentDescription description("DummyProxyProvider", "", 1, false);
1026       auto dummyProv = std::make_shared<DummyProxyProvider>(kGood);
1027       dummyProv->setDescription(description);
1028       provider.add(dummyProv);
1029     }
1030     controller.eventSetupForInstance(IOVSyncValue(Timestamp(2)));
1031     DummyDataConsumer consumer{edm::ESInputTag("", "")};
1032     consumer.updateLookup(provider.recordsToProxyIndices());
1033     consumer.prefetch(provider.eventSetupImpl());
1034     const EventSetup eventSetup{provider.eventSetupImpl(),
1035                                 static_cast<unsigned int>(edm::Transition::Event),
1036                                 consumer.esGetTokenIndices(edm::Transition::Event),
1037                                 pc};
1038     edm::ESHandle<DummyData> data = eventSetup.getHandle(consumer.m_token);
1039     CPPUNIT_ASSERT(kGood.value_ == data->value_);
1040   }
1041 
1042   //reverse order
1043   {
1044     SynchronousEventSetupsController controller;
1045     edm::ParameterSet pset = createDummyPset();
1046     EventSetupProvider& provider = *controller.makeProvider(pset, &activityRegistry);
1047 
1048     std::shared_ptr<DummyFinder> dummyFinder = std::make_shared<DummyFinder>();
1049     dummyFinder->setInterval(ValidityInterval(IOVSyncValue(Timestamp(2)), IOVSyncValue(Timestamp(3))));
1050     provider.add(std::shared_ptr<edm::EventSetupRecordIntervalFinder>(dummyFinder));
1051     {
1052       edm::eventsetup::ComponentDescription description("DummyProxyProvider", "", 0, false);
1053       auto dummyProv = std::make_shared<DummyProxyProvider>(kGood);
1054       dummyProv->setDescription(description);
1055       provider.add(dummyProv);
1056     }
1057     {
1058       edm::eventsetup::ComponentDescription description("DummyProxyProvider", "", 1, true);
1059       auto dummyProv = std::make_shared<DummyProxyProvider>(kBad);
1060       dummyProv->setDescription(description);
1061       provider.add(dummyProv);
1062     }
1063     controller.eventSetupForInstance(IOVSyncValue(Timestamp(2)));
1064     ESParentContext pc;
1065     DummyDataConsumer consumer{edm::ESInputTag("", "")};
1066     consumer.updateLookup(provider.recordsToProxyIndices());
1067     consumer.prefetch(provider.eventSetupImpl());
1068     EventSetup eventSetup{provider.eventSetupImpl(),
1069                           static_cast<unsigned int>(edm::Transition::Event),
1070                           consumer.esGetTokenIndices(edm::Transition::Event),
1071                           pc};
1072     edm::ESHandle<DummyData> data = eventSetup.getHandle(consumer.m_token);
1073     CPPUNIT_ASSERT(kGood.value_ == data->value_);
1074   }
1075 }
1076 
1077 void testEventsetup::preferTest() {
1078   edm::ParameterSet pset = createDummyPset();
1079 
1080   std::shared_ptr<DummyFinder> dummyFinder = std::make_shared<DummyFinder>();
1081   dummyFinder->setInterval(ValidityInterval(IOVSyncValue(Timestamp(2)), IOVSyncValue(Timestamp(3))));
1082 
1083   edm::ESParentContext pc;
1084   try {
1085     {
1086       SynchronousEventSetupsController controller;
1087       EventSetupProvider& provider = *controller.makeProvider(pset, &activityRegistry);
1088       provider.add(std::shared_ptr<edm::EventSetupRecordIntervalFinder>(dummyFinder));
1089 
1090       EventSetupProvider::PreferredProviderInfo preferInfo;
1091       EventSetupProvider::RecordToDataMap recordToData;
1092       //default means use all proxies
1093       preferInfo[ComponentDescription("DummyProxyProvider", "", ComponentDescription::unknownID(), false)] =
1094           recordToData;
1095       provider.setPreferredProviderInfo(preferInfo);
1096       {
1097         edm::eventsetup::ComponentDescription description("DummyProxyProvider", "bad", 0, false);
1098         auto dummyProv = std::make_shared<DummyProxyProvider>(kBad);
1099         dummyProv->setDescription(description);
1100         provider.add(dummyProv);
1101       }
1102       {
1103         edm::eventsetup::ComponentDescription description("DummyProxyProvider", "", 1, false);
1104         auto dummyProv = std::make_shared<DummyProxyProvider>(kGood);
1105         dummyProv->setDescription(description);
1106         provider.add(dummyProv);
1107       }
1108       controller.eventSetupForInstance(IOVSyncValue(Timestamp(2)));
1109       DummyDataConsumer consumer{edm::ESInputTag("", "")};
1110       consumer.updateLookup(provider.recordsToProxyIndices());
1111       consumer.prefetch(provider.eventSetupImpl());
1112       EventSetup eventSetup{provider.eventSetupImpl(),
1113                             static_cast<unsigned int>(edm::Transition::Event),
1114                             consumer.esGetTokenIndices(edm::Transition::Event),
1115                             pc};
1116       edm::ESHandle<DummyData> data = eventSetup.getHandle(consumer.m_token);
1117       CPPUNIT_ASSERT(kGood.value_ == data->value_);
1118     }
1119 
1120     //sources
1121     {
1122       SynchronousEventSetupsController controller;
1123       EventSetupProvider& provider = *controller.makeProvider(pset, &activityRegistry);
1124       provider.add(std::shared_ptr<edm::EventSetupRecordIntervalFinder>(dummyFinder));
1125 
1126       EventSetupProvider::PreferredProviderInfo preferInfo;
1127       EventSetupProvider::RecordToDataMap recordToData;
1128       //default means use all proxies
1129       preferInfo[ComponentDescription("DummyProxyProvider", "", ComponentDescription::unknownID(), false)] =
1130           recordToData;
1131       provider.setPreferredProviderInfo(preferInfo);
1132       {
1133         edm::eventsetup::ComponentDescription description("DummyProxyProvider", "", 0, true);
1134         auto dummyProv = std::make_shared<DummyProxyProvider>(kGood);
1135         dummyProv->setDescription(description);
1136         provider.add(dummyProv);
1137       }
1138       {
1139         edm::eventsetup::ComponentDescription description("DummyProxyProvider", "bad", 1, true);
1140         auto dummyProv = std::make_shared<DummyProxyProvider>(kBad);
1141         dummyProv->setDescription(description);
1142         provider.add(dummyProv);
1143       }
1144       controller.eventSetupForInstance(IOVSyncValue(Timestamp(2)));
1145       DummyDataConsumer consumer{edm::ESInputTag("", "")};
1146       consumer.updateLookup(provider.recordsToProxyIndices());
1147       consumer.prefetch(provider.eventSetupImpl());
1148       EventSetup eventSetup{provider.eventSetupImpl(),
1149                             static_cast<unsigned int>(edm::Transition::Event),
1150                             consumer.esGetTokenIndices(edm::Transition::Event),
1151                             pc};
1152       edm::ESHandle<DummyData> data = eventSetup.getHandle(consumer.m_token);
1153       CPPUNIT_ASSERT(kGood.value_ == data->value_);
1154     }
1155 
1156     //specific name
1157     {
1158       SynchronousEventSetupsController controller;
1159       EventSetupProvider& provider = *controller.makeProvider(pset, &activityRegistry);
1160       provider.add(std::shared_ptr<edm::EventSetupRecordIntervalFinder>(dummyFinder));
1161 
1162       EventSetupProvider::PreferredProviderInfo preferInfo;
1163       EventSetupProvider::RecordToDataMap recordToData;
1164       recordToData.insert(
1165           std::make_pair(std::string("DummyRecord"), std::make_pair(std::string("DummyData"), std::string())));
1166       preferInfo[ComponentDescription("DummyProxyProvider", "", ComponentDescription::unknownID(), false)] =
1167           recordToData;
1168       provider.setPreferredProviderInfo(preferInfo);
1169       {
1170         edm::eventsetup::ComponentDescription description("DummyProxyProvider", "", 0, true);
1171         auto dummyProv = std::make_shared<DummyProxyProvider>(kGood);
1172         dummyProv->setDescription(description);
1173         provider.add(dummyProv);
1174       }
1175       {
1176         edm::eventsetup::ComponentDescription description("DummyProxyProvider", "bad", 1, true);
1177         auto dummyProv = std::make_shared<DummyProxyProvider>(kBad);
1178         dummyProv->setDescription(description);
1179         provider.add(dummyProv);
1180       }
1181       controller.eventSetupForInstance(IOVSyncValue(Timestamp(2)));
1182       DummyDataConsumer consumer{edm::ESInputTag("", "")};
1183       consumer.updateLookup(provider.recordsToProxyIndices());
1184       consumer.prefetch(provider.eventSetupImpl());
1185       EventSetup eventSetup{provider.eventSetupImpl(),
1186                             static_cast<unsigned int>(edm::Transition::Event),
1187                             consumer.esGetTokenIndices(edm::Transition::Event),
1188                             pc};
1189       edm::ESHandle<DummyData> data = eventSetup.getHandle(consumer.m_token);
1190       CPPUNIT_ASSERT(kGood.value_ == data->value_);
1191     }
1192 
1193   } catch (const cms::Exception& iException) {
1194     std::cout << "caught " << iException.explainSelf() << std::endl;
1195     throw;
1196   }
1197 }
1198 
1199 void testEventsetup::introspectionTest() {
1200   SynchronousEventSetupsController controller;
1201   edm::ParameterSet pset = createDummyPset();
1202   EventSetupProvider& provider = *controller.makeProvider(pset, &activityRegistry);
1203 
1204   std::shared_ptr<DummyFinder> dummyFinder = std::make_shared<DummyFinder>();
1205   dummyFinder->setInterval(ValidityInterval(IOVSyncValue(Timestamp(2)), IOVSyncValue(Timestamp(3))));
1206   provider.add(std::shared_ptr<edm::EventSetupRecordIntervalFinder>(dummyFinder));
1207 
1208   edm::ESParentContext pc;
1209   try {
1210     {
1211       edm::eventsetup::ComponentDescription description("DummyProxyProvider", "", 0, true);
1212       edm::ParameterSet ps;
1213       ps.addParameter<std::string>("name", "test11");
1214       ps.registerIt();
1215       description.pid_ = ps.id();
1216       auto dummyProv = std::make_shared<DummyProxyProvider>(kBad);
1217       dummyProv->setDescription(description);
1218       provider.add(dummyProv);
1219     }
1220     EventSetupRecordKey dummyRecordKey = EventSetupRecordKey::makeKey<DummyRecord>();
1221     controller.eventSetupForInstance(IOVSyncValue(Timestamp(2)));
1222     {
1223       EventSetup eventSetup{provider.eventSetupImpl(), 0, nullptr, pc};
1224 
1225       CPPUNIT_ASSERT(eventSetup.recordIsProvidedByAModule(dummyRecordKey));
1226       std::vector<edm::eventsetup::EventSetupRecordKey> recordKeys;
1227       eventSetup.fillAvailableRecordKeys(recordKeys);
1228       CPPUNIT_ASSERT(1 == recordKeys.size());
1229       CPPUNIT_ASSERT(dummyRecordKey == recordKeys[0]);
1230       auto record = eventSetup.find(recordKeys[0]);
1231       CPPUNIT_ASSERT(record.has_value());
1232     }
1233     // Intentionally an out of range sync value so the IOV is invalid
1234     // to test the find function with a record that exists in the
1235     // EventSetupImpl but has a null pointer.
1236     controller.eventSetupForInstance(IOVSyncValue(Timestamp(4)));
1237     {
1238       EventSetup eventSetup{provider.eventSetupImpl(), 0, nullptr, pc};
1239 
1240       CPPUNIT_ASSERT(eventSetup.recordIsProvidedByAModule(dummyRecordKey));
1241       std::vector<edm::eventsetup::EventSetupRecordKey> recordKeys;
1242       eventSetup.fillAvailableRecordKeys(recordKeys);
1243       CPPUNIT_ASSERT(0 == recordKeys.size());
1244       auto record = eventSetup.find(dummyRecordKey);
1245       CPPUNIT_ASSERT(!record.has_value());
1246 
1247       // Just to try all cases test find with a record type not in the EventSetupImpl
1248       // at all.
1249       EventSetupRecordKey dummyRecordKey1 = EventSetupRecordKey::makeKey<DummyEventSetupRecord>();
1250       auto record1 = eventSetup.find(dummyRecordKey1);
1251       CPPUNIT_ASSERT(!record1.has_value());
1252     }
1253   } catch (const cms::Exception& iException) {
1254     std::cout << "caught " << iException.explainSelf() << std::endl;
1255     throw;
1256   }
1257 }
1258 
1259 void testEventsetup::iovExtensionTest() {
1260   SynchronousEventSetupsController controller;
1261   edm::ParameterSet pset = createDummyPset();
1262   EventSetupProvider& provider = *controller.makeProvider(pset, &activityRegistry);
1263 
1264   std::shared_ptr<DummyFinder> finder = std::make_shared<DummyFinder>();
1265   finder->setInterval(ValidityInterval(IOVSyncValue(Timestamp(2)), IOVSyncValue(Timestamp(3))));
1266   provider.add(std::shared_ptr<edm::EventSetupRecordIntervalFinder>(finder));
1267 
1268   edm::ESParentContext pc;
1269   {
1270     controller.eventSetupForInstance(IOVSyncValue{Timestamp(2)});
1271     EventSetup eventSetup{provider.eventSetupImpl(), 0, nullptr, pc};
1272     CPPUNIT_ASSERT(2 == eventSetup.get<DummyRecord>().cacheIdentifier());
1273   }
1274   {
1275     controller.eventSetupForInstance(IOVSyncValue{Timestamp(3)});
1276     EventSetup eventSetup{provider.eventSetupImpl(), 0, nullptr, pc};
1277     eventSetup.get<DummyRecord>();
1278     CPPUNIT_ASSERT(2 == eventSetup.get<DummyRecord>().cacheIdentifier());
1279   }
1280   //extending the IOV should not cause the cache to be reset
1281   finder->setInterval(ValidityInterval(IOVSyncValue{Timestamp{2}}, IOVSyncValue{Timestamp{4}}));
1282   {
1283     controller.eventSetupForInstance(IOVSyncValue{Timestamp(4)});
1284     EventSetup eventSetup{provider.eventSetupImpl(), 0, nullptr, pc};
1285     CPPUNIT_ASSERT(2 == eventSetup.get<DummyRecord>().cacheIdentifier());
1286   }
1287 
1288   //this is a new IOV so should get cache reset
1289   finder->setInterval(ValidityInterval(IOVSyncValue{Timestamp{5}}, IOVSyncValue{Timestamp{6}}));
1290   {
1291     controller.eventSetupForInstance(IOVSyncValue{Timestamp(5)});
1292     EventSetup eventSetup{provider.eventSetupImpl(), 0, nullptr, pc};
1293     CPPUNIT_ASSERT(3 == eventSetup.get<DummyRecord>().cacheIdentifier());
1294   }
1295 }
1296 
1297 void testEventsetup::resetProxiesTest() {
1298   SynchronousEventSetupsController controller;
1299   edm::ParameterSet pset = createDummyPset();
1300   EventSetupProvider& provider = *controller.makeProvider(pset, &activityRegistry);
1301 
1302   std::shared_ptr<DummyFinder> finder = std::make_shared<DummyFinder>();
1303   finder->setInterval(ValidityInterval(IOVSyncValue(Timestamp(2)), IOVSyncValue(Timestamp(3))));
1304   provider.add(std::shared_ptr<edm::EventSetupRecordIntervalFinder>(finder));
1305 
1306   ComponentDescription description("DummyProxyProvider", "", 0, true);
1307   ParameterSet ps;
1308   ps.addParameter<std::string>("name", "test11");
1309   ps.registerIt();
1310   description.pid_ = ps.id();
1311   DummyData kOne{1};
1312   auto dummyProv = std::make_shared<DummyProxyProvider>(kOne);
1313   dummyProv->setDescription(description);
1314   provider.add(dummyProv);
1315 
1316   edm::ESParentContext pc;
1317   {
1318     controller.eventSetupForInstance(IOVSyncValue{Timestamp(2)});
1319     DummyDataConsumer consumer{edm::ESInputTag("", "")};
1320     consumer.updateLookup(provider.recordsToProxyIndices());
1321     consumer.prefetch(provider.eventSetupImpl());
1322     EventSetup eventSetup{provider.eventSetupImpl(),
1323                           static_cast<unsigned int>(edm::Transition::Event),
1324                           consumer.esGetTokenIndices(edm::Transition::Event),
1325                           pc};
1326     CPPUNIT_ASSERT(2 == eventSetup.get<DummyRecord>().cacheIdentifier());
1327     edm::ESHandle<DummyData> data = eventSetup.getHandle(consumer.m_token);
1328     CPPUNIT_ASSERT(data->value_ == 1);
1329   }
1330   provider.forceCacheClear();
1331   {
1332     controller.eventSetupForInstance(IOVSyncValue{Timestamp(2)});
1333     DummyDataConsumer consumer{edm::ESInputTag("", "")};
1334     consumer.updateLookup(provider.recordsToProxyIndices());
1335     consumer.prefetch(provider.eventSetupImpl());
1336     EventSetup eventSetup{provider.eventSetupImpl(),
1337                           static_cast<unsigned int>(edm::Transition::Event),
1338                           consumer.esGetTokenIndices(edm::Transition::Event),
1339                           pc};
1340     eventSetup.get<DummyRecord>();
1341     CPPUNIT_ASSERT(3 == eventSetup.get<DummyRecord>().cacheIdentifier());
1342     dummyProv->incrementData();
1343     edm::ESHandle<DummyData> data = eventSetup.getHandle(consumer.m_token);
1344     CPPUNIT_ASSERT(data->value_ == 2);
1345   }
1346 }