testUtils.h
Go to the documentation of this file.
1 #ifndef TESTUTILS_H
2 #define TESTUTILS_H
3 
4 #include <unistd.h>
5 #include <stdio.h>
6 #include <sys/types.h>
7 #include <sys/wait.h>
8 
9 //for the function prepareROSForTests
10 #include <ros/ros.h>
11 #include <end_effector/Utils.h>
12 
13 
18 namespace ROSEE {
19 
20 namespace TestUtils {
21 
22 class Process
23 {
24 
25 public:
26 
27  Process(std::vector<std::string> args);
28 
29  int wait();
30 
31  void kill(int signal = SIGTERM);
32 
33  ~Process();
34 
35 private:
36 
37  std::string _name;
38  pid_t _pid;
39 
40 };
41 
42 Process::Process(std::vector<std::string> args):
43  _name(args[0])
44 {
45  std::vector<const char *> args_cstr;
46  for(auto& a : args) args_cstr.push_back(a.c_str());
47  args_cstr.push_back(nullptr);
48 
49  char ** argv = (char**)args_cstr.data();
50 
51  _pid = ::fork();
52 
53  if(_pid == -1)
54  {
55  perror("fork");
56  throw std::runtime_error("Unable to fork()");
57  }
58 
59  if(_pid == 0)
60  {
61  ::execvp(argv[0], argv);
62  perror("execvp");
63  throw std::runtime_error("Unknown command");
64  }
65 
66 }
67 
69 {
70  int status;
71  while(::waitpid(_pid, &status, 0) != _pid);
72  printf("Child process '%s' exited with status %d\n", _name.c_str(), status);
73  return status;
74 
75 }
76 
77 void Process::kill(int signal)
78 {
79  ::kill(_pid, signal);
80  printf("Killed process '%s' with signal %d\n", _name.c_str(), signal);
81 }
82 
84 {
85  kill(SIGINT);
86  wait();
87 }
88 
95 int prepareROSForTests ( int argc, char **argv, std::string testName ) {
96 
97 
98 
99  ros::init ( argc, argv, testName );
100 
102  //ros::Time::init();
103  //while (!ros::master::check()) //wait for roscore to be ready
104  //{
105  // std::cout << "waiting for roscore..." << std::endl;
106  // ros::Duration(0.2).sleep();
107  //}
109 
110  //fill ros param with file models, needed by moveit parserMoveIt
111  std::string modelPathURDF = ROSEE::Utils::getPackagePath() + "configs/urdf/" + argv[1];
112  std::string modelPathSRDF = ROSEE::Utils::getPackagePath() + "configs/srdf/" + argv[1];
113 
114  //Is there a better way to parse?
115  std::ifstream urdf(modelPathURDF + ".urdf");
116  std::ifstream srdf(modelPathSRDF + ".srdf");
117  std::stringstream sUrdf, sSrdf;
118  sUrdf << urdf.rdbuf();
119  sSrdf << srdf.rdbuf();
120 
121  ros::param::set("robot_description" , sUrdf.str());
122  ros::param::set("robot_description_semantic" , sSrdf.str());
123  ros::param::set("robot_name", argv[1]);
124 
125  return 0;
126 }
127 
128 
129 } //namespace TestUtils
130 
131 } //namespace ROSEE
132 
133 #endif // TESTUTILS_H
void kill(int signal=SIGTERM)
Definition: testUtils.h:77
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ROSCPP_DECL void set(const std::string &key, const XmlRpc::XmlRpcValue &v)
int prepareROSForTests(int argc, char **argv, std::string testName)
Function to be called in the main of each test, it runs roscore and fill parameter server with robot ...
Definition: testUtils.h:95
Process(std::vector< std::string > args)
Definition: testUtils.h:42
static std::string getPackagePath()
Definition: Utils.h:87
list a


end-effector
Author(s): Luca Muratore , Davide Torielli , Liana Bertoni
autogenerated on Tue Apr 5 2022 02:57:53