-
Notifications
You must be signed in to change notification settings - Fork 372
Add tests for ControllerInterface class. #662
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
destogl
merged 21 commits into
ros-controls:master
from
destogl:controller-interface-test-pr
Apr 6, 2022
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
74a9c29
Add lifecycle nodes
2e19977
pre-commit fixes
afb4daf
fix state bug
32bdaac
Add configure and cleanup methods
964e2a7
address names
0b265dd
Consts are back!
5fee7fc
Minor formatting
c7231ba
Fixes of controller tests.
destogl c6b567c
Add custom 'configure' to controller interface to get 'update_rate' p…
destogl d3ec3ef
Revert controller reload (cleanup calls test).
destogl ef245ab
Cleanup is actually not called if in unconfigured state.
destogl bb6d25b
Can not call 'cleanup' from 'active' state.
destogl 02b6cb1
Disalbe external interfaces of LifecycleNode.
destogl dc0c59a
Use 'auto_declare' for 'update_rate' parameter.
destogl b5d0c8f
Fix error when rebasing.
destogl 9b57e73
Added tests for ControllerInterface.
destogl 4c2fdba
Rename and optimize tests of controller_interface.
destogl 6bdc685
Add check in controller interface to update 'update_rate' parameter o…
destogl 1e54864
Merge branch 'master' into controller-interface-test-pr
destogl 134a5f7
Correct formatting.
destogl 6b817aa
Remove chainable controller cpp file because it is not added yet.
destogl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
controller_interface/test/test_controller_interface.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| // Copyright (c) 2022, Stogl Robotics Consulting UG (haftungsbeschränkt) | ||
| // | ||
| // 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 "test_controller_interface.hpp" | ||
|
|
||
| #include <gmock/gmock.h> | ||
| #include <memory> | ||
|
|
||
| #include "rclcpp/executor_options.hpp" | ||
| #include "rclcpp/executors/multi_threaded_executor.hpp" | ||
|
|
||
| template <class T, size_t N> | ||
| constexpr size_t arrlen(T (&)[N]) | ||
| { | ||
| return N; | ||
| } | ||
|
|
||
| TEST(TestableControllerInterface, init) | ||
| { | ||
| char const * const argv[] = {""}; | ||
| int argc = arrlen(argv); | ||
| rclcpp::init(argc, argv); | ||
|
|
||
| TestableControllerInterface controller; | ||
|
|
||
| // try to get node when not initialized | ||
| ASSERT_THROW(controller.get_node(), std::runtime_error); | ||
|
|
||
| // initialize, create node | ||
| ASSERT_EQ(controller.init(TEST_CONTROLLER_NAME), controller_interface::return_type::OK); | ||
| ASSERT_NO_THROW(controller.get_node()); | ||
|
|
||
| // update_rate is set to default 0 | ||
| ASSERT_EQ(controller.get_update_rate(), 0u); | ||
|
|
||
| // Even after configure is 0 | ||
| controller.configure(); | ||
| ASSERT_EQ(controller.get_update_rate(), 0u); | ||
|
|
||
| rclcpp::shutdown(); | ||
| } | ||
|
|
||
| TEST(TestableControllerInterface, setting_update_rate_in_configure) | ||
| { | ||
| // mocks the declaration of overrides parameters in a yaml file | ||
| char const * const argv[] = {"", "--ros-args", "-p", "update_rate:=2812"}; | ||
| int argc = arrlen(argv); | ||
| rclcpp::init(argc, argv); | ||
|
|
||
| TestableControllerInterface controller; | ||
| // initialize, create node | ||
| ASSERT_EQ(controller.init(TEST_CONTROLLER_NAME), controller_interface::return_type::OK); | ||
|
|
||
| // initialize executor to be able to get parameter update | ||
| auto executor = | ||
| std::make_shared<rclcpp::executors::MultiThreadedExecutor>(rclcpp::ExecutorOptions(), 2); | ||
|
|
||
| executor->add_node(controller.get_node()->get_node_base_interface()); | ||
| auto update_executor_spin_future = | ||
| std::async(std::launch::async, [&]() -> void { executor->spin(); }); | ||
| // This sleep is needed to prevent a too fast test from ending before the | ||
| // executor has began to spin, which causes it to hang | ||
| using namespace std::chrono_literals; | ||
| std::this_thread::sleep_for(50ms); | ||
|
|
||
| // update_rate is set to default 0 because it is set on configure | ||
| ASSERT_EQ(controller.get_update_rate(), 0u); | ||
|
|
||
| // Even after configure is 0 | ||
| controller.configure(); | ||
| ASSERT_EQ(controller.get_update_rate(), 2812u); | ||
|
|
||
| // Test updating of update_rate parameter | ||
| EXPECT_EQ(std::system("ros2 param set /testable_controller_interface update_rate 623"), 0); | ||
| // Keep the same update rate until transition from 'UNCONFIGURED' TO 'INACTIVE' does not happen | ||
| controller.configure(); // No transition so the update rate should stay intact | ||
| ASSERT_NE(controller.get_update_rate(), 623u); | ||
| ASSERT_EQ(controller.get_update_rate(), 2812u); | ||
|
|
||
| controller.get_node()->activate(); | ||
| controller.configure(); // No transition so the update rate should stay intact | ||
| ASSERT_NE(controller.get_update_rate(), 623u); | ||
| ASSERT_EQ(controller.get_update_rate(), 2812u); | ||
|
|
||
| controller.update(controller.get_node()->now(), rclcpp::Duration::from_seconds(0.1)); | ||
| controller.configure(); // No transition so the update rate should stay intact | ||
| ASSERT_NE(controller.get_update_rate(), 623u); | ||
| ASSERT_EQ(controller.get_update_rate(), 2812u); | ||
|
|
||
| controller.get_node()->deactivate(); | ||
| controller.configure(); // No transition so the update rate should stay intact | ||
| ASSERT_NE(controller.get_update_rate(), 623u); | ||
| ASSERT_EQ(controller.get_update_rate(), 2812u); | ||
|
|
||
| controller.get_node()->cleanup(); | ||
| ASSERT_EQ(controller.get_update_rate(), 2812u); | ||
| // It is first changed after controller is configured again. | ||
| controller.configure(); | ||
| ASSERT_EQ(controller.get_update_rate(), 623u); | ||
|
|
||
| executor->cancel(); | ||
| rclcpp::shutdown(); | ||
| } | ||
|
|
||
| TEST(TestableControllerInterfaceInitError, init_with_error) | ||
| { | ||
| char const * const argv[] = {""}; | ||
| int argc = arrlen(argv); | ||
| rclcpp::init(argc, argv); | ||
|
|
||
| TestableControllerInterfaceInitError controller; | ||
|
|
||
| // initialize, create node | ||
| ASSERT_EQ(controller.init(TEST_CONTROLLER_NAME), controller_interface::return_type::ERROR); | ||
|
|
||
| rclcpp::shutdown(); | ||
| } | ||
|
|
||
| TEST(TestableControllerInterfaceInitFailure, init_with_failure) | ||
| { | ||
| char const * const argv[] = {""}; | ||
| int argc = arrlen(argv); | ||
| rclcpp::init(argc, argv); | ||
|
|
||
| TestableControllerInterfaceInitFailure controller; | ||
|
|
||
| // initialize, create node | ||
| ASSERT_EQ(controller.init(TEST_CONTROLLER_NAME), controller_interface::return_type::ERROR); | ||
|
|
||
| rclcpp::shutdown(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // Copyright (c) 2022, Stogl Robotics Consulting UG (haftungsbeschränkt) | ||
| // | ||
| // 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. | ||
|
|
||
| #ifndef TEST_CONTROLLER_INTERFACE_HPP_ | ||
| #define TEST_CONTROLLER_INTERFACE_HPP_ | ||
|
|
||
| #include "controller_interface/controller_interface.hpp" | ||
| #include "rclcpp_lifecycle/node_interfaces/lifecycle_node_interface.hpp" | ||
|
|
||
| using CallbackReturn = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn; | ||
|
|
||
| constexpr char TEST_CONTROLLER_NAME[] = "testable_controller_interface"; | ||
|
|
||
| class TestableControllerInterface : public controller_interface::ControllerInterface | ||
| { | ||
| public: | ||
| CallbackReturn on_init() override { return CallbackReturn::SUCCESS; } | ||
|
|
||
| controller_interface::InterfaceConfiguration command_interface_configuration() const override | ||
| { | ||
| return controller_interface::InterfaceConfiguration{ | ||
| controller_interface::interface_configuration_type::NONE}; | ||
| } | ||
|
|
||
| controller_interface::InterfaceConfiguration state_interface_configuration() const override | ||
| { | ||
| return controller_interface::InterfaceConfiguration{ | ||
| controller_interface::interface_configuration_type::NONE}; | ||
| } | ||
|
|
||
| controller_interface::return_type update( | ||
| const rclcpp::Time & /*time*/, const rclcpp::Duration & /*period*/) override | ||
| { | ||
| return controller_interface::return_type::OK; | ||
| } | ||
| }; | ||
|
|
||
| class TestableControllerInterfaceInitError : public TestableControllerInterface | ||
| { | ||
| public: | ||
| CallbackReturn on_init() override { return CallbackReturn::ERROR; } | ||
| }; | ||
|
|
||
| class TestableControllerInterfaceInitFailure : public TestableControllerInterface | ||
| { | ||
| public: | ||
| CallbackReturn on_init() override { return CallbackReturn::FAILURE; } | ||
| }; | ||
|
|
||
| #endif // TEST_CONTROLLER_INTERFACE_HPP_ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bmagyar please comment on this. Maybe you have an idea how to achieve the behavior I described.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@destogl I am maybe more a fan of:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is also a valid approach, for sure. @bmagyar what do you think?
The only “drawback” from my side is that uses have to explicit call this on the parent class. If someone forgets to do this, it may come with an issue that this is not working.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we shouldn't rely on people remembering stuff for us... the code in this PR may be ugly but we don't spill complexity into user code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bmagyar @destogl We could then maybe refactor parts in the
hardware_interface