forked from ros2/rclcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_graph_listener.cpp
More file actions
300 lines (250 loc) · 10.3 KB
/
test_graph_listener.cpp
File metadata and controls
300 lines (250 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright 2020 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <gtest/gtest.h>
#include <memory>
#include "rclcpp/contexts/default_context.hpp"
#include "rclcpp/graph_listener.hpp"
#include "rclcpp/node_interfaces/node_graph.hpp"
#include "rclcpp/rclcpp.hpp"
#include "../mocking_utils/patch.hpp"
#include "../utils/rclcpp_gtest_macros.hpp"
namespace
{
constexpr char node_name[] = "node";
constexpr char node_namespace[] = "ns";
constexpr char shutdown_error_str[] = "GraphListener already shutdown";
} // namespace
class TestGraphListener : public ::testing::Test
{
public:
void SetUp()
{
rclcpp::init(0, nullptr);
node_ = std::make_shared<rclcpp::Node>(node_name, node_namespace);
node_graph_ = node_->get_node_graph_interface();
ASSERT_NE(nullptr, node_graph_);
graph_listener_ =
std::make_shared<rclcpp::graph_listener::GraphListener>(
rclcpp::contexts::get_global_default_context());
}
void TearDown()
{
rclcpp::shutdown();
}
protected:
std::shared_ptr<rclcpp::Node> node() {return node_;}
rclcpp::node_interfaces::NodeGraphInterface * node_graph() {return node_graph_.get();}
std::shared_ptr<rclcpp::graph_listener::GraphListener> graph_listener() {return graph_listener_;}
private:
std::shared_ptr<rclcpp::Node> node_;
rclcpp::node_interfaces::NodeGraphInterface::SharedPtr node_graph_;
std::shared_ptr<rclcpp::graph_listener::GraphListener> graph_listener_;
};
/* Run base class init/shutdown */
TEST_F(TestGraphListener, construction_and_destruction) {
EXPECT_FALSE(graph_listener()->has_node(node_graph()));
EXPECT_FALSE(graph_listener()->is_shutdown());
}
// Required for mocking_utils below
MOCKING_UTILS_BOOL_OPERATOR_RETURNS_FALSE(rcl_guard_condition_options_t, ==)
MOCKING_UTILS_BOOL_OPERATOR_RETURNS_FALSE(rcl_guard_condition_options_t, !=)
MOCKING_UTILS_BOOL_OPERATOR_RETURNS_FALSE(rcl_guard_condition_options_t, <)
MOCKING_UTILS_BOOL_OPERATOR_RETURNS_FALSE(rcl_guard_condition_options_t, >)
/* Error creating a new graph listener */
TEST_F(TestGraphListener, error_construct_graph_listener) {
using rclcpp::contexts::get_global_default_context;
auto mock = mocking_utils::patch_and_return(
"lib:rclcpp", rcl_guard_condition_init, RCL_RET_ERROR);
RCLCPP_EXPECT_THROW_EQ(
{
auto graph_listener_error =
std::make_shared<rclcpp::graph_listener::GraphListener>(get_global_default_context());
graph_listener_error.reset();
}, std::runtime_error("failed to create interrupt guard condition: error not set"));
}
// Required for mocking_utils below
MOCKING_UTILS_BOOL_OPERATOR_RETURNS_FALSE(rcutils_allocator_t, ==)
MOCKING_UTILS_BOOL_OPERATOR_RETURNS_FALSE(rcutils_allocator_t, !=)
MOCKING_UTILS_BOOL_OPERATOR_RETURNS_FALSE(rcutils_allocator_t, <)
MOCKING_UTILS_BOOL_OPERATOR_RETURNS_FALSE(rcutils_allocator_t, >)
/* Errors that occur when initializing the graph_listener */
TEST_F(TestGraphListener, error_start_graph_listener) {
{
auto mock = mocking_utils::patch_and_return(
"lib:rclcpp", rcl_wait_set_init, RCL_RET_ERROR);
RCLCPP_EXPECT_THROW_EQ(
graph_listener()->start_if_not_started(),
std::runtime_error("failed to initialize wait set: error not set"));
}
{
EXPECT_NO_THROW(graph_listener()->shutdown());
RCLCPP_EXPECT_THROW_EQ(
graph_listener()->start_if_not_started(),
std::runtime_error(shutdown_error_str));
}
}
class TestGraphListenerProtectedMethods : public rclcpp::graph_listener::GraphListener
{
public:
explicit TestGraphListenerProtectedMethods(std::shared_ptr<rclcpp::Context> parent_context)
: rclcpp::graph_listener::GraphListener{parent_context}
{}
void run_protected()
{
this->run();
}
void mock_init_wait_set()
{
this->init_wait_set();
}
void mock_cleanup_wait_set()
{
this->cleanup_wait_set();
}
};
/* Errors running graph protected methods */
TEST_F(TestGraphListener, error_run_graph_listener_destroy_context) {
auto context_to_destroy = std::make_shared<rclcpp::contexts::DefaultContext>();
context_to_destroy->init(0, nullptr);
auto graph_listener_error =
std::make_shared<TestGraphListenerProtectedMethods>(context_to_destroy);
context_to_destroy.reset();
EXPECT_THROW(
graph_listener_error->run_protected(),
rclcpp::exceptions::RCLError);
}
TEST_F(TestGraphListener, error_run_graph_listener_mock_wait_set_clear) {
auto global_context = rclcpp::contexts::get_global_default_context();
auto graph_listener_test =
std::make_shared<TestGraphListenerProtectedMethods>(global_context);
graph_listener_test->mock_init_wait_set();
auto mock_wait_set_clear = mocking_utils::patch_and_return(
"lib:rclcpp", rcl_wait_set_clear, RCL_RET_ERROR);
RCLCPP_EXPECT_THROW_EQ(
graph_listener_test->run_protected(),
std::runtime_error("failed to clear wait set: error not set"));
}
TEST_F(TestGraphListener, error_run_graph_listener_mock_wait_set_add_guard_condition) {
auto global_context = rclcpp::contexts::get_global_default_context();
auto graph_listener_test =
std::make_shared<TestGraphListenerProtectedMethods>(global_context);
graph_listener_test->mock_init_wait_set();
auto mock_wait_set_clear = mocking_utils::patch_and_return(
"lib:rclcpp", rcl_wait_set_add_guard_condition, RCL_RET_ERROR);
RCLCPP_EXPECT_THROW_EQ(
graph_listener_test->run_protected(),
std::runtime_error("failed to add interrupt guard condition to wait set: error not set"));
}
TEST_F(TestGraphListener, error_run_graph_listener_mock_wait_error) {
auto global_context = rclcpp::contexts::get_global_default_context();
auto graph_listener_test =
std::make_shared<TestGraphListenerProtectedMethods>(global_context);
graph_listener_test->mock_init_wait_set();
auto mock_wait_set_clear = mocking_utils::patch_and_return(
"lib:rclcpp", rcl_wait, RCL_RET_ERROR);
RCLCPP_EXPECT_THROW_EQ(
graph_listener_test->run_protected(),
std::runtime_error("failed to wait on wait set: error not set"));
}
TEST_F(TestGraphListener, error_run_graph_listener_mock_wait_timeout) {
auto global_context = rclcpp::contexts::get_global_default_context();
auto graph_listener_test =
std::make_shared<TestGraphListenerProtectedMethods>(global_context);
graph_listener_test->mock_init_wait_set();
auto mock_wait_set_clear = mocking_utils::patch_and_return(
"lib:rclcpp", rcl_wait, RCL_RET_TIMEOUT);
RCLCPP_EXPECT_THROW_EQ(
graph_listener_test->run_protected(),
std::runtime_error("rcl_wait unexpectedly timed out"));
}
/* Add/Remove node usage */
TEST_F(TestGraphListener, test_graph_listener_add_remove_node) {
EXPECT_FALSE(graph_listener()->has_node(node_graph()));
graph_listener()->add_node(node_graph());
EXPECT_TRUE(graph_listener()->has_node(node_graph()));
graph_listener()->remove_node(node_graph());
EXPECT_FALSE(graph_listener()->has_node(node_graph()));
}
/* Add/Remove node error usage */
TEST_F(TestGraphListener, test_errors_graph_listener_add_remove_node) {
// nullptrs tests
EXPECT_FALSE(graph_listener()->has_node(nullptr));
RCLCPP_EXPECT_THROW_EQ(
graph_listener()->add_node(nullptr),
std::invalid_argument("node is nullptr"));
RCLCPP_EXPECT_THROW_EQ(
graph_listener()->remove_node(nullptr),
std::invalid_argument("node is nullptr"));
// Already added
graph_listener()->add_node(node_graph());
EXPECT_TRUE(graph_listener()->has_node(node_graph()));
RCLCPP_EXPECT_THROW_EQ(
graph_listener()->add_node(node_graph()),
std::runtime_error("node already added"));
// Remove node not found
graph_listener()->remove_node(node_graph());
EXPECT_FALSE(graph_listener()->has_node(node_graph()));
RCLCPP_EXPECT_THROW_EQ(
graph_listener()->remove_node(node_graph()),
std::runtime_error("node not found"));
// Add and remove after shutdown
EXPECT_NO_THROW(graph_listener()->shutdown());
RCLCPP_EXPECT_THROW_EQ(
graph_listener()->add_node(node_graph()),
std::runtime_error(shutdown_error_str));
// Remove works the same
RCLCPP_EXPECT_THROW_EQ(
graph_listener()->remove_node(node_graph()),
std::runtime_error("node not found"));
}
/* Shutdown errors */
TEST_F(TestGraphListener, test_graph_listener_shutdown_wait_fini_error_nothrow) {
auto global_context = rclcpp::contexts::get_global_default_context();
auto graph_listener_test =
std::make_shared<TestGraphListenerProtectedMethods>(global_context);
graph_listener_test->start_if_not_started();
{
auto mock_wait_set_fini = mocking_utils::patch_and_return(
"lib:rclcpp", rcl_wait_set_fini, RCL_RET_ERROR);
// Exception is logged when using nothrow_t
EXPECT_NO_THROW(graph_listener_test->shutdown(std::nothrow_t()));
}
graph_listener_test->mock_cleanup_wait_set();
}
TEST_F(TestGraphListener, test_graph_listener_shutdown_wait_fini_error_throw) {
auto global_context = rclcpp::contexts::get_global_default_context();
auto graph_listener_test =
std::make_shared<TestGraphListenerProtectedMethods>(global_context);
graph_listener()->start_if_not_started();
{
auto mock_wait_set_fini = mocking_utils::patch_and_return(
"lib:rclcpp", rcl_wait_set_fini, RCL_RET_ERROR);
RCLCPP_EXPECT_THROW_EQ(
graph_listener()->shutdown(),
std::runtime_error("failed to finalize wait set: error not set"));
}
graph_listener_test->mock_cleanup_wait_set();
}
TEST_F(TestGraphListener, test_graph_listener_shutdown_guard_fini_error_throw) {
auto global_context = rclcpp::contexts::get_global_default_context();
auto graph_listener_test =
std::make_shared<TestGraphListenerProtectedMethods>(global_context);
graph_listener_test->start_if_not_started();
auto mock_wait_set_fini = mocking_utils::patch_and_return(
"lib:rclcpp", rcl_guard_condition_fini, RCL_RET_ERROR);
RCLCPP_EXPECT_THROW_EQ(
graph_listener_test->shutdown(),
std::runtime_error("failed to finalize interrupt guard condition: error not set"));
graph_listener_test->mock_cleanup_wait_set();
}