Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-06-17 01:30:26

0001 /*
0002  *  servicesmanager_t.cppunit.cc
0003  *  CMSSW
0004  *
0005  *  Created by Chris Jones on 9/5/05.
0006  *
0007  */
0008 #include "FWCore/ServiceRegistry/interface/ServiceToken.h"
0009 
0010 #include "FWCore/ServiceRegistry/interface/ServicesManager.h"
0011 #include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"
0012 #include <cppunit/extensions/HelperMacros.h>
0013 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0014 
0015 #include "FWCore/ServiceRegistry/test/stubs/DummyService.h"
0016 #include "FWCore/ServiceRegistry/test/stubs/DependsOnDummyService.h"
0017 #include "FWCore/PluginManager/interface/ProblemTracker.h"
0018 #include <iostream>
0019 
0020 class testServicesManager : public CppUnit::TestFixture {
0021   CPPUNIT_TEST_SUITE(testServicesManager);
0022 
0023   CPPUNIT_TEST(putGetTest);
0024   CPPUNIT_TEST(loadTest);
0025   CPPUNIT_TEST(legacyTest);
0026   CPPUNIT_TEST(dependencyTest);
0027   CPPUNIT_TEST(saveConfigTest);
0028 
0029   CPPUNIT_TEST_SUITE_END();
0030 
0031 public:
0032   void setUp() {}
0033   void tearDown() {}
0034 
0035   void putGetTest();
0036   void loadTest();
0037   void legacyTest();
0038   void dependencyTest();
0039   void saveConfigTest();
0040 };
0041 
0042 ///registration of the test so that the runner can find it
0043 CPPUNIT_TEST_SUITE_REGISTRATION(testServicesManager);
0044 
0045 namespace {
0046   struct DummyService {
0047     //DummyService(const edm::ParameterSet&,
0048     //             edm::ActivityRegistry&) {}
0049   };
0050 }  // namespace
0051 
0052 //namespace edm {
0053 //   class ActivityRegistry {};
0054 //}
0055 
0056 void testServicesManager::putGetTest() {
0057   using namespace edm::serviceregistry;
0058 
0059   std::vector<edm::ParameterSet> ps;
0060   ServicesManager sm(ps);
0061 
0062   CPPUNIT_ASSERT(!sm.isAvailable<DummyService>());
0063   bool exceptionThrown = true;
0064   try {
0065     sm.get<DummyService>();
0066     exceptionThrown = false;
0067   } catch (const edm::Exception&) {
0068   }
0069   CPPUNIT_ASSERT(exceptionThrown);
0070 
0071   auto ptrWrapper = std::make_shared<ServiceWrapper<DummyService>>(std::make_unique<DummyService>());
0072 
0073   CPPUNIT_ASSERT(sm.put(ptrWrapper));
0074 
0075   CPPUNIT_ASSERT(sm.isAvailable<DummyService>());
0076 
0077   sm.get<DummyService>();
0078 
0079   CPPUNIT_ASSERT(!sm.put(ptrWrapper));
0080 }
0081 
0082 void testServicesManager::loadTest() {
0083   typedef testserviceregistry::DummyService TestService;
0084 
0085   using namespace edm::serviceregistry;
0086 
0087   edm::AssertHandler ah;
0088 
0089   {
0090     std::vector<edm::ParameterSet> pss;
0091 
0092     edm::ParameterSet ps;
0093     std::string typeName("DummyService");
0094     ps.addParameter("@service_type", typeName);
0095     int value = 1;
0096     ps.addParameter("value", value);
0097     pss.push_back(ps);
0098 
0099     ServicesManager sm(pss);
0100 
0101     CPPUNIT_ASSERT(1 == sm.get<TestService>().value());
0102   }
0103   {
0104     std::vector<edm::ParameterSet> pss;
0105 
0106     edm::ParameterSet ps;
0107     std::string typeName("DoesntExistService");
0108     ps.addParameter("@service_type", typeName);
0109     pss.push_back(ps);
0110 
0111     bool threwConfigurationException = false;
0112     try {
0113       ServicesManager sm(pss);
0114     } catch (const cms::Exception&) {
0115       threwConfigurationException = true;
0116     }
0117 
0118     CPPUNIT_ASSERT(threwConfigurationException);
0119   }
0120   {
0121     std::vector<edm::ParameterSet> pss;
0122 
0123     edm::ParameterSet ps;
0124     std::string typeName("DummyService");
0125     ps.addParameter("@service_type", typeName);
0126     int value = 1;
0127     ps.addParameter("value", value);
0128     pss.push_back(ps);
0129     pss.push_back(ps);
0130 
0131     bool caughtMultipleServiceError = false;
0132     try {
0133       ServicesManager sm(pss);
0134     } catch (const edm::Exception&) {
0135       caughtMultipleServiceError = true;
0136     }
0137 
0138     CPPUNIT_ASSERT(caughtMultipleServiceError);
0139   }
0140   //NEED A TEST FOR SERVICES THAT DEPEND ON OTHER SERVICES
0141 }
0142 
0143 void testServicesManager::legacyTest() {
0144   typedef testserviceregistry::DummyService TestService;
0145 
0146   using namespace edm::serviceregistry;
0147 
0148   edm::AssertHandler ah;
0149 
0150   std::string typeName("DummyService");
0151 
0152   std::vector<edm::ParameterSet> pssLegacy;
0153   {
0154     edm::ParameterSet ps;
0155     ps.addParameter("@service_type", typeName);
0156     int value = 1;
0157     ps.addParameter("value", value);
0158     pssLegacy.push_back(ps);
0159   }
0160   auto legacy = std::make_shared<ServicesManager>(pssLegacy);
0161   CPPUNIT_ASSERT(1 == legacy->get<TestService>().value());
0162 
0163   edm::ServiceToken legacyToken(legacy);
0164   {
0165     std::vector<edm::ParameterSet> pss;
0166 
0167     edm::ParameterSet ps;
0168     ps.addParameter("@service_type", typeName);
0169     int value = 2;
0170     ps.addParameter("value", value);
0171     pss.push_back(ps);
0172 
0173     bool threwConfigurationException = false;
0174     try {
0175       ServicesManager sm(legacyToken, kOverlapIsError, pss);
0176     } catch (const edm::Exception&) {
0177       threwConfigurationException = true;
0178     }
0179 
0180     CPPUNIT_ASSERT(threwConfigurationException);
0181   }
0182   {
0183     std::vector<edm::ParameterSet> pss;
0184 
0185     edm::ParameterSet ps;
0186     ps.addParameter("@service_type", typeName);
0187     int value = 2;
0188     ps.addParameter("value", value);
0189     pss.push_back(ps);
0190 
0191     ServicesManager sm(legacyToken, kTokenOverrides, pss);
0192 
0193     CPPUNIT_ASSERT(1 == sm.get<TestService>().value());
0194   }
0195   {
0196     try {
0197       std::vector<edm::ParameterSet> pss;
0198 
0199       ServicesManager sm(legacyToken, kOverlapIsError, pss);
0200 
0201       CPPUNIT_ASSERT(!sm.get<TestService>().beginJobCalled());
0202       edm::ActivityRegistry ar;
0203       sm.connectTo(ar);
0204       ar.postBeginJobSignal_();
0205 
0206       CPPUNIT_ASSERT(sm.get<TestService>().beginJobCalled());
0207     } catch (const edm::Exception& iException) {
0208       std::cout << iException.what() << std::endl;
0209       throw;
0210     } catch (const std::exception& iException) {
0211       std::cout << iException.what() << std::endl;
0212       throw;
0213     }
0214   }
0215 }
0216 
0217 void testServicesManager::dependencyTest() {
0218   //Try both order of creating services
0219   typedef testserviceregistry::DummyService TestService;
0220 
0221   using namespace edm::serviceregistry;
0222 
0223   edm::AssertHandler ah;
0224 
0225   {
0226     std::vector<edm::ParameterSet> pss;
0227     {
0228       edm::ParameterSet ps;
0229       std::string typeName("DummyService");
0230       ps.addParameter("@service_type", typeName);
0231       int value = 1;
0232       ps.addParameter("value", value);
0233       pss.push_back(ps);
0234     }
0235     {
0236       edm::ParameterSet ps;
0237       std::string typeName("DependsOnDummyService");
0238       ps.addParameter("@service_type", typeName);
0239       pss.push_back(ps);
0240     }
0241 
0242     ServicesManager sm(pss);
0243 
0244     CPPUNIT_ASSERT(1 == sm.get<testserviceregistry::DependsOnDummyService>().value());
0245   }
0246   {
0247     std::vector<edm::ParameterSet> pss;
0248     {
0249       edm::ParameterSet ps;
0250       std::string typeName("DependsOnDummyService");
0251       ps.addParameter("@service_type", typeName);
0252       pss.push_back(ps);
0253     }
0254     {
0255       edm::ParameterSet ps;
0256       std::string typeName("DummyService");
0257       ps.addParameter("@service_type", typeName);
0258       int value = 1;
0259       ps.addParameter("value", value);
0260       pss.push_back(ps);
0261     }
0262 
0263     ServicesManager sm(pss);
0264 
0265     CPPUNIT_ASSERT(1 == sm.get<testserviceregistry::DependsOnDummyService>().value());
0266   }
0267 }
0268 
0269 void testServicesManager::saveConfigTest() {
0270   typedef testserviceregistry::DummyService TestService;
0271 
0272   using namespace edm::serviceregistry;
0273 
0274   edm::AssertHandler ah;
0275 
0276   {
0277     std::vector<edm::ParameterSet> pss;
0278     {
0279       edm::ParameterSet ps;
0280       std::string typeName("DummyService");
0281       ps.addParameter("@service_type", typeName);
0282       int value = 1;
0283       ps.addParameter("value", value);
0284       pss.push_back(ps);
0285     }
0286 
0287     {
0288       edm::ParameterSet ps;
0289       std::string typeName("DummyStoreConfigService");
0290       ps.addParameter("@service_type", typeName);
0291       pss.push_back(ps);
0292     }
0293 
0294     ServicesManager sm(pss);
0295 
0296     CPPUNIT_ASSERT(!pss[0].exists("@save_config"));
0297     CPPUNIT_ASSERT(pss[1].exists("@save_config"));
0298   }
0299 }