Skip to content

Commit

Permalink
Test nodehandles (#64)
Browse files Browse the repository at this point in the history
* Added unit tests for the nodehandles

* fix copy-n-paste test suite

* disable failing namespace test tracked upstream

* various minor fixups
  • Loading branch information
mikaelarguedas committed Jul 17, 2017
1 parent 17d9014 commit a506c89
Show file tree
Hide file tree
Showing 9 changed files with 542 additions and 1 deletion.
17 changes: 16 additions & 1 deletion test_nodelet/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ if(CATKIN_ENABLE_TESTING)
)

#common commands for building c++ executables and libraries
add_library(${PROJECT_NAME} src/plus.cpp src/console_tests.cpp src/failing_nodelet.cpp)
add_library(${PROJECT_NAME} src/plus.cpp src/console_tests.cpp src/failing_nodelet.cpp src/nodehandles.cpp)
target_link_libraries(${PROJECT_NAME} ${BOOST_LIBRARIES}
${catkin_LIBRARIES}
)
Expand All @@ -32,6 +32,21 @@ if(CATKIN_ENABLE_TESTING)
add_rostest(test/test_bond_break_on_shutdown.launch)
add_rostest(test/test_unload_called_twice.launch)

add_rostest_gtest(test_nodehandles_different_namespaces
test/test_nodehandles_different_namespaces.test
test/test_nodehandles_different_namespaces.cpp)
target_link_libraries(test_nodehandles_different_namespaces ${catkin_LIBRARIES})

add_rostest_gtest(test_nodehandles_same_namespaces
test/test_nodehandles_same_namespaces.test
test/test_nodehandles_same_namespaces.cpp)
target_link_libraries(test_nodehandles_same_namespaces ${catkin_LIBRARIES})

add_rostest_gtest(test_nodehandles_manager_namespaced
test/test_nodehandles_manager_namespaced.test
test/test_nodehandles_manager_namespaced.cpp)
target_link_libraries(test_nodehandles_manager_namespaced ${catkin_LIBRARIES})

# Not a real test. Tries to measure overhead of CallbackQueueManager.
add_executable(benchmark src/benchmark.cpp)
target_link_libraries(benchmark ${BOOST_LIBRARIES}
Expand Down
30 changes: 30 additions & 0 deletions test_nodelet/src/nodehandles.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <nodelet/nodelet.h>
#include <pluginlib/class_list_macros.h>
#include <string>
#include <ros/ros.h>
#include <std_msgs/Bool.h>
#include <std_msgs/Byte.h>
#include <std_msgs/Time.h>

namespace test_nodelet
{

class NodehandleTest : public nodelet::Nodelet
{
public:
NodehandleTest(){};
virtual void onInit()
{
ros::NodeHandle nh = this->getNodeHandle();
ros::NodeHandle pnh = this->getPrivateNodeHandle();
global_pub_ = nh.advertise<std_msgs::Time>("/global", 1000);
namespaced_pub_ = nh.advertise<std_msgs::Byte>("namespaced", 1000);
private_pub_ = pnh.advertise<std_msgs::Bool>("private", 1000);
}
private:
ros::Publisher global_pub_, namespaced_pub_, private_pub_;
};

} // namespace test_nodelet

PLUGINLIB_EXPORT_CLASS(test_nodelet::NodehandleTest, nodelet::Nodelet);
154 changes: 154 additions & 0 deletions test_nodelet/test/test_nodehandles_different_namespaces.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*********************************************************************
* test_nodehandles_different_namespaces.cpp
*
* Software License Agreement (BSD License)
*
* Copyright (c) 2016, University of Patras
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of University of Patras nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Authors: Aris Synodinos
*********************************************************************/

#include <gtest/gtest.h>
#include <std_msgs/Bool.h>
#include <std_msgs/Byte.h>
#include <std_msgs/Empty.h>
#include <std_msgs/String.h>
#include <std_msgs/Float32.h>
#include <std_msgs/Float64.h>
#include <ros/ros.h>

TEST(DifferentNamespaces, NodeletPrivateNodehandle) {
ros::NodeHandle nh;
ros::Duration(2).sleep();
ros::master::V_TopicInfo master_topics;
ros::master::getTopics(master_topics);
bool found_topic = false;

for (ros::master::V_TopicInfo::iterator it = master_topics.begin() ; it != master_topics.end(); it++) {
const ros::master::TopicInfo& info = *it;
if (info.datatype.compare("std_msgs/Bool") == 0) {
found_topic = true;
EXPECT_STREQ("/nodelet_namespace/nodehandle_test/private", info.name.c_str());
}
}
EXPECT_TRUE(found_topic);
}

TEST(DifferentNamespaces, NodeletNamespacedNodehandle) {
ros::NodeHandle nh;
ros::Duration(2).sleep();
ros::master::V_TopicInfo master_topics;
ros::master::getTopics(master_topics);
bool found_topic = false;

for (ros::master::V_TopicInfo::iterator it = master_topics.begin() ; it != master_topics.end(); it++) {
const ros::master::TopicInfo& info = *it;
if (info.datatype.compare("std_msgs/Byte") == 0) {
found_topic = true;
EXPECT_STREQ("/nodelet_namespace/namespaced", info.name.c_str());
}
}
EXPECT_TRUE(found_topic);
}

TEST(DifferentNamespaces, NodeletGlobalNodehandle) {
ros::NodeHandle nh;
ros::Duration(2).sleep();
ros::master::V_TopicInfo master_topics;
ros::master::getTopics(master_topics);
bool found_topic = false;

for (ros::master::V_TopicInfo::iterator it = master_topics.begin() ; it != master_topics.end(); it++) {
const ros::master::TopicInfo& info = *it;
if (info.datatype.compare("std_msgs/Time") == 0) {
found_topic = true;
EXPECT_STREQ("/global", info.name.c_str());
}
}
EXPECT_TRUE(found_topic);
}

TEST(DifferentNamespaces, NodePrivateNodehandle) {
ros::NodeHandle nh("~");
ros::Publisher pub = nh.advertise<std_msgs::Empty>("private", 10);
ros::master::V_TopicInfo master_topics;
ros::master::getTopics(master_topics);
bool found_topic = false;

for (ros::master::V_TopicInfo::iterator it = master_topics.begin() ; it != master_topics.end(); it++) {
const ros::master::TopicInfo& info = *it;
if (info.datatype.compare("std_msgs/Empty") == 0) {
found_topic = true;
EXPECT_STREQ("/test_namespace/test_nodehandles/private", info.name.c_str());
}
}
EXPECT_TRUE(found_topic);
}

TEST(DifferentNamespaces, NodeNamespacedNodehandle) {
ros::NodeHandle nh;
ros::Publisher pub = nh.advertise<std_msgs::String>("namespaced", 10);
ros::master::V_TopicInfo master_topics;
ros::master::getTopics(master_topics);
bool found_topic = false;

for (ros::master::V_TopicInfo::iterator it = master_topics.begin() ; it != master_topics.end(); it++) {
const ros::master::TopicInfo& info = *it;
if (info.datatype.compare("std_msgs/String") == 0) {
found_topic = true;
EXPECT_STREQ("/test_namespace/namespaced", info.name.c_str());
}
}
EXPECT_TRUE(found_topic);
}

TEST(DifferentNamespaces, NodeGlobalNodehandle) {
ros::NodeHandle nh;
ros::Publisher pub = nh.advertise<std_msgs::Float32>("/public", 10);
ros::master::V_TopicInfo master_topics;
ros::master::getTopics(master_topics);
bool found_topic = false;

for (ros::master::V_TopicInfo::iterator it = master_topics.begin() ; it != master_topics.end(); it++) {
const ros::master::TopicInfo& info = *it;
if (info.datatype.compare("std_msgs/Float32") == 0) {
found_topic = true;
EXPECT_STREQ("/public", info.name.c_str());
}
}
EXPECT_TRUE(found_topic);
}

int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
ros::init(argc, argv, "test_nodehandles_different_namespaces");
return RUN_ALL_TESTS();
}
12 changes: 12 additions & 0 deletions test_nodelet/test/test_nodehandles_different_namespaces.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<launch>
<group ns="manager_namespace">
<node pkg="nodelet" type="nodelet" name="nodelet_manager" args="manager"/>
</group>
<group ns="nodelet_namespace">
<node pkg="nodelet" type="nodelet" name="nodehandle_test" args="load test_nodelet/NodehandleTest /manager_namespace/nodelet_manager"/>
</group>
<group ns="test_namespace">
<test test-name="test_nodehandles" pkg="test_nodelet" type="test_nodehandles_different_namespaces"/>
</group>
</launch>
155 changes: 155 additions & 0 deletions test_nodelet/test/test_nodehandles_manager_namespaced.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*********************************************************************
* test_nodehandles_same_namespaces.cpp
*
* Software License Agreement (BSD License)
*
* Copyright (c) 2016, University of Patras
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of University of Patras nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Authors: Aris Synodinos
*********************************************************************/

#include <gtest/gtest.h>
#include <std_msgs/Bool.h>
#include <std_msgs/Byte.h>
#include <std_msgs/Empty.h>
#include <std_msgs/String.h>
#include <std_msgs/Float32.h>
#include <ros/ros.h>

TEST(ManagerNamespaced, NodeletPrivateNodehandle) {
ros::NodeHandle nh;
ros::Duration(2).sleep();
ros::master::V_TopicInfo master_topics;
ros::master::getTopics(master_topics);
bool found_topic = false;

for (ros::master::V_TopicInfo::iterator it = master_topics.begin() ; it != master_topics.end(); it++) {
const ros::master::TopicInfo& info = *it;
if (info.datatype.compare("std_msgs/Bool") == 0) {
found_topic = true;
EXPECT_STREQ("/nodehandle_test/private", info.name.c_str());
}
}
EXPECT_TRUE(found_topic);
}

// TODO(mikaelarguedas) reenable this once
// https://github.com/ros/nodelet_core/issues/7 is fixed
// TEST(ManagerNamespaced, NodeletNamespacedNodehandle) {
// ros::NodeHandle nh;
// ros::Duration(2).sleep();
// ros::master::V_TopicInfo master_topics;
// ros::master::getTopics(master_topics);
// bool found_topic = false;
//
// for (ros::master::V_TopicInfo::iterator it = master_topics.begin() ; it != master_topics.end(); it++) {
// const ros::master::TopicInfo& info = *it;
// if (info.datatype.compare("std_msgs/Byte") == 0) {
// found_topic = true;
// EXPECT_STREQ("/namespaced", info.name.c_str());
// }
// }
// EXPECT_TRUE(found_topic);
// }

TEST(ManagerNamespaced, NodeletGlobalNodehandle) {
ros::NodeHandle nh;
ros::Duration(2).sleep();
ros::master::V_TopicInfo master_topics;
ros::master::getTopics(master_topics);
bool found_topic = false;

for (ros::master::V_TopicInfo::iterator it = master_topics.begin() ; it != master_topics.end(); it++) {
const ros::master::TopicInfo& info = *it;
if (info.datatype.compare("std_msgs/Time") == 0) {
found_topic = true;
EXPECT_STREQ("/global", info.name.c_str());
}
}
EXPECT_TRUE(found_topic);
}

TEST(ManagerNamespaced, NodePrivateNodehandle) {
ros::NodeHandle nh("~");
ros::Publisher pub = nh.advertise<std_msgs::Empty>("private", 10);
ros::master::V_TopicInfo master_topics;
ros::master::getTopics(master_topics);
bool found_topic = false;

for (ros::master::V_TopicInfo::iterator it = master_topics.begin() ; it != master_topics.end(); it++) {
const ros::master::TopicInfo& info = *it;
if (info.datatype.compare("std_msgs/Empty") == 0) {
found_topic = true;
EXPECT_STREQ("/test_nodehandles/private", info.name.c_str());
}
}
EXPECT_TRUE(found_topic);
}

TEST(ManagerNamespaced, NodeNamespacedNodehandle) {
ros::NodeHandle nh;
ros::Publisher pub = nh.advertise<std_msgs::String>("namespaced", 10);
ros::master::V_TopicInfo master_topics;
ros::master::getTopics(master_topics);
bool found_topic = false;

for (ros::master::V_TopicInfo::iterator it = master_topics.begin() ; it != master_topics.end(); it++) {
const ros::master::TopicInfo& info = *it;
if (info.datatype.compare("std_msgs/String") == 0) {
found_topic = true;
EXPECT_STREQ("/namespaced", info.name.c_str());
}
}
EXPECT_TRUE(found_topic);
}

TEST(ManagerNamespaced, NodeGlobalNodehandle) {
ros::NodeHandle nh;
ros::Publisher pub = nh.advertise<std_msgs::Float32>("/public", 10);
ros::master::V_TopicInfo master_topics;
ros::master::getTopics(master_topics);
bool found_topic = false;

for (ros::master::V_TopicInfo::iterator it = master_topics.begin() ; it != master_topics.end(); it++) {
const ros::master::TopicInfo& info = *it;
if (info.datatype.compare("std_msgs/Float32") == 0) {
found_topic = true;
EXPECT_STREQ("/public", info.name.c_str());
}
}
EXPECT_TRUE(found_topic);
}

int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
ros::init(argc, argv, "test_nodehandles_manager_namespaced");
return RUN_ALL_TESTS();
}
9 changes: 9 additions & 0 deletions test_nodelet/test/test_nodehandles_manager_namespaced.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<launch>
<group ns="manager_namespace">
<node pkg="nodelet" type="nodelet" name="nodelet_manager" args="manager"/>
</group>

<node pkg="nodelet" type="nodelet" name="nodehandle_test" args="load test_nodelet/NodehandleTest manager_namespace/nodelet_manager"/>
<test test-name="test_nodehandles" pkg="test_nodelet" type="test_nodehandles_manager_namespaced"/>
</launch>
Loading

0 comments on commit a506c89

Please sign in to comment.