Go to the documentation of this file.00001 #include <gtest/gtest.h>
00002
00003 #include <ros/master.h>
00004 #include <std_msgs/Int32.h>
00005 #include <ros/duration.h>
00006 #include <ros/subscriber.h>
00007 #include <ros/node_handle.h>
00008
00009 #include <atomic>
00010 #include <iostream>
00011 #include <string>
00012
00013 static double CALLBACK_TEST_TIME_LIMIT_ = 1.0;
00014
00015 class Int32CallbackTest
00016 {
00017 public:
00018 explicit Int32CallbackTest(const std::string& topic) : got_msg_(false)
00019 {
00020 ros::NodeHandle node_handle;
00021 int queue_size = 1;
00022 ros::Subscriber subscriber = node_handle.subscribe(topic, queue_size, &Int32CallbackTest::callback, this);
00023
00024 ros::Time time_out = ros::Time::now() + ros::Duration(CALLBACK_TEST_TIME_LIMIT_);
00025
00026 while (subscriber.getNumPublishers() == 0 && ros::Time::now() < time_out && ros::ok())
00027 {
00028 ros::Duration(0.001).sleep();
00029 }
00030
00031 while (!got_msg_.load() && ros::Time::now() < time_out && ros::ok())
00032 {
00033 ros::spinOnce();
00034 ros::Duration(0.001).sleep();
00035 }
00036 }
00037
00038 ~Int32CallbackTest()
00039 {
00040 EXPECT_TRUE(got_msg_.load());
00041 }
00042
00043 void callback(const std_msgs::Int32Ptr& number)
00044 {
00045 got_msg_ = true;
00046 }
00047
00048 private:
00049 std::atomic<bool> got_msg_;
00050 };
00051
00052 TEST(Publishers, internal)
00053 {
00054 Int32CallbackTest("fibonacci_publisher_internal");
00055 }
00056
00057 TEST(Publishers, other)
00058 {
00059 Int32CallbackTest("fibonacci_publisher_other");
00060 }
00061
00062 int main(int argc, char **argv)
00063 {
00064 testing::InitGoogleTest(&argc, argv);
00065 int time_seed = static_cast<int>(time(0));
00066 srand(time_seed);
00067
00068 ros::init(argc, argv, "publishers");
00069
00070 ros::NodeHandle node_handle("~");
00071 node_handle.getParam("single_test_time_limit", CALLBACK_TEST_TIME_LIMIT_);
00072
00073 return RUN_ALL_TESTS();
00074 }