From 05bcc9352dde42e8196570497dc009ef74ac914a Mon Sep 17 00:00:00 2001 From: Nils Krumnack <krumnack@iastate.edu> Date: Tue, 9 Mar 2021 16:11:23 -0600 Subject: [PATCH] introduce a unit test for dual-use services Mostly a 1-to-1 copy of the dual-use tool test for AsgToolConfig. --- .../AsgExampleTools/AsgExampleToolsDict.h | 1 + .../AsgExampleTools/IUnitTestService1.h | 41 ++++++ .../AsgExampleTools/UnitTestService1.h | 73 +++++++++++ .../AsgExampleTools/selection.xml | 1 + .../AsgExampleTools/CMakeLists.txt | 8 +- .../AsgExampleTools/Root/UnitTestService1.cxx | 117 +++++++++++++++++ .../components/AsgExampleTools_entries.cxx | 2 + .../test/gt_AsgServiceConfig.cxx | 122 ++++++++++++++++++ 8 files changed, 364 insertions(+), 1 deletion(-) create mode 100644 Control/AthToolSupport/AsgExampleTools/AsgExampleTools/IUnitTestService1.h create mode 100644 Control/AthToolSupport/AsgExampleTools/AsgExampleTools/UnitTestService1.h create mode 100644 Control/AthToolSupport/AsgExampleTools/Root/UnitTestService1.cxx create mode 100644 Control/AthToolSupport/AsgExampleTools/test/gt_AsgServiceConfig.cxx diff --git a/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/AsgExampleToolsDict.h b/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/AsgExampleToolsDict.h index f2bf45164df0..79085024200f 100644 --- a/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/AsgExampleToolsDict.h +++ b/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/AsgExampleToolsDict.h @@ -11,5 +11,6 @@ #include "AsgExampleTools/UnitTestTool3.h" #include "AsgExampleTools/UnitTestTool2.h" #include "AsgExampleTools/UnitTestTool1A.h" +#include "AsgExampleTools/UnitTestService1.h" #endif // not ASGTOOLS_ASGTOOLSDICT_H diff --git a/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/IUnitTestService1.h b/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/IUnitTestService1.h new file mode 100644 index 000000000000..0cea36d91beb --- /dev/null +++ b/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/IUnitTestService1.h @@ -0,0 +1,41 @@ +/* + Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration +*/ + +/// @author Nils Krumnack + + + +#ifndef ASG_TOOLS__I_UNIT_TEST_SERVICE1_H +#define ASG_TOOLS__I_UNIT_TEST_SERVICE1_H + +#include <AsgServices/IAsgService.h> + +namespace asg +{ + /// \brief the interface for \ref UnitTestService1 + + class IUnitTestService1 : virtual public IAsgService + { + // Declare the interface that this class provides + ASG_SERVICE_INTERFACE( CP::IUnitTestService1 ) + + /// \brief get the integer property + public: + virtual std::string getPropertyString () const = 0; + + /// \brief get the integer property + public: + virtual int getPropertyInt () const = 0; + + /// \brief get the integer property + public: + virtual void setPropertyInt (int val_property) = 0; + + /// \brief get whether we have been initialized + public: + virtual bool isInitialized () const = 0; + }; +} + +#endif diff --git a/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/UnitTestService1.h b/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/UnitTestService1.h new file mode 100644 index 000000000000..41894cfc1bd6 --- /dev/null +++ b/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/UnitTestService1.h @@ -0,0 +1,73 @@ +/* + Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration +*/ + +/// @author Nils Krumnack + + + +#ifndef ASG_TOOLS__UNIT_TEST_SERVICE1_H +#define ASG_TOOLS__UNIT_TEST_SERVICE1_H + +#include <AsgServices/AsgService.h> +#include <AsgExampleTools/IUnitTestService1.h> + +namespace asg +{ + /// \brief a service used to unit test AnaToolHandle + /// + /// This allows to unit test the various capabilities of + /// AnaToolHandle in a controlled fashion. + + struct UnitTestService1 : virtual public IUnitTestService1, + public AsgService + { + /// \brief standard constructor + public: + UnitTestService1 (const std::string& name, ISvcLocator* pSvcLocator); + + /// \brief standard destructor + public: + ~UnitTestService1 (); + + public: + StatusCode initialize () override; + + ASG_SERVICE_CLASS1 (UnitTestService1, IUnitTestService1) + + public: + virtual std::string getPropertyString () const override; + + public: + virtual int getPropertyInt () const override; + + public: + virtual void setPropertyInt (int val_property) override; + + public: + virtual bool isInitialized () const override; + + /// \brief whether initialize has been called + public: + bool m_isInitialized = false; + + /// \brief the string property + public: + std::string m_propertyString; + + /// \brief the integer property + public: + int m_propertyInt = -7; + + /// \brief whether initialize should fail + public: + bool m_initializeFail = false; + + /// \brief the number of times the service of the given name has been + /// instantiated + public: + static int& instance_counts (const std::string& name); + }; +} + +#endif diff --git a/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/selection.xml b/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/selection.xml index 47bba34f9ec1..2115d7f2dc67 100644 --- a/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/selection.xml +++ b/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/selection.xml @@ -6,5 +6,6 @@ <class name="asg::UnitTestTool3" /> <class name="asg::UnitTestTool2" /> <class name="asg::UnitTestTool1A" /> + <class name="asg::UnitTestService1" /> </lcgdict> diff --git a/Control/AthToolSupport/AsgExampleTools/CMakeLists.txt b/Control/AthToolSupport/AsgExampleTools/CMakeLists.txt index d1b02221d1c7..bab49dfc48ec 100644 --- a/Control/AthToolSupport/AsgExampleTools/CMakeLists.txt +++ b/Control/AthToolSupport/AsgExampleTools/CMakeLists.txt @@ -21,7 +21,7 @@ atlas_add_root_dictionary( AsgExampleToolsLib AsgExampleToolsCintDict atlas_add_library( AsgExampleToolsLib AsgExampleTools/*.h Root/*.cxx ${AsgExampleToolsCintDict} PUBLIC_HEADERS AsgExampleTools - LINK_LIBRARIES AsgTools AsgDataHandlesLib AsgTestingLib ${extra_lib} ) + LINK_LIBRARIES AsgTools AsgServicesLib AsgDataHandlesLib AsgTestingLib ${extra_lib} ) if( NOT XAOD_STANDALONE ) atlas_add_component( AsgExampleTools @@ -62,6 +62,12 @@ atlas_add_test( gt_AsgToolConfig LINK_LIBRARIES ${GTEST_LIBRARIES} AsgTools AsgExampleToolsLib AsgTestingLib ) set_tests_properties (AsgExampleTools_gt_AsgToolConfig_ctest PROPERTIES LABELS "AsgTools;AsgExampleTools" ) +atlas_add_test( gt_AsgServiceConfig + SOURCES test/gt_AsgServiceConfig.cxx + INCLUDE_DIRS ${GTEST_INCLUDE_DIRS} + LINK_LIBRARIES ${GTEST_LIBRARIES} AsgExampleToolsLib AsgTestingLib ) +set_tests_properties (AsgExampleTools_gt_AsgServiceConfig_ctest PROPERTIES LABELS "AsgServices;AsgExampleTools" ) + if( XAOD_STANDALONE ) diff --git a/Control/AthToolSupport/AsgExampleTools/Root/UnitTestService1.cxx b/Control/AthToolSupport/AsgExampleTools/Root/UnitTestService1.cxx new file mode 100644 index 000000000000..c0872687a348 --- /dev/null +++ b/Control/AthToolSupport/AsgExampleTools/Root/UnitTestService1.cxx @@ -0,0 +1,117 @@ +/* + Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration +*/ + +/// @author Nils Krumnack + + + +// +// includes +// + +#include <AsgExampleTools/UnitTestService1.h> + +#include <AsgMessaging/MessageCheck.h> +#include <gtest/gtest.h> +#include <map> + +// +// method implementations +// + +namespace asg +{ + UnitTestService1 :: + UnitTestService1 (const std::string& name, ISvcLocator* pSvcLocator) + : AsgService (name, pSvcLocator) + { + // declareInterface<IUnitTestService1>(); + + declareProperty ("propertyInt", m_propertyInt, "the integer property"); + declareProperty ("propertyString", m_propertyString, "the string property"); + declareProperty ("initializeFail", m_initializeFail, "whether initialize should fail"); + + ++ instance_counts (name); + + ANA_MSG_INFO ("create UnitTestService1 " << this); + } + + + + UnitTestService1 :: + ~UnitTestService1 () + { + ANA_MSG_INFO ("destroy UnitTestService1 " << this); + + -- instance_counts (name()); + } + + + + StatusCode UnitTestService1 :: + initialize () + { + ANA_MSG_INFO ("initialize UnitTestService1 " << this); + ANA_MSG_INFO (" propertyString: " << m_propertyString); + ANA_MSG_INFO (" propertyInt: " << m_propertyInt); + + if (m_initializeFail) + { + ATH_MSG_ERROR ("service configured to fail initialize"); + return StatusCode::FAILURE; + } + if (m_isInitialized) + { + ATH_MSG_ERROR ("initialize called twice"); + return StatusCode::FAILURE; + } + m_isInitialized = true; + return StatusCode::SUCCESS; + } + + + + std::string UnitTestService1 :: + getPropertyString () const + { + return m_propertyString; + } + + + + int UnitTestService1 :: + getPropertyInt () const + { + return m_propertyInt; + } + + + + void UnitTestService1 :: + setPropertyInt (int val_property) + { + m_propertyInt = val_property; + } + + + + bool UnitTestService1 :: + isInitialized () const + { + return m_isInitialized; + } + + + + int& UnitTestService1 :: + instance_counts (const std::string& name) + { + static std::map<std::string,int> counts; + auto iter = counts.find (name); + if (iter == counts.end()) + iter = counts.insert (std::make_pair (name, 0)).first; + assert (iter != counts.end()); + return iter->second; + } +} diff --git a/Control/AthToolSupport/AsgExampleTools/src/components/AsgExampleTools_entries.cxx b/Control/AthToolSupport/AsgExampleTools/src/components/AsgExampleTools_entries.cxx index 978309b448d7..6f1ecd0507d2 100644 --- a/Control/AthToolSupport/AsgExampleTools/src/components/AsgExampleTools_entries.cxx +++ b/Control/AthToolSupport/AsgExampleTools/src/components/AsgExampleTools_entries.cxx @@ -4,6 +4,7 @@ #include "AsgExampleTools/AsgHelloTool.h" #include <AsgExampleTools/DataHandleTestTool.h> +#include <AsgExampleTools/UnitTestService1.h> #include <AsgExampleTools/UnitTestTool1.h> #include <AsgExampleTools/UnitTestTool1A.h> #include <AsgExampleTools/UnitTestTool2.h> @@ -14,6 +15,7 @@ DECLARE_COMPONENT( AsgHelloTool ) DECLARE_COMPONENT( AsgExampleAlgorithm ) DECLARE_COMPONENT( asg::DataHandleTestTool ) +DECLARE_COMPONENT( asg::UnitTestService1 ) DECLARE_COMPONENT( asg::UnitTestTool1 ) DECLARE_COMPONENT( asg::UnitTestTool1A ) DECLARE_COMPONENT( asg::UnitTestTool2 ) diff --git a/Control/AthToolSupport/AsgExampleTools/test/gt_AsgServiceConfig.cxx b/Control/AthToolSupport/AsgExampleTools/test/gt_AsgServiceConfig.cxx new file mode 100644 index 000000000000..96103d0afb25 --- /dev/null +++ b/Control/AthToolSupport/AsgExampleTools/test/gt_AsgServiceConfig.cxx @@ -0,0 +1,122 @@ +/* + Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration +*/ + +/// @author Nils Krumnack + + + +// +// includes +// + +#include <AsgServices/AsgServiceConfig.h> +#include <AsgMessaging/MessageCheck.h> +#include <AsgTesting/UnitTest.h> +#include <AsgExampleTools/UnitTestService1.h> +#include <cmath> +#include <gtest/gtest.h> +#include <sstream> + +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" + +// +// method implementations +// + +using namespace asg::msgUserCode; + +namespace asg +{ + namespace + { + /// \brief make a unique service name to be used in unit tests + std::string makeUniqueName () + { + static unsigned index = 0; + std::ostringstream str; + str << "unique" << ++ index; + return str.str(); + } + } + + + + TEST (AsgServiceConfigTest, basic) + { + const std::string name = makeUniqueName(); + AsgServiceConfig config ("asg::UnitTestService1/" + name); + std::shared_ptr<IUnitTestService1> service; + ASSERT_SUCCESS (config.makeService (service)); + EXPECT_EQ (name, service->name()); + EXPECT_TRUE (service->isInitialized()); + EXPECT_EQ (-7, service->getPropertyInt()); + EXPECT_EQ ("", service->getPropertyString()); + } + + + + TEST (AsgServiceConfigTest, basic_properties) + { + const std::string name = makeUniqueName(); + AsgServiceConfig config ("asg::UnitTestService1/" + name); + EXPECT_SUCCESS (config.setProperty ("propertyInt", 17)); + EXPECT_SUCCESS (config.setProperty ("propertyString", "alpha")); + std::shared_ptr<IUnitTestService1> service; + ASSERT_SUCCESS (config.makeService (service)); + EXPECT_EQ (name, service->name()); + EXPECT_TRUE (service->isInitialized()); + EXPECT_EQ (17, service->getPropertyInt()); + EXPECT_EQ ("alpha", service->getPropertyString()); + } + + + + TEST (AsgServiceConfigTest, basic_propertyFromString) + { + const std::string name = makeUniqueName(); + AsgServiceConfig config ("asg::UnitTestService1/" + name); + config.setPropertyFromString ("propertyInt", "17"); + config.setPropertyFromString ("propertyString", "'alpha'"); + std::shared_ptr<IUnitTestService1> service; + ASSERT_SUCCESS (config.makeService (service)); + EXPECT_EQ (name, service->name()); + EXPECT_TRUE (service->isInitialized()); + EXPECT_EQ (17, service->getPropertyInt()); + EXPECT_EQ ("alpha", service->getPropertyString()); + } + + + +#ifdef XAOD_STANDALONE + TEST (AsgServiceConfigTest, basic_delete) + { + const std::string name = makeUniqueName(); + { + AsgServiceConfig config ("asg::UnitTestService1/" + name); + std::shared_ptr<IUnitTestService1> service; + ASSERT_SUCCESS (config.makeService (service)); + ASSERT_EQ (service, ServiceStore::get (name, true)); + } + ASSERT_EQ (nullptr, ServiceStore::get (name, true)); + } +#endif + + + + TEST (AsgServiceConfigTest, serviceHandle) + { + const std::string name = makeUniqueName(); + AsgServiceConfig config1 ("asg::UnitTestService1/" + name); + ServiceHandle<IUnitTestService1> service1 ("", "AsgServiceConfigTest"); + ASSERT_SUCCESS (config1.makeService (service1)); + EXPECT_EQ (name, service1->name()); + + ServiceHandle<IUnitTestService1> service2 ("asg::UnitTestService1/" + name, "UnitTest"); + ASSERT_SUCCESS (service2.retrieve ()); + EXPECT_EQ (name, service2->name()); + EXPECT_EQ (&*service1, &*service2); + } +} + +ATLAS_GOOGLE_TEST_MAIN -- GitLab