rgbd
shm_server_gtest.cpp
Go to the documentation of this file.
1 #include "test_utils.h"
2 
3 #include <gtest/gtest.h>
4 
5 #include <boost/interprocess/shared_memory_object.hpp>
6 
7 #include <rgbd/image.h>
8 #include <rgbd/server_shm.h>
9 
10 #include <ros/duration.h>
11 #include <ros/init.h>
12 #include <ros/node_handle.h>
13 #include <ros/subscriber.h>
14 
15 #include <std_msgs/String.h>
16 
17 #include <memory>
18 #include <thread>
19 
20 
21 namespace ipc = boost::interprocess;
22 
23 
24 class SHMServer : public testing::Test
25 {
26 protected:
27  void SetUp() override
28  {
31  }
32 
33  const std::string test_server_name = "/test_ns/rgbd";
34  const std::string test_server_name_shm = "-test_ns-rgbd";
35 
38 };
39 
41 {
42 protected:
44  {
45  }
46 
47  void SetUp() override
48  {
49  sub = nh.subscribe<std_msgs::String>(test_server_name_hosts, 10, &SHMServerHostame::hostsCallback, this);
50  }
51 
52  void hostsCallback(const std_msgs::String::ConstPtr& msg)
53  {
54  if (msg->data != test_hostname)
55  correct_hostname = false;
56  }
57 
58  const std::string test_server_name_hosts = "/test_ns/rgbd/hosts";
59  const std::string test_hostname = "test_hostname";
60 
61  ros::NodeHandle nh;
62  ros::Subscriber sub;
64 };
65 
66 TEST_F(SHMServer, Initialize)
67 {
68  EXPECT_FALSE(ros::isShuttingDown());
69  EXPECT_THROW(ipc::shared_memory_object(ipc::open_only, test_server_name_shm.c_str(), ipc::read_write), ipc::interprocess_exception);
70 }
71 
72 TEST_F(SHMServer, SHMCreated)
73 {
74  EXPECT_FALSE(ros::isShuttingDown());
75  server.send(image);
76  EXPECT_FALSE(ros::isShuttingDown());
77  ros::Duration(1.1).sleep(); // Give thread some time to run
78  EXPECT_FALSE(ros::isShuttingDown());
79 }
80 
81 TEST_F(SHMServer, DeleteSHM)
82 {
83  EXPECT_FALSE(ros::isShuttingDown());
84  server.send(image);
85  EXPECT_FALSE(ros::isShuttingDown());
86  ipc::shared_memory_object::remove(test_server_name_shm.c_str());
87  ros::Duration(1.1).sleep(); // Give thread some time to run
88  EXPECT_TRUE(ros::isShuttingDown());
89 }
90 
91 TEST_F(SHMServerHostame, PubHostname)
92 {
93  std::thread thread = std::thread(rgbd::pubHostnameThreadFunc, std::ref(nh), test_server_name, test_hostname, 10);
94  ros::Duration(5.).sleep();
95  nh.shutdown();
96  thread.join();
97  EXPECT_FALSE(ros::isShuttingDown());
98  EXPECT_TRUE(correct_hostname);
99 }
100 
101 // Run all the tests that were declared with TEST()
102 int main(int argc, char **argv)
103 {
104  ros::init(argc, argv, "shm_server_behaviour");
105  testing::InitGoogleTest(&argc, argv);
106  return RUN_ALL_TESTS();
107 }
SHMServer::image
rgbd::Image image
Definition: shm_server_gtest.cpp:36
std::string
SHMServer::server
rgbd::ServerSHM server
Definition: shm_server_gtest.cpp:37
test_utils.h
SHMServerHostame::SetUp
void SetUp() override
Definition: shm_server_gtest.cpp:47
SHMServerHostame::SHMServerHostame
SHMServerHostame()
Definition: shm_server_gtest.cpp:43
SHMServer::SetUp
void SetUp() override
Definition: shm_server_gtest.cpp:27
SHMServer::test_server_name_shm
const std::string test_server_name_shm
Definition: shm_server_gtest.cpp:34
TEST_F
TEST_F(SHMServer, Initialize)
Definition: shm_server_gtest.cpp:66
rgbd::pubHostnameThreadFunc
void pubHostnameThreadFunc(ros::NodeHandle &nh, const std::string server_name, const std::string hostname, const float frequency)
Definition: server_shm.cpp:154
rgbd::Image
Definition: image.h:43
thread
main
int main(int argc, char **argv)
Definition: shm_server_gtest.cpp:102
SHMServerHostame::sub
ros::Subscriber sub
Definition: shm_server_gtest.cpp:62
SHMServerHostame::hostsCallback
void hostsCallback(const std_msgs::String::ConstPtr &msg)
Definition: shm_server_gtest.cpp:52
SHMServer
Definition: shm_server_gtest.cpp:24
image.h
memory
SHMServer::test_server_name
const std::string test_server_name
Definition: shm_server_gtest.cpp:33
rgbd::ServerSHM::initialize
void initialize(const std::string &name)
initialize Initialize shared memory server
Definition: server_shm.cpp:40
SHMServerHostame::nh
ros::NodeHandle nh
Definition: shm_server_gtest.cpp:61
SHMServerHostame::correct_hostname
bool correct_hostname
Definition: shm_server_gtest.cpp:63
SHMServerHostame
Definition: shm_server_gtest.cpp:40
SHMServerHostame::test_hostname
const std::string test_hostname
Definition: shm_server_gtest.cpp:59
rgbd::generateRandomImage
rgbd::Image generateRandomImage()
Definition: test_utils.h:36
rgbd::ServerSHM
Server which uses shared memory, this only works for clients on the same machine.
Definition: server_shm.h:22
server_shm.h
std::ref
T ref(T... args)
std::thread::join
T join(T... args)
SHMServerHostame::test_server_name_hosts
const std::string test_server_name_hosts
Definition: shm_server_gtest.cpp:58