ros_test_helper.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 Pilz GmbH & Co. KG
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13 
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef PRBT_HARDWARE_SUPPORT_ROS_TEST_HELPER_H
19 #define PRBT_HARDWARE_SUPPORT_ROS_TEST_HELPER_H
20 
21 #include <string>
22 #include <algorithm>
23 
24 #include <ros/ros.h>
25 #include <gtest/gtest.h>
26 
27 namespace prbt_hardware_support
28 {
34 inline bool checkForNode(std::string node_name)
35 {
36  std::vector<std::string> node_names;
37  return (ros::master::getNodes(node_names) &&
38  std::find(node_names.begin(), node_names.end(), node_name) == node_names.end());
39 }
40 
46 inline ::testing::AssertionResult waitForNode(const std::string node_name, const double loop_frequency = 10.0,
47  const ros::Duration timeout = ros::Duration(10.0))
48 {
49  ROS_INFO_STREAM("Waiting for Node " << node_name);
50  std::vector<std::string> node_names;
51  const auto timeout_time = ros::Time::now() + timeout;
52 
53  while (ros::master::getNodes(node_names) &&
54  std::find(node_names.begin(), node_names.end(), node_name) == node_names.end())
55  {
56  if (ros::Time::now() > timeout_time)
57  {
58  return ::testing::AssertionFailure() << "Timeout while waiting for node: \"" << node_name << "\"";
59  }
60  ros::Rate(loop_frequency).sleep();
61  }
62  return ::testing::AssertionSuccess() << "Found note \"" << node_name << "\"";
63 }
64 
71 inline ::testing::AssertionResult waitForSubscriber(const ros::Publisher& publisher,
72  const unsigned int subscriber_count = 1,
73  const double loop_frequency = 10.0,
74  const ros::Duration timeout = ros::Duration(10.0))
75 {
76  ROS_INFO_STREAM("Waiting for " << subscriber_count << " subscriber to topic \"" << publisher.getTopic() << "\"");
77  ros::Rate rate(loop_frequency);
78  const auto timeout_time = ros::Time::now() + timeout;
79  while (publisher.getNumSubscribers() < subscriber_count && ros::ok())
80  {
81  if (ros::Time::now() > timeout_time)
82  {
83  return ::testing::AssertionFailure() << "Timeout while waiting for " << subscriber_count
84  << " subscriber to topic \"" << publisher.getTopic() << "\"";
85  }
86  rate.sleep();
87  }
88  return ::testing::AssertionSuccess() << "Registered " << subscriber_count << " subscriber to topic \""
89  << publisher.getTopic() << "\"";
90 }
91 
98 inline ::testing::AssertionResult waitForNodeShutdown(std::string node_name, double timeout = 1.0,
99  double loop_frequency = 10.0)
100 {
101  std::vector<std::string> node_names;
102  auto start_time = ros::Time::now();
103  while (ros::master::getNodes(node_names) &&
104  std::find(node_names.begin(), node_names.end(), node_name) != node_names.end())
105  {
106  node_names.clear();
107  if (ros::Time::now() > start_time + ros::Duration(timeout))
108  {
109  return ::testing::AssertionFailure() << "Timed out waiting for shutdown of Node " << node_name;
110  }
111  ros::Rate(loop_frequency).sleep();
112  }
113  return ::testing::AssertionSuccess();
114 }
115 
116 template <class T>
117 static void initalizeAndRun(T& obj, const char* ip, unsigned int port)
118 {
119  if (!obj.init(ip, port))
120  {
121  ROS_ERROR("Initialization failed.");
122  return;
123  }
124  ROS_INFO_STREAM("Starting Server on " << ip << ":" << port);
125 
126  obj.run();
127 }
128 
129 } // namespace prbt_hardware_support
130 
131 #endif // PRBT_HARDWARE_SUPPORT_ROS_TEST_HELPER_H
static void initalizeAndRun(T &obj, const char *ip, unsigned int port)
bool checkForNode(std::string node_name)
Checks if a node is up and running.
ROSCPP_DECL bool ok()
inline ::testing::AssertionResult waitForNode(const std::string node_name, const double loop_frequency=10.0, const ros::Duration timeout=ros::Duration(10.0))
Blocks until a node defined by node_name comes up.
bool sleep()
std::string getTopic() const
#define ROS_INFO_STREAM(args)
static Time now()
inline ::testing::AssertionResult waitForSubscriber(const ros::Publisher &publisher, const unsigned int subscriber_count=1, const double loop_frequency=10.0, const ros::Duration timeout=ros::Duration(10.0))
Blocks until the specified number of subscribers is registered on a topic.
ROSCPP_DECL bool getNodes(V_string &nodes)
inline ::testing::AssertionResult waitForNodeShutdown(std::string node_name, double timeout=1.0, double loop_frequency=10.0)
Blocks until a node defined by node_name can no longer be found.
uint32_t getNumSubscribers() const
#define ROS_ERROR(...)


prbt_hardware_support
Author(s):
autogenerated on Mon Feb 28 2022 23:14:34