Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-07-16 00:28:33

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