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