exercise_bond.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 
31 #include <bondcpp/bond.h>
32 #include <gtest/gtest.h>
33 
34 #ifndef _WIN32
35 # include <uuid/uuid.h>
36 #else
37 # include <rpc.h>
38 #endif
39 
40 #include <ros/spinner.h>
41 
42 #include <test_bond/TestBond.h>
43 
44 #include <string>
45 
46 const char TOPIC[] = "test_bond_topic";
47 
48 std::string genId()
49 {
50 #ifndef _WIN32
51  uuid_t uuid;
52  uuid_generate_random(uuid);
53  char uuid_str[40];
54  uuid_unparse(uuid, uuid_str);
55  return std::string(uuid_str);
56 #else
57  UUID uuid;
58  UuidCreate(&uuid);
59  RPC_CSTR str;
60  UuidToStringA(&uuid, &str);
61  std::string return_string(reinterpret_cast<char *>(str));
62  RpcStringFreeA(&str);
63  return return_string;
64 #endif
65 }
66 
68 {
69  ros::NodeHandle nh;
70  ros::ServiceClient cli = nh.serviceClient<test_bond::TestBond>("test_bond");
71  EXPECT_TRUE(cli);
72  EXPECT_TRUE(cli.waitForExistence(ros::Duration(5.0)));
73  return cli;
74 }
75 
76 TEST(ExerciseBondCpp, normal)
77 {
78  std::string id = genId();
79  bond::Bond bond(TOPIC, id);
81  EXPECT_TRUE(cli.isValid());
82  EXPECT_TRUE(cli.exists());
83  test_bond::TestBond::Request req;
84  test_bond::TestBond::Response resp;
85  req.topic = TOPIC;
86  req.id = id;
87  req.delay_death = ros::Duration(2.0);
88  ASSERT_TRUE(cli.call(req, resp));
89  bond.start();
90 
91  EXPECT_TRUE(bond.waitUntilFormed(ros::Duration(2.0)));
92  EXPECT_TRUE(bond.waitUntilBroken(ros::Duration(5.0)));
93 }
94 
95 TEST(ExerciseBondCpp, remoteNeverConnects)
96 {
97  std::string id = genId();
98  // Never starts the other side of the bond
99  bond::Bond bond(TOPIC, id);
100  bond.start();
101  EXPECT_FALSE(bond.waitUntilFormed(ros::Duration(1.0)));
102  EXPECT_TRUE(bond.waitUntilBroken(ros::Duration(20.0)));
103 }
104 
105 TEST(ExerciseBondCpp, heartbeatTimeout)
106 {
107  std::string id = genId();
108  bond::Bond bond(TOPIC, id);
109 
111  test_bond::TestBond::Request req;
112  test_bond::TestBond::Response resp;
113  req.topic = TOPIC;
114  req.id = id;
115  req.delay_death = ros::Duration(2.0);
116  req.inhibit_death_message = true;
117  ASSERT_TRUE(cli.call(req, resp));
118 
119  bond.start();
120 
121  EXPECT_TRUE(bond.waitUntilFormed(ros::Duration(2.0)));
122  EXPECT_TRUE(bond.waitUntilBroken(ros::Duration(10.0)));
123 }
124 
125 TEST(ExerciseBondCpp, cleanLocalDeath)
126 {
127  std::string id = genId();
128  bond::Bond bond(TOPIC, id);
129 
131  test_bond::TestBond::Request req;
132  test_bond::TestBond::Response resp;
133  req.topic = TOPIC;
134  req.id = id;
135  req.delay_death = ros::Duration(-1);
136  ASSERT_TRUE(cli.call(req, resp));
137 
138  bond.start();
139 
140  EXPECT_TRUE(bond.waitUntilFormed(ros::Duration(2.0)));
141  bond.breakBond();
142  EXPECT_TRUE(bond.waitUntilBroken(ros::Duration(5.0)));
143 }
144 
145 TEST(ExerciseBondCpp, localDeathNoAck)
146 {
147  std::string id = genId();
148  bond::Bond bond(TOPIC, id);
149 
151  test_bond::TestBond::Request req;
152  test_bond::TestBond::Response resp;
153  req.topic = TOPIC;
154  req.id = id;
155  req.delay_death = ros::Duration(-1);
156  req.inhibit_death_message = true;
157  ASSERT_TRUE(cli.call(req, resp));
158 
159  bond.start();
160 
161  EXPECT_TRUE(bond.waitUntilFormed(ros::Duration(2.0)));
162  bond.breakBond();
163  EXPECT_TRUE(bond.waitUntilBroken(ros::Duration(5.0)));
164 }
165 
166 TEST(ExerciseBondCpp, remoteIgnoresLocalDeath)
167 {
168  std::string id = genId();
169  bond::Bond bond(TOPIC, id);
170 
172  test_bond::TestBond::Request req;
173  test_bond::TestBond::Response resp;
174  req.topic = TOPIC;
175  req.id = id;
176  req.delay_death = ros::Duration(-1);
177  req.inhibit_death = true;
178  ASSERT_TRUE(cli.call(req, resp));
179 
180  bond.start();
181 
182  EXPECT_TRUE(bond.waitUntilFormed(ros::Duration(2.0)));
183  bond.breakBond();
184  EXPECT_FALSE(bond.waitUntilBroken(ros::Duration(1.0)));
185  EXPECT_TRUE(bond.waitUntilBroken(ros::Duration(10.0)));
186 }
187 
188 int main(int argc, char ** argv)
189 {
190  testing::InitGoogleTest(&argc, argv);
191  ros::init(argc, argv, "exercise_bondcpp", true);
192  ros::AsyncSpinner spinner(1);
193  spinner.start();
194  ros::NodeHandle nh;
195  int ret = RUN_ALL_TESTS();
196  spinner.stop();
197  return ret;
198 };
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
std::string genId()
void breakBond()
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool call(MReq &req, MRes &res)
bool waitUntilBroken(ros::Duration timeout=ros::Duration(-1))
bool waitUntilFormed(ros::Duration timeout=ros::Duration(-1))
void start()
TEST(ExerciseBondCpp, normal)
bool waitForExistence(ros::Duration timeout=ros::Duration(-1))
int main(int argc, char **argv)
ros::ServiceClient getService()
bool isValid() const
const char TOPIC[]


test_bond
Author(s): Stuart Glaser
autogenerated on Wed Sep 2 2020 03:40:47