test_wrappers.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include <rve_rpc/request_wrapper.h>
00031 #include <rve_rpc/response_wrapper.h>
00032 #include <rve_rpc/TestRequest.h>
00033 #include <rve_rpc/TestResponse.h>
00034 #include <std_msgs/UInt32.h>
00035 
00036 #include <ros/ros.h>
00037 
00038 #include <gtest/gtest.h>
00039 
00040 #include <boost/thread.hpp>
00041 
00042 using namespace rve_rpc;
00043 namespace ser = ros::serialization;
00044 
00045 RequestConstPtr g_request;
00046 void requestMessageCallback(const RequestConstPtr& msg)
00047 {
00048   g_request = msg;
00049 }
00050 
00051 RequestWrapperConstPtr g_request_wrapper;
00052 void requestWrapperCallback(const RequestWrapperConstPtr& wrapper)
00053 {
00054   g_request_wrapper = wrapper;
00055 }
00056 
00057 TEST(RequestWrapper, sendWrapperReceiveMessage)
00058 {
00059   g_request.reset();
00060   RequestWrapperPtr wrap(new RequestWrapper);
00061   wrap->message.serialize = serialize<uint32_t>;
00062   wrap->message.serialized_length = serializedLength<uint32_t>;
00063   wrap->message.message = boost::shared_ptr<uint32_t>(new uint32_t(5));
00064   wrap->request_id = rve_common::UUID::generate();
00065   wrap->method = "foo";
00066 
00067   ros::NodeHandle nh;
00068   ros::Subscriber sub = nh.subscribe("test", 0, requestMessageCallback);
00069   ros::Publisher pub = nh.advertise<RequestWrapper>("test", 0);
00070   pub.publish(wrap);
00071 
00072   while (!g_request)
00073   {
00074     ros::spinOnce();
00075     ros::WallDuration(0.01).sleep();
00076   }
00077 
00078   ASSERT_EQ(g_request->data.size(), 4U);
00079 
00080   ser::IStream stream((uint8_t*)&g_request->data.front(), (uint32_t)g_request->data.size());
00081   uint32_t val = 0;
00082   ser::deserialize(stream, val);
00083   EXPECT_EQ(val, 5U);
00084   EXPECT_EQ(wrap->request_id, g_request->request_id);
00085   EXPECT_STREQ(wrap->method.c_str(), g_request->method.c_str());
00086 }
00087 
00088 TEST(RequestWrapper, sendMessageReceiveWrapper)
00089 {
00090   g_request_wrapper.reset();
00091   RequestPtr req(new Request);
00092   req->data.resize(4);
00093   ser::IStream stream((uint8_t*)&req->data.front(), (uint32_t)req->data.size());
00094   ros::serialization::serialize(stream, (uint32_t)5);
00095   req->request_id = rve_common::UUID::generate();
00096   req->method = "bar";
00097 
00098   ros::NodeHandle nh;
00099   ros::Subscriber sub = nh.subscribe("test", 0, requestWrapperCallback);
00100   ros::Publisher pub = nh.advertise<RequestWrapper>("test", 0);
00101   pub.publish(req);
00102 
00103   while (!g_request_wrapper)
00104   {
00105     ros::spinOnce();
00106     ros::WallDuration(0.01).sleep();
00107   }
00108 
00109   ASSERT_EQ(g_request_wrapper->serialized_message.num_bytes, 4U);
00110   EXPECT_EQ(*g_request_wrapper->instantiate<uint32_t>(), 5U);
00111   EXPECT_EQ(g_request_wrapper->request_id, req->request_id);
00112   EXPECT_STREQ(g_request_wrapper->method.c_str(), req->method.c_str());
00113 }
00114 
00115 TEST(RequestWrapper, instantiate)
00116 {
00117   RequestWrapper wrap;
00118   wrap.message.serialize = serialize<uint32_t>;
00119   wrap.message.serialized_length = serializedLength<uint32_t>;
00120   wrap.message.message = boost::shared_ptr<uint32_t>(new uint32_t(5));
00121   uint32_t val = *wrap.instantiate<uint32_t>();
00122   EXPECT_EQ(val, 5U);
00123   std_msgs::UInt32ConstPtr msg = wrap.instantiate<std_msgs::UInt32>();
00124   EXPECT_EQ(msg->data, 5U);
00125 }
00126 
00127 ResponseConstPtr g_response;
00128 void responseMessageCallback(const ResponseConstPtr& msg)
00129 {
00130   g_response = msg;
00131 }
00132 
00133 ResponseWrapperConstPtr g_response_wrapper;
00134 void responseWrapperCallback(const ResponseWrapperConstPtr& wrapper)
00135 {
00136   g_response_wrapper = wrapper;
00137 }
00138 
00139 TEST(ResponseWrapper, sendWrapperReceiveMessage)
00140 {
00141   g_response.reset();
00142   ResponseWrapperPtr wrap(new ResponseWrapper);
00143   wrap->message.serialize = serialize<uint32_t>;
00144   wrap->message.serialized_length = serializedLength<uint32_t>;
00145   wrap->message.message = boost::shared_ptr<uint32_t>(new uint32_t(5));
00146   wrap->request_id = rve_common::UUID::generate();
00147   wrap->error_code = 5;
00148   wrap->error_string = "foo";
00149 
00150   ros::NodeHandle nh;
00151   ros::Subscriber sub = nh.subscribe("test", 0, responseMessageCallback);
00152   ros::Publisher pub = nh.advertise<ResponseWrapper>("test", 0);
00153   pub.publish(wrap);
00154 
00155   while (!g_response)
00156   {
00157     ros::spinOnce();
00158     ros::WallDuration(0.01).sleep();
00159   }
00160 
00161   ASSERT_EQ(g_response->data.size(), 4U);
00162 
00163   ser::IStream stream((uint8_t*)&g_response->data.front(), (uint32_t)g_response->data.size());
00164   uint32_t val = 0;
00165   ser::deserialize(stream, val);
00166   EXPECT_EQ(val, 5U);
00167   EXPECT_EQ(wrap->request_id, g_response->request_id);
00168   EXPECT_EQ(wrap->error_code, g_response->error_code);
00169   EXPECT_STREQ(wrap->error_string.c_str(), g_response->error_string.c_str());
00170 }
00171 
00172 TEST(ResponseWrapper, sendMessageReceiveWrapper)
00173 {
00174   g_response_wrapper.reset();
00175   ResponsePtr req(new Response);
00176   req->data.resize(4);
00177   ser::IStream stream((uint8_t*)&req->data.front(), (uint32_t)req->data.size());
00178   ros::serialization::serialize(stream, (uint32_t)5);
00179   req->request_id = rve_common::UUID::generate();
00180   req->error_code = 5;
00181   req->error_string = "bar";
00182 
00183   ros::NodeHandle nh;
00184   ros::Subscriber sub = nh.subscribe("test", 0, responseWrapperCallback);
00185   ros::Publisher pub = nh.advertise<ResponseWrapper>("test", 0);
00186   pub.publish(req);
00187 
00188   while (!g_response_wrapper)
00189   {
00190     ros::spinOnce();
00191     ros::WallDuration(0.01).sleep();
00192   }
00193 
00194   ASSERT_EQ(g_response_wrapper->serialized_message.num_bytes, 4U);
00195   EXPECT_EQ(*g_response_wrapper->instantiate<uint32_t>(), 5U);
00196   EXPECT_EQ(g_response_wrapper->request_id, req->request_id);
00197   EXPECT_EQ(g_response_wrapper->error_code, req->error_code);
00198   EXPECT_STREQ(g_response_wrapper->error_string.c_str(), req->error_string.c_str());
00199 }
00200 
00201 TEST(ResponseWrapper, instantiate)
00202 {
00203   ResponseWrapper wrap;
00204   wrap.message.serialize = serialize<uint32_t>;
00205   wrap.message.serialized_length = serializedLength<uint32_t>;
00206   wrap.message.message = boost::shared_ptr<uint32_t>(new uint32_t(5));
00207   uint32_t val = *wrap.instantiate<uint32_t>();
00208   EXPECT_EQ(val, 5U);
00209   std_msgs::UInt32ConstPtr msg = wrap.instantiate<std_msgs::UInt32>();
00210   EXPECT_EQ(msg->data, 5U);
00211 }
00212 
00213 int main(int argc, char** argv)
00214 {
00215   testing::InitGoogleTest(&argc, argv);
00216   ros::init(argc, argv, "test_intra");
00217   ros::NodeHandle nh;
00218 
00219   return RUN_ALL_TESTS();
00220 }
00221 


rve_rpc
Author(s): Josh Faust
autogenerated on Wed Dec 11 2013 14:30:53