async_test.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Pilz GmbH & Co. KG
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <algorithm>
18 
19 #include <ros/ros.h>
20 
22 
23 namespace testing
24 {
25 
26 void AsyncTest::barricade(std::string clear_event)
27 {
28  barricade({clear_event});
29 }
30 
31 void AsyncTest::barricade(std::initializer_list<std::string> clear_events)
32 {
33  std::unique_lock<std::mutex> lk(m_);
34 
35  std::stringstream events_stringstream;
36  for (auto event : clear_events)
37  {
38  events_stringstream << event << ", ";
39  }
40  ROS_DEBUG_NAMED("Test", "Adding Barricade[%s]", events_stringstream.str().c_str());
41 
42  std::copy_if(clear_events.begin(), clear_events.end(), std::inserter(clear_events_, clear_events_.end()),
43  [this](std::string event){ return this->waitlist_.count(event) == 0; });
44  waitlist_.clear();
45 
46  while(!clear_events_.empty())
47  {
48  cv_.wait(lk);
49  }
50 }
51 
52 void AsyncTest::triggerClearEvent(std::string event)
53 {
54  std::lock_guard<std::mutex> lk(m_);
55 
56  if (clear_events_.empty())
57  {
58  ROS_DEBUG_NAMED("Test", "Clearing Barricade[%s]", event.c_str());
59  waitlist_.insert(event);
60  }
61  else if (clear_events_.erase(event) < 1)
62  {
63  ROS_WARN_STREAM("Triggered event " << event << " despite not waiting for it.");
64  }
65  cv_.notify_one();
66 }
67 
68 } // namespace testing
void barricade(std::string clear_event)
Will block until the event given by clear_event is triggered. Unblocks immediately, if the event was triggered in advance.
Definition: async_test.cpp:26
std::condition_variable cv_
Definition: async_test.h:93
void triggerClearEvent(std::string event)
Triggeres a clear event. If a call to barricade is currently pending it will unblock as soon as all c...
Definition: async_test.cpp:52
#define ROS_DEBUG_NAMED(name,...)
std::set< std::string > clear_events_
Definition: async_test.h:94
#define ROS_WARN_STREAM(args)
std::mutex m_
Definition: async_test.h:92
std::set< std::string > waitlist_
Definition: async_test.h:95


pilz_testutils
Author(s):
autogenerated on Tue Feb 2 2021 03:50:13