Skip to content

Commit f0af36a

Browse files
committed
Not working! tried waiting for urdf to arrive before starting controllers in test
In tests we have to be sure, that our urdf arrived before continuing with the actuell tests otherwise they will naturally fail...
1 parent e92707d commit f0af36a

File tree

3 files changed

+102
-7
lines changed

3 files changed

+102
-7
lines changed

controller_manager/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,16 @@ if(BUILD_TESTING)
144144
ament_target_dependencies(test_controller_manager_srvs
145145
controller_manager_msgs
146146
)
147+
ament_add_gmock(test_controller_manager_urdf_passing
148+
test/test_controller_manager_urdf_passing.cpp
149+
)
150+
target_link_libraries(test_controller_manager_urdf_passing
151+
controller_manager
152+
ros2_control_test_assets::ros2_control_test_assets
153+
)
154+
ament_target_dependencies(test_controller_manager_urdf_passing
155+
controller_manager_msgs
156+
)
147157

148158
add_library(test_controller_with_interfaces SHARED
149159
test/test_controller_with_interfaces/test_controller_with_interfaces.cpp

controller_manager/test/controller_manager_test_common.hpp

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
#include <memory>
2323
#include <string>
2424
#include <thread>
25+
#include <utility>
2526
#include <vector>
2627

2728
#include "controller_interface/controller_interface.hpp"
2829

2930
#include "controller_manager/controller_manager.hpp"
31+
#include "controller_manager_msgs/srv/list_hardware_interfaces.hpp"
3032
#include "controller_manager_msgs/srv/switch_controller.hpp"
3133

3234
#include "rclcpp/rclcpp.hpp"
@@ -63,14 +65,14 @@ template <typename CtrlMgr>
6365
class ControllerManagerFixture : public ::testing::Test
6466
{
6567
public:
66-
//TODO parameter hinzufügen, welche hw erwartet wird
68+
// TODO(Manuel): Maybe add parameter of which hardware is to be expected
6769
explicit ControllerManagerFixture(
6870
const std::string & robot_description = ros2_control_test_assets::minimal_robot_urdf,
6971
const std::string ns = "/", const bool & pass_urdf_as_parameter = false)
7072
: robot_description_(robot_description), ns_(ns), pass_urdf_as_parameter_(pass_urdf_as_parameter)
7173
{
7274
executor_ = std::make_shared<rclcpp::executors::SingleThreadedExecutor>();
73-
// We want ot be able to create a ResourceManager where no urdf file has been passed to
75+
// We want to be able to create a ResourceManager where no urdf file has been passed to
7476
if (robot_description_.empty())
7577
{
7678
cm_ = std::make_shared<CtrlMgr>(
@@ -88,18 +90,37 @@ class ControllerManagerFixture : public ::testing::Test
8890
}
8991
else
9092
{
93+
// First wie create a node and a publisher for publishing the passed robot description file
9194
urdf_publisher_node_ = std::make_shared<rclcpp::Node>("robot_description_publisher", ns_);
9295
description_pub_ = urdf_publisher_node_->create_publisher<std_msgs::msg::String>(
9396
"robot_description", rclcpp::QoS(1).transient_local());
9497
executor_->add_node(urdf_publisher_node_);
9598
publish_robot_description_file(robot_description_);
96-
99+
// Then we create controller manager which subscribes to topic and receive
100+
// published robot description file. Publishing is transient_local so starting cm
101+
// later should not pose problem and is closer to real world applications
97102
cm_ = std::make_shared<CtrlMgr>(
98103
std::make_unique<hardware_interface::ResourceManager>(), executor_, TEST_CM_NAME);
99-
100-
//TODO warten bis hw da ist und initialisiert
101-
// von urdf wissen wir was da sein soll
102-
// list hw
104+
executor_->add_node(cm_);
105+
// Now we have to wait for cm to process callback and initialize everything.
106+
// We have to wait here, otherwise controllers can not be initialized since
107+
// no hardware has been received.
108+
service_caller_node_ = std::make_shared<rclcpp::Node>("service_caller_node", ns_);
109+
executor_->add_node(service_caller_node_);
110+
auto client =
111+
service_caller_node_->create_client<controller_manager_msgs::srv::ListHardwareInterfaces>(
112+
"get_hw_interfaces");
113+
auto request =
114+
std::make_shared<controller_manager_msgs::srv::ListHardwareInterfaces::Request>();
115+
EXPECT_TRUE(client->wait_for_service(std::chrono::milliseconds(500)));
116+
auto future = client->async_send_request(request);
117+
EXPECT_EQ(
118+
executor_->spin_until_future_complete(future, std::chrono::milliseconds(1000)),
119+
rclcpp::FutureReturnCode::SUCCESS);
120+
auto res = future.get();
121+
auto command_interfaces = res->command_interfaces;
122+
auto state_interfaces = res->state_interfaces;
123+
// check for command-/stateinterfaces but spin_until_future_complete times out...
103124
}
104125
}
105126
}
@@ -168,6 +189,7 @@ class ControllerManagerFixture : public ::testing::Test
168189
const bool pass_urdf_as_parameter_;
169190
std::shared_ptr<rclcpp::Node> urdf_publisher_node_;
170191
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr description_pub_;
192+
std::shared_ptr<rclcpp::Node> service_caller_node_;
171193
};
172194

173195
class TestControllerManagerSrvs
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2020 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 <gtest/gtest.h>
16+
#include <memory>
17+
#include <string>
18+
#include <utility>
19+
#include <vector>
20+
21+
#include "controller_manager/controller_manager.hpp"
22+
#include "controller_manager_test_common.hpp"
23+
24+
class TestControllerManagerWithTestableCM;
25+
26+
class TestableControllerManager : public controller_manager::ControllerManager
27+
{
28+
friend TestControllerManagerWithTestableCM;
29+
30+
FRIEND_TEST(TestControllerManagerWithTestableCM, callback_gets_passed);
31+
FRIEND_TEST(TestControllerManagerWithTestableCM, initial_failing);
32+
33+
public:
34+
TestableControllerManager(
35+
std::unique_ptr<hardware_interface::ResourceManager> resource_manager,
36+
std::shared_ptr<rclcpp::Executor> executor,
37+
const std::string & manager_node_name = "controller_manager",
38+
const std::string & namespace_ = "")
39+
: controller_manager::ControllerManager(
40+
std::move(resource_manager), executor, manager_node_name, namespace_)
41+
{
42+
}
43+
};
44+
45+
class TestControllerManagerWithTestableCM
46+
: public ControllerManagerFixture<TestableControllerManager>,
47+
public testing::WithParamInterface<Strictness>
48+
{
49+
};
50+
51+
// only exemplary to test if working not a useful test yet
52+
TEST_P(TestControllerManagerWithTestableCM, callback_gets_passed)
53+
{
54+
ASSERT_FALSE(cm_->resource_manager_->load_urdf_called());
55+
}
56+
57+
TEST_P(TestControllerManagerWithTestableCM, initial_failing)
58+
{
59+
ASSERT_TRUE(cm_->resource_manager_->load_urdf_called());
60+
}
61+
62+
INSTANTIATE_TEST_SUITE_P(
63+
test_best_effort, TestControllerManagerWithTestableCM, testing::Values(best_effort));

0 commit comments

Comments
 (0)