3
3
#include < gmock/gmock.h>
4
4
#pragma clang diagnostic pop
5
5
6
+ using namespace std ::chrono_literals;
7
+
8
+ #include < rclcpp/rclcpp.hpp>
9
+
6
10
#include " dynamic_graph_bridge/ros.hpp"
7
11
#include " dynamic_graph_bridge/sot_loader.hh"
8
12
#include " dynamic_graph_bridge_msgs/msg/vector.hpp"
9
- #include " test_common .hpp"
13
+ #include " dynamic_graph_bridge/ros_python_interpreter_server .hpp"
10
14
11
15
namespace test_sot_loader {
12
16
int l_argc;
@@ -21,29 +25,131 @@ class MockSotLoaderTest : public ::testing::Test {
21
25
public:
22
26
rclcpp::Subscription<dynamic_graph_bridge_msgs::msg::Vector>::SharedPtr
23
27
subscription_;
28
+ bool service_done_;
29
+ std::string node_name_;
30
+ std::string response_dg_python_result_;
24
31
25
- ~MockSotLoader () {}
32
+ MockSotLoader ():
33
+ node_name_ (" unittest_sot_loader" ) {}
26
34
27
- void topic_callback (const dynamic_graph_bridge_msgs::msg::Vector::SharedPtr msg) const {
28
- auto lsize=msg->data .size ();
35
+ ~MockSotLoader () {
36
+ service_done_ = false ;
37
+ response_dg_python_result_ = " " ;
29
38
}
30
39
40
+ void topic_callback (const dynamic_graph_bridge_msgs::msg::Vector::SharedPtr msg) const {
41
+ bool ok=true ;
42
+ for (std::vector<double >::size_type idx=0 ;idx<msg->data .size ();idx++) {
43
+ if (msg->data [idx]!=0.0 )
44
+ ok=false ;
45
+ }
46
+ ASSERT_TRUE (ok);
47
+ }
48
+ // This method is to listen to publication from the control signal (dg world)
49
+ // to the control_ros topic (ros world).
31
50
void subscribe_to_a_topic () {
32
- subscription_ = create_subscription<dynamic_graph_bridge_msgs::msg::Vector>(
33
- " control_ros" , 1 , std::bind (&MockSotLoader::topic_callback, this ,
34
- std::placeholders::_1));
51
+ subscription_ = dynamic_graph_bridge::get_ros_node (node_name_)->
52
+ create_subscription<dynamic_graph_bridge_msgs::msg::Vector>(
53
+ " control_ros" , 1 , std::bind (&MockSotLoader::topic_callback, this ,
54
+ std::placeholders::_1));
55
+ }
56
+
57
+ // This method is to receive the result of client request to run_python_file
58
+ //
59
+ void response_dg_python (
60
+ const rclcpp::Client<dynamic_graph_bridge_msgs::srv::RunPythonFile>::SharedFuture
61
+ future) {
62
+ RCLCPP_INFO (rclcpp::get_logger (" dynamic_graph_bridge" ), " response_dg_python:" );
63
+ auto status = future.wait_for (500ms);
64
+ if (status == std::future_status::ready) {
65
+ // uncomment below line if using Empty() message
66
+ // RCLCPP_INFO(this->get_logger(), "Result: success");
67
+ // comment below line if using Empty() message
68
+ RCLCPP_INFO (rclcpp::get_logger (" dynamic_graph_bridge" ),
69
+ " response_dg_python - Result: %s" ,
70
+ future.get ()->result .c_str ());
71
+ service_done_ = true ;
72
+ response_dg_python_result_ = future.get ()->result ;
73
+ } else {
74
+ RCLCPP_INFO (rclcpp::get_logger (" dynmic_graph_bridge" ),
75
+ " response_dg_python - Service In-Progress..." );
76
+ }
77
+ }
78
+
79
+ void start_run_python_script_ros_service (const std::string& file_name) {
80
+ // Service name.
81
+ std::string service_name = " /dynamic_graph_bridge/run_python_file" ;
82
+ // Create a client from a temporary node.
83
+ auto client = dynamic_graph_bridge::get_ros_node (node_name_)->
84
+ create_client<dynamic_graph_bridge_msgs::srv::RunPythonFile>(
85
+ service_name);
86
+ ASSERT_TRUE (client->wait_for_service (1s));
87
+
88
+ // Fill the command message.
89
+ dynamic_graph_bridge_msgs::srv::RunPythonFile::Request::SharedPtr request =
90
+ std::make_shared<
91
+ dynamic_graph_bridge_msgs::srv::RunPythonFile::Request>();
92
+ request->input = file_name;
93
+ // Call the service.
94
+ auto response =
95
+ client->async_send_request (request,
96
+ std::bind (&MockSotLoader::response_dg_python,
97
+ this ,
98
+ std::placeholders::_1));
99
+ RCLCPP_INFO (rclcpp::get_logger (" dynamic_graph_bridge" ),
100
+ " Send request to service %s - Waiting" ,
101
+ service_name.c_str ());
102
+ response.wait ();
103
+ RCLCPP_INFO (rclcpp::get_logger (" dynamic_graph_bridge" ),
104
+ " Get the answer" );
105
+
106
+ }
107
+
108
+ void display_services (
109
+ std::map<std::string, std::vector<std::string> >& list_service_and_types) {
110
+ for (std::map<std::string, std::vector<std::string> >::const_iterator
111
+ const_it = list_service_and_types.begin ();
112
+ const_it != list_service_and_types.end (); ++const_it) {
113
+ std::cerr << const_it->first << " \t\t\t " ;
114
+ for (unsigned i = 0 ; i < const_it->second .size (); ++i) {
115
+ std::cerr << std::right;
116
+ std::cerr << const_it->second [i] << std::endl;
117
+ }
118
+ }
119
+ }
120
+
121
+ void local_ros_spin () {
122
+ RCLCPP_INFO (rclcpp::get_logger (" dynamic_graph_bridge" ),
123
+ " local_ros_spin" );
124
+ dynamic_graph_bridge::ros_spin ();
35
125
}
36
126
37
127
void generateEvents () {
38
128
usleep (20000 );
39
- std::cerr << " Start Dynamic Graph " << std::endl;
129
+ RCLCPP_INFO (rclcpp::get_logger (" dynamic_graph_bridge" ),
130
+ " Start Dynamic Graph " );
40
131
dynamic_graph_stopped_ = false ;
41
132
42
- usleep (20000 );
43
- std::cerr << " Stop Dynamic Graph " << std::endl;
133
+ std::string file_name =
134
+ TEST_CONFIG_PATH + std::string (" simple_ros_publish.py" );
135
+ std::string result = " " ;
136
+ std::string standard_output = " " ;
137
+ std::string standard_error = " " ;
138
+ start_run_python_script_ros_service (file_name);
139
+
140
+
141
+ usleep (100000 );
142
+ std::map<std::string, std::vector<std::string>> list_service_and_types;
143
+ dynamic_graph_bridge::RosNodePtr ros_node = dynamic_graph_bridge::get_ros_node (node_name_);
144
+ bool simple_service_exists = false ;
145
+
146
+ usleep (30720000 ); // Wait 30s
147
+ RCLCPP_INFO (rclcpp::get_logger (" dynamic_graph_bridge" ),
148
+ " Stop Dynamic Graph " );
44
149
dynamic_graph_stopped_ = true ;
45
150
}
46
151
152
+
47
153
void testLoadController () {
48
154
// Set input call
49
155
int argc = 2 ;
@@ -79,16 +185,11 @@ class MockSotLoaderTest : public ::testing::Test {
79
185
// Start the thread generating events.
80
186
std::thread local_events (&MockSotLoader::generateEvents, this );
81
187
82
- // Create and call the clients.
83
- std::string file_name =
84
- TEST_CONFIG_PATH + std::string (" simple_ros_publish.py" );
85
- std::string result = " " ;
86
- std::string standard_output = " " ;
87
- std::string standard_error = " " ;
88
- // start_run_python_script_ros_service(file_name, result);
188
+ // Start the thread for ros events.
189
+ std::thread local_ros_spin_thread (&MockSotLoader::local_ros_spin, this );
89
190
90
191
// Wait for each threads.
91
- SotLoader::lthread_join (); // Wait 100 ms
192
+ SotLoader::lthread_join ();
92
193
local_events.join ();
93
194
}
94
195
};
@@ -111,11 +212,13 @@ class MockSotLoaderTest : public ::testing::Test {
111
212
void SetUp () {
112
213
mockSotLoader_ptr_ = new MockSotLoader ();
113
214
mockSotLoader_ptr_->initialize ();
215
+ dynamic_graph_bridge::ros_spin_non_blocking ();
114
216
}
115
217
116
218
void TearDown () {
117
219
delete mockSotLoader_ptr_;
118
220
mockSotLoader_ptr_ = nullptr ;
221
+ dynamic_graph_bridge::ros_clean ();
119
222
}
120
223
};
121
224
0 commit comments