controller_base_test.cpp
Go to the documentation of this file.
1 // Copyright (C) 2019, Pal Robotics S.L.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright notice,
7 // this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of Pal Robotics S.L. nor the names of its
12 // contributors may be used to endorse or promote products derived from
13 // this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 // POSSIBILITY OF SUCH DAMAGE.
27 
29 
32 
33 #include <ros/ros.h>
34 
35 #include <gmock/gmock.h>
36 
37 using ::testing::StrictMock;
38 using ::testing::_;
39 using ::testing::DoAll;
40 using ::testing::InvokeWithoutArgs;
41 using ::testing::Return;
42 
44 {
45 public:
46 
48  {
50  }
51 
52  MOCK_METHOD1(starting, void(const ros::Time&));
53  MOCK_METHOD2(update, void(const ros::Time&, const ros::Duration&));
54  MOCK_METHOD1(stopping, void(const ros::Time&));
55  MOCK_METHOD1(waiting, void(const ros::Time&));
56  MOCK_METHOD1(aborting, void(const ros::Time&));
59 };
60 
61 TEST(ControllerBaseAPI, DefaultStateTest)
62 {
63  StrictMock<ControllerMock> controller;
64 
65  ASSERT_FALSE(controller.isInitialized());
66  ASSERT_FALSE(controller.isRunning());
67  ASSERT_FALSE(controller.isStopped());
68  ASSERT_FALSE(controller.isWaiting());
69  ASSERT_FALSE(controller.isAborted());
70 }
71 
72 TEST(ControllerBaseAPI, IsInitializedTest)
73 {
74  StrictMock<ControllerMock> controller;
75 
76  hardware_interface::RobotHW* robot_hw = nullptr;
77  ros::NodeHandle* node_handle = nullptr;
79  const ros::Time time;
80 
81  EXPECT_CALL(controller, initRequest(_, _, _, _))
82  .Times(1)
83  .WillOnce(DoAll(InvokeWithoutArgs(&controller, &ControllerMock::initializeState),
84  Return(true)));
85 
86  // not initialized by default
87  ASSERT_FALSE(controller.isInitialized());
88 
89  ASSERT_TRUE(controller.initRequest(robot_hw, *node_handle, *node_handle, resources));
90  ASSERT_TRUE(controller.isInitialized());
91 }
92 
93 TEST(ControllerBaseAPI, IsRunningTest)
94 {
95  StrictMock<ControllerMock> controller;
96 
97  hardware_interface::RobotHW* robot_hw = nullptr;
98  ros::NodeHandle* node_handle = nullptr;
100  const ros::Time time;
101 
102  EXPECT_CALL(controller, starting(_)).Times(1);
103  EXPECT_CALL(controller, stopping(_)).Times(1);
104  EXPECT_CALL(controller, initRequest(_, _, _, _))
105  .Times(1)
106  .WillOnce(DoAll(InvokeWithoutArgs(&controller, &ControllerMock::initializeState),
107  Return(true)));
108 
109  // not running by default
110  ASSERT_FALSE(controller.isRunning());
111 
112  ASSERT_TRUE(controller.initRequest(robot_hw, *node_handle, *node_handle, resources));
113  ASSERT_TRUE(controller.startRequest(time));
114  ASSERT_TRUE(controller.isRunning());
115 
116  ASSERT_TRUE(controller.stopRequest(time));
117  ASSERT_FALSE(controller.isRunning());
118 }
119 
120 TEST(ControllerBaseAPI, IsStoppedTest)
121 {
122  StrictMock<ControllerMock> controller;
123 
124  hardware_interface::RobotHW* robot_hw = nullptr;
125  ros::NodeHandle* node_handle = nullptr;
127  const ros::Time time;
128 
129  EXPECT_CALL(controller, starting(_)).Times(1);
130  EXPECT_CALL(controller, stopping(_)).Times(1);
131  EXPECT_CALL(controller, initRequest(_, _, _, _))
132  .Times(1)
133  .WillOnce(DoAll(InvokeWithoutArgs(&controller, &ControllerMock::initializeState),
134  Return(true)));
135 
136  // not stopped by default
137  ASSERT_FALSE(controller.isStopped());
138 
139  ASSERT_TRUE(controller.initRequest(robot_hw, *node_handle, *node_handle, resources));
140  ASSERT_TRUE(controller.stopRequest(time));
141  ASSERT_TRUE(controller.isStopped());
142 
143  ASSERT_TRUE(controller.startRequest(time));
144  ASSERT_FALSE(controller.isStopped());
145 }
146 
147 TEST(ControllerBaseAPI, IsWaitingTest)
148 {
149  StrictMock<ControllerMock> controller;
150 
151  hardware_interface::RobotHW* robot_hw = nullptr;
152  ros::NodeHandle* node_handle = nullptr;
154  const ros::Time time;
155 
156  EXPECT_CALL(controller, starting(_)).Times(1);
157  EXPECT_CALL(controller, waiting(_)).Times(1);
158  EXPECT_CALL(controller, initRequest(_, _, _, _))
159  .Times(1)
160  .WillOnce(DoAll(InvokeWithoutArgs(&controller, &ControllerMock::initializeState),
161  Return(true)));
162 
163  // not waiting by default
164  ASSERT_FALSE(controller.isWaiting());
165 
166  ASSERT_TRUE(controller.initRequest(robot_hw, *node_handle, *node_handle, resources));
167  ASSERT_TRUE(controller.waitRequest(time));
168  ASSERT_TRUE(controller.isWaiting());
169 
170  ASSERT_TRUE(controller.startRequest(time));
171  ASSERT_FALSE(controller.isWaiting());
172 }
173 
174 TEST(ControllerBaseAPI, StartRequestTest)
175 {
176  StrictMock<ControllerMock> controller;
177 
178  hardware_interface::RobotHW* robot_hw = nullptr;
179  ros::NodeHandle* node_handle = nullptr;
181  const ros::Time time;
182 
183  EXPECT_CALL(controller, starting(_)).Times(2);
184  EXPECT_CALL(controller, initRequest(_, _, _, _))
185  .Times(1)
186  .WillOnce(DoAll(InvokeWithoutArgs(&controller, &ControllerMock::initializeState),
187  Return(true)));
188 
189  // not initialized
190  ASSERT_FALSE(controller.startRequest(time));
191 
192  ASSERT_TRUE(controller.initRequest(robot_hw, *node_handle, *node_handle, resources));
193  ASSERT_TRUE(controller.startRequest(time));
194 
195  // can initialize multiple times
196  ASSERT_TRUE(controller.startRequest(time));
197 }
198 
199 TEST(ControllerBaseAPI, StopRequestTest)
200 {
201  StrictMock<ControllerMock> controller;
202 
203  hardware_interface::RobotHW* robot_hw = nullptr;
204  ros::NodeHandle* node_handle = nullptr;
206  const ros::Time time;
207 
208  EXPECT_CALL(controller, stopping(_)).Times(2);
209  EXPECT_CALL(controller, initRequest(_, _, _, _))
210  .Times(1)
211  .WillOnce(DoAll(InvokeWithoutArgs(&controller, &ControllerMock::initializeState),
212  Return(true)));
213 
214  // not initialized
215  ASSERT_FALSE(controller.stopRequest(time));
216 
217  ASSERT_TRUE(controller.initRequest(robot_hw, *node_handle, *node_handle, resources));
218  ASSERT_TRUE(controller.stopRequest(time));
219 
220  // can stop multiple times
221  ASSERT_TRUE(controller.stopRequest(time));
222 }
223 
224 TEST(ControllerBaseAPI, WaitRequestTest)
225 {
226  StrictMock<ControllerMock> controller;
227 
228  hardware_interface::RobotHW* robot_hw = nullptr;
229  ros::NodeHandle* node_handle = nullptr;
231  const ros::Time time;
232 
233  EXPECT_CALL(controller, waiting(_)).Times(2);
234  EXPECT_CALL(controller, initRequest(_, _, _, _))
235  .Times(1)
236  .WillOnce(DoAll(InvokeWithoutArgs(&controller, &ControllerMock::initializeState),
237  Return(true)));
238 
239  // not initialized
240  ASSERT_FALSE(controller.waitRequest(time));
241 
242  ASSERT_TRUE(controller.initRequest(robot_hw, *node_handle, *node_handle, resources));
243  ASSERT_TRUE(controller.waitRequest(time));
244 
245  // can wait multiple times
246  ASSERT_TRUE(controller.waitRequest(time));
247 }
248 
249 TEST(ControllerBaseAPI, AbortRequestTest)
250 {
251  StrictMock<ControllerMock> controller;
252 
253  hardware_interface::RobotHW* robot_hw = nullptr;
254  ros::NodeHandle* node_handle = nullptr;
256  const ros::Time time;
257 
258  EXPECT_CALL(controller, aborting(_)).Times(2);
259  EXPECT_CALL(controller, initRequest(_, _, _, _))
260  .Times(1)
261  .WillOnce(DoAll(InvokeWithoutArgs(&controller, &ControllerMock::initializeState),
262  Return(true)));
263 
264  // not initialized
265  ASSERT_FALSE(controller.abortRequest(time));
266 
267  ASSERT_TRUE(controller.initRequest(robot_hw, *node_handle, *node_handle, resources));
268  ASSERT_TRUE(controller.abortRequest(time));
269 
270  // can abort multiple times
271  ASSERT_TRUE(controller.abortRequest(time));
272 }
273 
274 TEST(ControllerBaseAPI, UpdateRequestTest)
275 {
276  StrictMock<ControllerMock> controller;
277 
278  hardware_interface::RobotHW* robot_hw = nullptr;
279  ros::NodeHandle* node_handle = nullptr;
281  const ros::Time time;
282  const ros::Duration period;
283 
284  EXPECT_CALL(controller, starting(_)).Times(1);
285  EXPECT_CALL(controller, update(_, _)).Times(1);
286  EXPECT_CALL(controller, initRequest(_, _, _, _))
287  .Times(1)
288  .WillOnce(DoAll(InvokeWithoutArgs(&controller, &ControllerMock::initializeState),
289  Return(true)));
290 
291  // update is not called unless state is RUNNING
292  controller.updateRequest(time, period);
293 
294  ASSERT_TRUE(controller.initRequest(robot_hw, *node_handle, *node_handle, resources));
295  ASSERT_TRUE(controller.startRequest(time));
296 
297  controller.updateRequest(time, period);
298 }
299 
300 int main(int argc, char** argv)
301 {
302  testing::InitGoogleTest(&argc, argv);
303 
304  int ret = RUN_ALL_TESTS();
305  return ret;
306 }
TEST(ControllerBaseAPI, DefaultStateTest)
std::vector< hardware_interface::InterfaceResources > ClaimedResources
virtual void stopping(const ros::Time &)
This is called from within the realtime thread just after the last update call before the controller ...
MOCK_METHOD2(update, void(const ros::Time &, const ros::Duration &))
virtual void update(const ros::Time &time, const ros::Duration &period)=0
This is called periodically by the realtime thread when the controller is running.
Abstract Controller Interface.
MOCK_METHOD4(initRequest, bool(hardware_interface::RobotHW *, ros::NodeHandle &, ros::NodeHandle &, ClaimedResources &))
MOCK_METHOD1(starting, void(const ros::Time &))
int main(int argc, char **argv)
virtual bool initRequest(hardware_interface::RobotHW *robot_hw, ros::NodeHandle &root_nh, ros::NodeHandle &controller_nh, ClaimedResources &claimed_resources)=0
Request that the controller be initialized.
enum controller_interface::ControllerBase::@0 state_
The current execution state of the controller.
virtual void aborting(const ros::Time &)
This is called from within the realtime thread when the controller needs to be aborted.
virtual void waiting(const ros::Time &)
This is called from within the realtime thread while the controller is waiting to start...
virtual void starting(const ros::Time &)
This is called from within the realtime thread just before the first call to update.


controller_interface
Author(s): Wim Meeussen
autogenerated on Mon Feb 28 2022 23:30:15