Skip to content
Snippets Groups Projects
Commit 05bcc935 authored by Nils Krumnack's avatar Nils Krumnack
Browse files

introduce a unit test for dual-use services

Mostly a 1-to-1 copy of the dual-use tool test for AsgToolConfig.
parent a91ccba7
6 merge requests!58791DataQualityConfigurations: Modify L1Calo config for web display,!46784MuonCondInterface: Enable thread-safety checking.,!46776Updated LArMonitoring config file for WD to match new files produced using MT,!45405updated ART test cron job,!42417Draft: DIRE and VINCIA Base Fragments for Pythia 8.3,!41787introduce Dual-Use Services for use in AnalysisBase
......@@ -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
/*
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
/*
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
......@@ -6,5 +6,6 @@
<class name="asg::UnitTestTool3" />
<class name="asg::UnitTestTool2" />
<class name="asg::UnitTestTool1A" />
<class name="asg::UnitTestService1" />
</lcgdict>
......@@ -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 )
......
/*
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;
}
}
......@@ -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 )
......
/*
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment