3
3
#include < gmock/gmock.h>
4
4
#pragma clang diagnostic pop
5
5
6
+ #include < chrono>
7
+
6
8
using namespace std ::chrono_literals;
7
9
8
10
#include < rclcpp/rclcpp.hpp>
@@ -27,15 +29,17 @@ class MockSotLoaderTest : public ::testing::Test {
27
29
subscription_;
28
30
bool service_done_;
29
31
std::string node_name_;
30
- std::string response_dg_python_result_;
31
32
32
- MockSotLoader () : node_name_( " unittest_sot_loader " ) {}
33
+ rclcpp::CallbackGroup::SharedPtr reentrant_cb_group_;
33
34
34
- ~MockSotLoader () {
35
- service_done_ = false ;
36
- response_dg_python_result_ = " " ;
35
+ MockSotLoader () : node_name_(" unittest_sot_loader" ) {
36
+ reentrant_cb_group_ =
37
+ dynamic_graph_bridge::get_ros_node (node_name_)
38
+ ->create_callback_group (rclcpp::CallbackGroupType::Reentrant);
37
39
}
38
40
41
+ ~MockSotLoader () { service_done_ = false ; }
42
+
39
43
void topic_callback (
40
44
const dynamic_graph_bridge_msgs::msg::Vector::SharedPtr msg) const {
41
45
bool ok = true ;
@@ -48,45 +52,27 @@ class MockSotLoaderTest : public ::testing::Test {
48
52
// This method is to listen to publication from the control signal (dg
49
53
// world) to the control_ros topic (ros world).
50
54
void subscribe_to_a_topic () {
55
+ rclcpp::SubscriptionOptions subscription_options;
56
+ subscription_options.callback_group = reentrant_cb_group_;
51
57
subscription_ =
52
58
dynamic_graph_bridge::get_ros_node (node_name_)
53
59
->create_subscription <dynamic_graph_bridge_msgs::msg::Vector>(
54
60
" control_ros" , 1 ,
55
61
std::bind (&MockSotLoader::topic_callback, this ,
56
- std::placeholders::_1));
57
- }
58
-
59
- // This method is to receive the result of client request to run_python_file
60
- //
61
- void response_dg_python (
62
- const rclcpp::Client<dynamic_graph_bridge_msgs::srv::RunPythonFile>::
63
- SharedFuture future) {
64
- RCLCPP_INFO (rclcpp::get_logger (" dynamic_graph_bridge" ),
65
- " response_dg_python:" );
66
- auto status = future.wait_for (500ms);
67
- if (status == std::future_status::ready) {
68
- // uncomment below line if using Empty() message
69
- // RCLCPP_INFO(this->get_logger(), "Result: success");
70
- // comment below line if using Empty() message
71
- RCLCPP_INFO (rclcpp::get_logger (" dynamic_graph_bridge" ),
72
- " response_dg_python - Result: %s" ,
73
- future.get ()->result .c_str ());
74
- service_done_ = true ;
75
- response_dg_python_result_ = future.get ()->result ;
76
- } else {
77
- RCLCPP_INFO (rclcpp::get_logger (" dynmic_graph_bridge" ),
78
- " response_dg_python - Service In-Progress..." );
79
- }
62
+ std::placeholders::_1),
63
+ subscription_options);
80
64
}
81
65
82
66
void start_run_python_script_ros_service (const std::string& file_name) {
83
67
// Service name.
84
68
std::string service_name = " /dynamic_graph_bridge/run_python_file" ;
85
69
// Create a client from a temporary node.
70
+ rclcpp::CallbackGroup::SharedPtr local_cb_group =
71
+ dynamic_graph_bridge::get_callback_group (" python_interpreter" );
86
72
auto client =
87
- dynamic_graph_bridge::get_ros_node (node_name_ )
73
+ dynamic_graph_bridge::get_ros_node (" python_interpreter " )
88
74
->create_client <dynamic_graph_bridge_msgs::srv::RunPythonFile>(
89
- service_name);
75
+ service_name, rclcpp::ServicesQoS (), local_cb_group );
90
76
ASSERT_TRUE (client->wait_for_service (1s));
91
77
92
78
// Fill the command message.
@@ -95,13 +81,13 @@ class MockSotLoaderTest : public ::testing::Test {
95
81
dynamic_graph_bridge_msgs::srv::RunPythonFile::Request>();
96
82
request->input = file_name;
97
83
// Call the service.
98
- auto response = client->async_send_request (
99
- request, std::bind (&MockSotLoader::response_dg_python, this ,
100
- std::placeholders::_1));
84
+ auto response = client->async_send_request (request);
101
85
RCLCPP_INFO (rclcpp::get_logger (" dynamic_graph_bridge" ),
102
86
" Send request to service %s - Waiting" , service_name.c_str ());
103
87
response.wait ();
104
- RCLCPP_INFO (rclcpp::get_logger (" dynamic_graph_bridge" ), " Get the answer" );
88
+ RCLCPP_INFO (rclcpp::get_logger (" dynamic_graph_bridge" ),
89
+ " start_run_python_script_ros_service - Result: %s" ,
90
+ response.get ()->result .c_str ());
105
91
}
106
92
107
93
void display_services (std::map<std::string, std::vector<std::string>>&
@@ -178,7 +164,6 @@ class MockSotLoaderTest : public ::testing::Test {
178
164
179
165
// Start the control loop thread.
180
166
startControlLoop ();
181
-
182
167
// Start the thread generating events.
183
168
std::thread local_events (&MockSotLoader::generateEvents, this );
184
169
0 commit comments