fixture.h
Go to the documentation of this file.
1 
2 #include <gtest/gtest.h>
3 
4 #include <sys/socket.h>
5 #include <sys/fcntl.h>
6 #include <netinet/in.h>
7 #include <arpa/inet.h>
8 
9 #include "ros/ros.h"
10 
11 namespace rosserial {
12 #include "rosserial_test/ros.h"
13 }
14 
16 public:
17  virtual void SetUp()=0;
18  virtual void TearDown()=0;
19  int fd;
20 };
21 
22 class SerialSetup : public AbstractSetup {
23 public:
24  virtual void SetUp() {
25  ASSERT_NE(-1, fd = posix_openpt( O_RDWR | O_NOCTTY | O_NDELAY ));
26  ASSERT_NE(-1, grantpt(fd));
27  ASSERT_NE(-1, unlockpt(fd));
28 
29  char* pty_name;
30  ASSERT_TRUE((pty_name = ptsname(fd)) != NULL);
31 
32  ros::param::get("~port", symlink_name);
33  symlink(pty_name, symlink_name.c_str());
34  }
35  virtual void TearDown() {
36  unlink(symlink_name.c_str());
37  close(fd);
38  }
39  std::string symlink_name;
40 };
41 
42 class SocketSetup : public AbstractSetup {
43 public:
44  virtual void SetUp() {
45  ros::param::get("~tcp_port", tcp_port);
46  memset(&serv_addr, 0, sizeof(serv_addr));
47  serv_addr.sin_family = AF_INET;
48  serv_addr.sin_port = htons(tcp_port);
49  ASSERT_GE(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr), 0);
50 
51  // Try a bunch of times; we don't know how long it will take for the
52  // server to come up.
53  for (int attempt = 0; attempt < 10; attempt++)
54  {
55  fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
56  ASSERT_GE(fd, 0);
57  if (connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) >= 0)
58  {
59  // Connection successful, set nonblocking and return.
60  fcntl(fd, F_SETFL, O_NONBLOCK);
61  return;
62  }
63  close(fd);
64  ros::Duration(0.5).sleep();
65  }
66  FAIL() << "Unable to connect to rosserial socket server.";
67  }
68  virtual void TearDown() {
69  close(fd);
70  }
71  struct sockaddr_in serv_addr;
72  int tcp_port;
73 };
74 
75 class SingleClientFixture : public ::testing::Test {
76 protected:
77  static void SetModeFromParam() {
78  std::string mode;
79  ros::param::get("~mode", mode);
80  ROS_INFO_STREAM("Using test mode [" << mode << "]");
81  if (mode == "socket") {
82  setup = new SocketSetup();
83  } else if (mode == "serial") {
84  setup = new SerialSetup();
85  } else {
86  FAIL() << "Mode specified other than 'serial' or 'socket'.";
87  }
88  }
89  virtual void SetUp() {
90  if (setup == NULL) SetModeFromParam();
91  setup->SetUp();
92  rosserial::ClientComms::fd = setup->fd;
93  }
94  virtual void TearDown() {
95  setup->TearDown();
96  }
97 
101 };
103 
104 
SerialSetup::TearDown
virtual void TearDown()
Definition: fixture.h:35
setup
SingleClientFixture
Definition: fixture.h:75
SocketSetup::serv_addr
struct sockaddr_in serv_addr
Definition: fixture.h:71
AbstractSetup::TearDown
virtual void TearDown()=0
SingleClientFixture::setup
static AbstractSetup * setup
Definition: fixture.h:100
SerialSetup::symlink_name
std::string symlink_name
Definition: fixture.h:39
ros::param::get
ROSCPP_DECL bool get(const std::string &key, bool &b)
AbstractSetup
Definition: fixture.h:15
ros.h
AbstractSetup::fd
int fd
Definition: fixture.h:19
SingleClientFixture::SetModeFromParam
static void SetModeFromParam()
Definition: fixture.h:77
SerialSetup
Definition: fixture.h:22
SingleClientFixture::TearDown
virtual void TearDown()
Definition: fixture.h:94
SocketSetup::tcp_port
int tcp_port
Definition: fixture.h:72
SerialSetup::SetUp
virtual void SetUp()
Definition: fixture.h:24
ros::NodeHandle
NodeHandle_< ClientComms, 5, 5, 200, 200 > NodeHandle
Definition: ros.h:38
SocketSetup::TearDown
virtual void TearDown()
Definition: fixture.h:68
ros.h
ROS_INFO_STREAM
#define ROS_INFO_STREAM(args)
SingleClientFixture::nh
ros::NodeHandle nh
Definition: fixture.h:99
SocketSetup::SetUp
virtual void SetUp()
Definition: fixture.h:44
SingleClientFixture::client_nh
rosserial::ros::NodeHandle client_nh
Definition: fixture.h:98
rosserial
Definition: fixture.h:11
ros::Duration::sleep
bool sleep() const
SocketSetup
Definition: fixture.h:42
ros::Duration
AbstractSetup::SetUp
virtual void SetUp()=0
ros::NodeHandle
SingleClientFixture::SetUp
virtual void SetUp()
Definition: fixture.h:89


rosserial_test
Author(s):
autogenerated on Wed Mar 2 2022 00:58:17