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 {
29 
35 inline bool checkForNode(std::string node_name)
36 {
37  std::vector<std::string> node_names;
38  return (ros::master::getNodes(node_names) &&
39  std::find(node_names.begin(), node_names.end(), node_name) == node_names.end());
40 }
41 
47 inline void waitForNode(std::string node_name, double loop_frequency = 10.0)
48 {
49  ROS_INFO_STREAM("Waiting for Node " << node_name);
50  std::vector<std::string> node_names;
51  while (ros::master::getNodes(node_names) &&
52  std::find(node_names.begin(), node_names.end(), node_name) == node_names.end())
53  {
54  ros::Rate(loop_frequency).sleep();
55  }
56  ROS_INFO_STREAM("Node " << node_name << " found");
57 }
58 
65 inline ::testing::AssertionResult waitForNodeShutdown(std::string node_name, double timeout = 1.0, double loop_frequency = 10.0)
66 {
67  std::vector<std::string> node_names;
68  auto start_time = ros::Time::now();
69  while (ros::master::getNodes(node_names) &&
70  std::find(node_names.begin(), node_names.end(), node_name) != node_names.end())
71  {
72  node_names.clear();
73  if (ros::Time::now() > start_time + ros::Duration(timeout))
74  {
75  return ::testing::AssertionFailure() << "Timed out waiting for shutdown of Node " << node_name;
76  }
77  ros::Rate(loop_frequency).sleep();
78  }
79  return ::testing::AssertionSuccess();
80 }
81 
82 template<class T>
83 static void initalizeAndRun(T& obj, const char *ip, unsigned int port)
84 {
85  if ( !obj.init(ip, port) )
86  {
87  ROS_ERROR("Initialization failed.");
88  return;
89  }
90  ROS_INFO_STREAM("Starting Server on " << ip << ":" << port);
91 
92  obj.run();
93 }
94 
95 } // namespace prbt_hardware_support
96 
97 
98 #endif // PRBT_HARDWARE_SUPPORT_ROS_TEST_HELPER_H
void waitForNode(std::string node_name, double loop_frequency=10.0)
Blocks until a node defined by node_name comes up.
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.
bool sleep()
#define ROS_INFO_STREAM(args)
static Time now()
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.
#define ROS_ERROR(...)


prbt_hardware_support
Author(s):
autogenerated on Thu Feb 13 2020 03:16:51