artdaq_demo  v3_06_01
MisbehaviorTest_policy.cc
1 #include "artdaq/DAQdata/Globals.hh"
2 #include "artdaq/RoutingPolicies/PolicyMacros.hh"
3 #include "artdaq/RoutingPolicies/RoutingMasterPolicy.hh"
4 #include "fhiclcpp/ParameterSet.h"
5 #include "messagefacility/MessageLogger/MessageLogger.h"
6 
7 namespace demo {
11 class MisbehaviorTest : public artdaq::RoutingMasterPolicy
12 {
13 public:
28  explicit MisbehaviorTest( fhicl::ParameterSet ps );
29 
33  virtual ~MisbehaviorTest() = default;
34 
39  artdaq::detail::RoutingPacket GetCurrentTable() override;
40 
41 private:
42  artdaq::Fragment::sequence_id_t misbehave_after_;
43  size_t misbehave_pause_ms_;
44  bool misbehave_conflicting_table_data_;
45  bool misbehave_corrupt_table_data_;
46  bool misbehave_overload_event_builder_;
47 };
48 
49 MisbehaviorTest::MisbehaviorTest( fhicl::ParameterSet ps )
50  : RoutingMasterPolicy( ps )
51  , misbehave_after_( ps.get<size_t>( "misbehave_after_n_events", 1000 ) )
52  , misbehave_pause_ms_( ps.get<size_t>( "misbehave_pause_time_ms", 0 ) )
53  , misbehave_conflicting_table_data_( ps.get<bool>( "misbehave_send_conflicting_table_data", false ) )
54  , misbehave_corrupt_table_data_( ps.get<bool>( "misbehave_send_corrupt_table_data", false ) )
55  , misbehave_overload_event_builder_( ps.get<bool>( "misbehave_overload_event_builder", false ) )
56 {
57  srand( time( 0 ) );
58  auto count = ( misbehave_conflicting_table_data_ ? 1 : 0 ) + ( misbehave_corrupt_table_data_ ? 1 : 0 ) +
59  ( misbehave_overload_event_builder_ ? 1 : 0 ) + ( misbehave_pause_ms_ > 0 ? 1 : 0 );
60  if ( count > 1 )
61  {
62  mf::LogWarning( "MisbehaviorTest" ) << "Only one misbehavior is allowed at a time!";
63  exit( 3 );
64  }
65 }
66 
67 artdaq::detail::RoutingPacket MisbehaviorTest::GetCurrentTable()
68 {
69  auto tokens = getTokensSnapshot();
70  artdaq::detail::RoutingPacket output;
71 
72  auto half = tokens->size() / 2;
73  size_t counter = 0;
74  for ( ; counter < half; ++counter )
75  { output.emplace_back( artdaq::detail::RoutingPacketEntry( next_sequence_id_++, tokens->at( counter ) ) ); }
76 
77  if ( next_sequence_id_ > misbehave_after_ )
78  {
79  if ( tokens->size() > 0 )
80  {
81  if ( misbehave_pause_ms_ > 0 )
82  {
83  mf::LogError( "MisbehaviorTest" )
84  << "Pausing for " << misbehave_pause_ms_ << " milliseconds before sending table update";
85  usleep( misbehave_pause_ms_ * 1000 );
86  }
87  if ( misbehave_conflicting_table_data_ )
88  {
89  mf::LogError( "MisbehaviorTest" ) << "Adding conflicting data point to output";
90  output.emplace_back( next_sequence_id_, tokens->at( counter ) + 1 );
91  }
92  if ( misbehave_corrupt_table_data_ )
93  {
94  mf::LogError( "MisbehaviorTest" ) << "Adding random data point";
95  output.emplace_back( seedAndRandom(), rand() );
96  }
97  if ( misbehave_overload_event_builder_ )
98  {
99  mf::LogError( "MisbehaviorTest" ) << "Sending 100 events in a row to Rank " << tokens->at( 0 );
100  for ( auto ii = 0; ii < 100; ++ii ) { output.emplace_back( next_sequence_id_++, tokens->at( 0 ) ); }
101  }
102  misbehave_after_ += misbehave_after_;
103  }
104  }
105 
106  for ( ; counter < tokens->size(); ++counter )
107  { output.emplace_back( artdaq::detail::RoutingPacketEntry( next_sequence_id_++, tokens->at( counter ) ) ); }
108 
109  return output;
110 }
111 } // namespace demo
112 
113 DEFINE_ARTDAQ_ROUTING_POLICY( demo::MisbehaviorTest )
MisbehaviorTest(fhicl::ParameterSet ps)
MisbehaviorTest Constructor.
artdaq::detail::RoutingPacket GetCurrentTable() override
Generate and return a Routing Table.
A test RoutingMasterPolicy which does various &quot;bad&quot; things, determined by configuration.
virtual ~MisbehaviorTest()=default
MisbehaviorTest default Destructor.