Skip to content

Commit 25f57b5

Browse files
authored
Split test_subscriber into multiple compilation units. (#500)
* Split test_subscriber and test_publisher_subscriber into multiple compilation units. This should avoid an OOM error when compiling on the buildfarm. Signed-off-by: Chris Lalancette <[email protected]>
1 parent e29516d commit 25f57b5

10 files changed

+483
-147
lines changed

test_communication/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,18 @@ if(BUILD_TESTING)
416416
endforeach()
417417
endmacro()
418418

419+
add_library(subscribe_types STATIC
420+
"test/subscribe_array_types.cpp"
421+
"test/subscribe_basic_types.cpp"
422+
"test/subscribe_string_types.cpp")
423+
ament_target_dependencies(subscribe_types
424+
"rclcpp"
425+
"test_msgs")
426+
419427
# publisher combined with a subscriber
420428
custom_executable(test_publisher_subscriber_cpp
421429
"test/test_publisher_subscriber.cpp")
430+
target_link_libraries(test_publisher_subscriber_cpp subscribe_types)
422431
# subcription valid data
423432
custom_executable(test_subscription_valid_data_cpp
424433
"test/test_subscription_valid_data.cpp")
@@ -427,6 +436,7 @@ if(BUILD_TESTING)
427436
"test/test_publisher.cpp")
428437
custom_executable(test_subscriber_cpp
429438
"test/test_subscriber.cpp")
439+
target_link_libraries(test_subscriber_cpp subscribe_types)
430440
# executables requester / replier
431441
custom_executable(test_requester_cpp
432442
"test/test_requester.cpp")
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright 2015 Open Source Robotics Foundation, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <string>
16+
#include <vector>
17+
18+
#include "rclcpp/rclcpp.hpp"
19+
#include "test_msgs/msg/arrays.hpp"
20+
#include "test_msgs/msg/unbounded_sequences.hpp"
21+
#include "test_msgs/msg/bounded_plain_sequences.hpp"
22+
#include "test_msgs/msg/bounded_sequences.hpp"
23+
#include "test_msgs/msg/multi_nested.hpp"
24+
#include "test_msgs/msg/nested.hpp"
25+
26+
#include "subscribe_array_types.hpp"
27+
#include "subscribe_helper.hpp"
28+
29+
rclcpp::SubscriptionBase::SharedPtr subscribe_arrays(
30+
rclcpp::Node::SharedPtr node,
31+
const std::string & message_type,
32+
const std::vector<test_msgs::msg::Arrays::SharedPtr> & expected_messages,
33+
std::vector<bool> & received_messages)
34+
{
35+
return subscribe<test_msgs::msg::Arrays>(
36+
node, message_type, expected_messages, received_messages);
37+
}
38+
39+
rclcpp::SubscriptionBase::SharedPtr subscribe_unbounded_sequences(
40+
rclcpp::Node::SharedPtr node,
41+
const std::string & message_type,
42+
const std::vector<test_msgs::msg::UnboundedSequences::SharedPtr> & expected_messages,
43+
std::vector<bool> & received_messages)
44+
{
45+
return subscribe<test_msgs::msg::UnboundedSequences>(
46+
node, message_type, expected_messages, received_messages);
47+
}
48+
49+
rclcpp::SubscriptionBase::SharedPtr subscribe_bounded_plain_sequences(
50+
rclcpp::Node::SharedPtr node,
51+
const std::string & message_type,
52+
const std::vector<test_msgs::msg::BoundedPlainSequences::SharedPtr> & expected_messages,
53+
std::vector<bool> & received_messages)
54+
{
55+
return subscribe<test_msgs::msg::BoundedPlainSequences>(
56+
node, message_type, expected_messages, received_messages);
57+
}
58+
59+
rclcpp::SubscriptionBase::SharedPtr subscribe_bounded_sequences(
60+
rclcpp::Node::SharedPtr node,
61+
const std::string & message_type,
62+
const std::vector<test_msgs::msg::BoundedSequences::SharedPtr> & expected_messages,
63+
std::vector<bool> & received_messages)
64+
{
65+
return subscribe<test_msgs::msg::BoundedSequences>(
66+
node, message_type, expected_messages, received_messages);
67+
}
68+
69+
rclcpp::SubscriptionBase::SharedPtr subscribe_multi_nested(
70+
rclcpp::Node::SharedPtr node,
71+
const std::string & message_type,
72+
const std::vector<test_msgs::msg::MultiNested::SharedPtr> & expected_messages,
73+
std::vector<bool> & received_messages)
74+
{
75+
return subscribe<test_msgs::msg::MultiNested>(
76+
node, message_type, expected_messages, received_messages);
77+
}
78+
79+
rclcpp::SubscriptionBase::SharedPtr subscribe_nested(
80+
rclcpp::Node::SharedPtr node,
81+
const std::string & message_type,
82+
const std::vector<test_msgs::msg::Nested::SharedPtr> & expected_messages,
83+
std::vector<bool> & received_messages)
84+
{
85+
return subscribe<test_msgs::msg::Nested>(
86+
node, message_type, expected_messages, received_messages);
87+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2015 Open Source Robotics Foundation, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef SUBSCRIBE_ARRAY_TYPES_HPP_
16+
#define SUBSCRIBE_ARRAY_TYPES_HPP_
17+
18+
#include <string>
19+
#include <vector>
20+
21+
#include "rclcpp/rclcpp.hpp"
22+
#include "test_msgs/msg/arrays.hpp"
23+
#include "test_msgs/msg/unbounded_sequences.hpp"
24+
#include "test_msgs/msg/bounded_plain_sequences.hpp"
25+
#include "test_msgs/msg/bounded_sequences.hpp"
26+
#include "test_msgs/msg/multi_nested.hpp"
27+
#include "test_msgs/msg/nested.hpp"
28+
29+
rclcpp::SubscriptionBase::SharedPtr subscribe_arrays(
30+
rclcpp::Node::SharedPtr node,
31+
const std::string & message_type,
32+
const std::vector<test_msgs::msg::Arrays::SharedPtr> & expected_messages,
33+
std::vector<bool> & received_messages);
34+
35+
rclcpp::SubscriptionBase::SharedPtr subscribe_unbounded_sequences(
36+
rclcpp::Node::SharedPtr node,
37+
const std::string & message_type,
38+
const std::vector<test_msgs::msg::UnboundedSequences::SharedPtr> & expected_messages,
39+
std::vector<bool> & received_messages);
40+
41+
rclcpp::SubscriptionBase::SharedPtr subscribe_bounded_plain_sequences(
42+
rclcpp::Node::SharedPtr node,
43+
const std::string & message_type,
44+
const std::vector<test_msgs::msg::BoundedPlainSequences::SharedPtr> & expected_messages,
45+
std::vector<bool> & received_messages);
46+
47+
rclcpp::SubscriptionBase::SharedPtr subscribe_bounded_sequences(
48+
rclcpp::Node::SharedPtr node,
49+
const std::string & message_type,
50+
const std::vector<test_msgs::msg::BoundedSequences::SharedPtr> & expected_messages,
51+
std::vector<bool> & received_messages);
52+
53+
rclcpp::SubscriptionBase::SharedPtr subscribe_multi_nested(
54+
rclcpp::Node::SharedPtr node,
55+
const std::string & message_type,
56+
const std::vector<test_msgs::msg::MultiNested::SharedPtr> & expected_messages,
57+
std::vector<bool> & received_messages);
58+
59+
rclcpp::SubscriptionBase::SharedPtr subscribe_nested(
60+
rclcpp::Node::SharedPtr node,
61+
const std::string & message_type,
62+
const std::vector<test_msgs::msg::Nested::SharedPtr> & expected_messages,
63+
std::vector<bool> & received_messages);
64+
65+
#endif // SUBSCRIBE_ARRAY_TYPES_HPP_
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2015 Open Source Robotics Foundation, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <string>
16+
#include <vector>
17+
18+
#include "rclcpp/rclcpp.hpp"
19+
#include "test_msgs/msg/empty.hpp"
20+
#include "test_msgs/msg/basic_types.hpp"
21+
#include "test_msgs/msg/builtins.hpp"
22+
#include "test_msgs/msg/constants.hpp"
23+
#include "test_msgs/msg/defaults.hpp"
24+
25+
#include "subscribe_basic_types.hpp"
26+
#include "subscribe_helper.hpp"
27+
28+
rclcpp::SubscriptionBase::SharedPtr subscribe_empty(
29+
rclcpp::Node::SharedPtr node,
30+
const std::string & message_type,
31+
const std::vector<test_msgs::msg::Empty::SharedPtr> & messages_expected,
32+
std::vector<bool> & received_messages)
33+
{
34+
return subscribe<test_msgs::msg::Empty>(node, message_type, messages_expected, received_messages);
35+
}
36+
37+
rclcpp::SubscriptionBase::SharedPtr subscribe_basic_types(
38+
rclcpp::Node::SharedPtr node,
39+
const std::string & message_type,
40+
const std::vector<test_msgs::msg::BasicTypes::SharedPtr> & messages_expected,
41+
std::vector<bool> & received_messages)
42+
{
43+
return subscribe<test_msgs::msg::BasicTypes>(
44+
node, message_type, messages_expected, received_messages);
45+
}
46+
47+
rclcpp::SubscriptionBase::SharedPtr subscribe_builtins(
48+
rclcpp::Node::SharedPtr node,
49+
const std::string & message_type,
50+
const std::vector<test_msgs::msg::Builtins::SharedPtr> & messages_expected,
51+
std::vector<bool> & received_messages)
52+
{
53+
return subscribe<test_msgs::msg::Builtins>(
54+
node, message_type, messages_expected, received_messages);
55+
}
56+
57+
rclcpp::SubscriptionBase::SharedPtr subscribe_constants(
58+
rclcpp::Node::SharedPtr node,
59+
const std::string & message_type,
60+
const std::vector<test_msgs::msg::Constants::SharedPtr> & messages_expected,
61+
std::vector<bool> & received_messages)
62+
{
63+
return subscribe<test_msgs::msg::Constants>(
64+
node, message_type, messages_expected, received_messages);
65+
}
66+
67+
rclcpp::SubscriptionBase::SharedPtr subscribe_defaults(
68+
rclcpp::Node::SharedPtr node,
69+
const std::string & message_type,
70+
const std::vector<test_msgs::msg::Defaults::SharedPtr> & messages_expected,
71+
std::vector<bool> & received_messages)
72+
{
73+
return subscribe<test_msgs::msg::Defaults>(
74+
node, message_type, messages_expected, received_messages);
75+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2015 Open Source Robotics Foundation, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef SUBSCRIBE_BASIC_TYPES_HPP_
16+
#define SUBSCRIBE_BASIC_TYPES_HPP_
17+
18+
#include <string>
19+
#include <vector>
20+
21+
#include "rclcpp/rclcpp.hpp"
22+
#include "test_msgs/msg/empty.hpp"
23+
#include "test_msgs/msg/basic_types.hpp"
24+
#include "test_msgs/msg/builtins.hpp"
25+
#include "test_msgs/msg/constants.hpp"
26+
#include "test_msgs/msg/defaults.hpp"
27+
28+
rclcpp::SubscriptionBase::SharedPtr subscribe_empty(
29+
rclcpp::Node::SharedPtr node,
30+
const std::string & message_type,
31+
const std::vector<test_msgs::msg::Empty::SharedPtr> & messages_expected,
32+
std::vector<bool> & received_messages);
33+
34+
rclcpp::SubscriptionBase::SharedPtr subscribe_basic_types(
35+
rclcpp::Node::SharedPtr node,
36+
const std::string & message_type,
37+
const std::vector<test_msgs::msg::BasicTypes::SharedPtr> & messages_expected,
38+
std::vector<bool> & received_messages);
39+
40+
rclcpp::SubscriptionBase::SharedPtr subscribe_builtins(
41+
rclcpp::Node::SharedPtr node,
42+
const std::string & message_type,
43+
const std::vector<test_msgs::msg::Builtins::SharedPtr> & messages_expected,
44+
std::vector<bool> & received_messages);
45+
46+
rclcpp::SubscriptionBase::SharedPtr subscribe_constants(
47+
rclcpp::Node::SharedPtr node,
48+
const std::string & message_type,
49+
const std::vector<test_msgs::msg::Constants::SharedPtr> & messages_expected,
50+
std::vector<bool> & received_messages);
51+
52+
rclcpp::SubscriptionBase::SharedPtr subscribe_defaults(
53+
rclcpp::Node::SharedPtr node,
54+
const std::string & message_type,
55+
const std::vector<test_msgs::msg::Defaults::SharedPtr> & messages_expected,
56+
std::vector<bool> & received_messages);
57+
58+
#endif // SUBSCRIBE_BASIC_TYPES_HPP_
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2015 Open Source Robotics Foundation, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef SUBSCRIBE_HELPER_HPP_
16+
#define SUBSCRIBE_HELPER_HPP_
17+
18+
#include <cstdio>
19+
#include <stdexcept>
20+
#include <string>
21+
#include <vector>
22+
23+
#include "rclcpp/rclcpp.hpp"
24+
25+
template<typename T>
26+
rclcpp::SubscriptionBase::SharedPtr subscribe(
27+
rclcpp::Node::SharedPtr node,
28+
const std::string & message_type,
29+
const std::vector<typename T::SharedPtr> & expected_messages,
30+
std::vector<bool> & received_messages)
31+
{
32+
received_messages.assign(expected_messages.size(), false);
33+
34+
auto callback =
35+
[&expected_messages, &received_messages](
36+
const typename T::ConstSharedPtr received_message
37+
) -> void
38+
{
39+
// find received message in vector of expected messages
40+
auto received = received_messages.begin();
41+
bool known_message = false;
42+
size_t index = 0;
43+
for (auto expected_message : expected_messages) {
44+
if (*received_message == *expected_message) {
45+
*received = true;
46+
printf("received message #%zu of %zu\n", index + 1, expected_messages.size());
47+
known_message = true;
48+
break;
49+
}
50+
++received;
51+
++index;
52+
}
53+
if (!known_message) {
54+
throw std::runtime_error("received message does not match any expected message");
55+
}
56+
57+
// shutdown node when all expected messages have been received
58+
for (auto received_msg : received_messages) {
59+
if (!received_msg) {
60+
return;
61+
}
62+
}
63+
rclcpp::shutdown();
64+
};
65+
66+
auto qos = rclcpp::QoS(rclcpp::KeepLast(expected_messages.size()));
67+
68+
return
69+
node->create_subscription<T>(std::string("test/message/") + message_type, qos, callback);
70+
}
71+
72+
#endif // SUBSCRIBE_HELPER_HPP_

0 commit comments

Comments
 (0)