The Gaudi Framework  master (da3d77e1)
TestingSvcs.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #include <GaudiKernel/Service.h>
12 
13 namespace GaudiTesting {
14 
17  class FailingSvc : public Service {
18  public:
20  using Service::Service;
21 
22  StatusCode initialize() override {
24  if ( sc.isFailure() ) {
25  error() << "failed to initialize base class" << endmsg;
26  return sc;
27  }
28  return handle( "initialize" );
29  }
30  StatusCode start() override {
32  if ( sc.isFailure() ) {
33  error() << "failed to start base class" << endmsg;
34  return sc;
35  }
36  return handle( "start" );
37  }
38  StatusCode stop() override {
39  StatusCode sc = handle( "stop" );
40  if ( sc.isFailure() ) return sc;
41  return Service::stop();
42  }
43  StatusCode finalize() override {
44  StatusCode sc = handle( "finalize" );
45  if ( sc.isFailure() ) return sc;
46  return Service::finalize();
47  }
48 
49  private:
51  this, "Transition", "", "In which transition to fail ['initialize', 'start', 'stop', 'finalize']" };
52  Gaudi::Property<std::string> m_mode{ this, "Mode", "failure", "Type of failure ['failure', 'exception']" };
53 
54  inline StatusCode handle( const std::string& transition ) {
55  if ( m_transition == transition ) {
56  if ( m_mode == "exception" ) {
57  throw GaudiException( "forced failure in " + transition, name(), StatusCode::FAILURE );
58  } else if ( m_mode == "failure" ) {
59  return StatusCode::FAILURE;
60  } else {
61  warning() << "Unknown type of failure '" << m_mode << "', use 'failure' or 'exception'" << endmsg;
62  }
63  }
64  return StatusCode::SUCCESS;
65  }
66  };
67 
68  DECLARE_COMPONENT( FailingSvc )
69 } // namespace GaudiTesting
GaudiTesting::FailingSvc::m_mode
Gaudi::Property< std::string > m_mode
Definition: TestingSvcs.cpp:52
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:118
std::string
STL class.
Service::start
StatusCode start() override
Definition: Service.cpp:187
GaudiTesting
Definition: __init__.py:1
GaudiException
Definition: GaudiException.h:31
GaudiTesting::FailingSvc::m_transition
Gaudi::Property< std::string > m_transition
Definition: TestingSvcs.cpp:50
GaudiTesting::FailingSvc::start
StatusCode start() override
Definition: TestingSvcs.cpp:30
GaudiTesting::FailingSvc::stop
StatusCode stop() override
Definition: TestingSvcs.cpp:38
Service::finalize
StatusCode finalize() override
Definition: Service.cpp:222
Service
Definition: Service.h:46
GaudiTesting::FailingSvc
Special service that issue a failure in one of the transitions (for testing).
Definition: TestingSvcs.cpp:17
Service::name
const std::string & name() const override
Retrieve name of the service
Definition: Service.cpp:332
StatusCode
Definition: StatusCode.h:65
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
Service.h
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
Service::stop
StatusCode stop() override
Definition: Service.cpp:181
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
Service::Service
Service(std::string name, ISvcLocator *svcloc)
Standard Constructor
Definition: Service.cpp:339
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
GaudiTesting::FailingSvc::finalize
StatusCode finalize() override
Definition: TestingSvcs.cpp:43
GaudiTesting::FailingSvc::handle
StatusCode handle(const std::string &transition)
Definition: TestingSvcs.cpp:54
Gaudi::Property< std::string >
GaudiTesting::FailingSvc::initialize
StatusCode initialize() override
Definition: TestingSvcs.cpp:22