Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:13:03

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     std::vector<edm::ParameterSet> pss;
0197 
0198     edm::ParameterSet ps;
0199     ps.addParameter("@service_type", typeName);
0200     int value = 2;
0201     ps.addParameter("value", value);
0202     pss.push_back(ps);
0203 
0204     ServicesManager sm(legacyToken, kConfigurationOverrides, pss);
0205 
0206     CPPUNIT_ASSERT(2 == sm.get<TestService>().value());
0207   }
0208   {
0209     try {
0210       std::vector<edm::ParameterSet> pss;
0211 
0212       ServicesManager sm(legacyToken, kOverlapIsError, pss);
0213 
0214       CPPUNIT_ASSERT(!sm.get<TestService>().beginJobCalled());
0215       edm::ActivityRegistry ar;
0216       sm.connectTo(ar);
0217       ar.postBeginJobSignal_();
0218 
0219       CPPUNIT_ASSERT(sm.get<TestService>().beginJobCalled());
0220     } catch (const edm::Exception& iException) {
0221       std::cout << iException.what() << std::endl;
0222       throw;
0223     } catch (const std::exception& iException) {
0224       std::cout << iException.what() << std::endl;
0225       throw;
0226     }
0227   }
0228 }
0229 
0230 void testServicesManager::dependencyTest() {
0231   //Try both order of creating services
0232   typedef testserviceregistry::DummyService TestService;
0233 
0234   using namespace edm::serviceregistry;
0235 
0236   edm::AssertHandler ah;
0237 
0238   {
0239     std::vector<edm::ParameterSet> pss;
0240     {
0241       edm::ParameterSet ps;
0242       std::string typeName("DummyService");
0243       ps.addParameter("@service_type", typeName);
0244       int value = 1;
0245       ps.addParameter("value", value);
0246       pss.push_back(ps);
0247     }
0248     {
0249       edm::ParameterSet ps;
0250       std::string typeName("DependsOnDummyService");
0251       ps.addParameter("@service_type", typeName);
0252       pss.push_back(ps);
0253     }
0254 
0255     ServicesManager sm(pss);
0256 
0257     CPPUNIT_ASSERT(1 == sm.get<testserviceregistry::DependsOnDummyService>().value());
0258   }
0259   {
0260     std::vector<edm::ParameterSet> pss;
0261     {
0262       edm::ParameterSet ps;
0263       std::string typeName("DependsOnDummyService");
0264       ps.addParameter("@service_type", typeName);
0265       pss.push_back(ps);
0266     }
0267     {
0268       edm::ParameterSet ps;
0269       std::string typeName("DummyService");
0270       ps.addParameter("@service_type", typeName);
0271       int value = 1;
0272       ps.addParameter("value", value);
0273       pss.push_back(ps);
0274     }
0275 
0276     ServicesManager sm(pss);
0277 
0278     CPPUNIT_ASSERT(1 == sm.get<testserviceregistry::DependsOnDummyService>().value());
0279   }
0280 }
0281 
0282 void testServicesManager::saveConfigTest() {
0283   typedef testserviceregistry::DummyService TestService;
0284 
0285   using namespace edm::serviceregistry;
0286 
0287   edm::AssertHandler ah;
0288 
0289   {
0290     std::vector<edm::ParameterSet> pss;
0291     {
0292       edm::ParameterSet ps;
0293       std::string typeName("DummyService");
0294       ps.addParameter("@service_type", typeName);
0295       int value = 1;
0296       ps.addParameter("value", value);
0297       pss.push_back(ps);
0298     }
0299 
0300     {
0301       edm::ParameterSet ps;
0302       std::string typeName("DummyStoreConfigService");
0303       ps.addParameter("@service_type", typeName);
0304       pss.push_back(ps);
0305     }
0306 
0307     ServicesManager sm(pss);
0308 
0309     CPPUNIT_ASSERT(!pss[0].exists("@save_config"));
0310     CPPUNIT_ASSERT(pss[1].exists("@save_config"));
0311   }
0312 }