-
Notifications
You must be signed in to change notification settings - Fork 479
Fix callback group logic in executor #2476
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
Closed
+160
−11
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
139 changes: 139 additions & 0 deletions
139
rclcpp/test/rclcpp/executors/test_executors_callback_group_behavior.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,139 @@ | ||
| // Copyright 2024 Open Source Robotics Foundation, Inc. | ||
| // | ||
| // 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. | ||
|
|
||
| /** | ||
| * This test checks that when callback groups go out of scope, that their associated executable | ||
| * entities should not be returned as valid executables. | ||
| * | ||
| * The test makes use of a bit of executor internals, but is meant to prevent regressions of behavior. | ||
| * Ref: https://github.com/ros2/rclcpp/issues/2474 | ||
| */ | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <chrono> | ||
| #include <rclcpp/callback_group.hpp> | ||
| #include <rclcpp/executor.hpp> | ||
| #include <rclcpp/node.hpp> | ||
|
|
||
| class CustomExecutor: public rclcpp::Executor | ||
| { | ||
| public: | ||
| explicit CustomExecutor(const rclcpp::ExecutorOptions & options = rclcpp::ExecutorOptions()) | ||
| : rclcpp::Executor(options) | ||
| { | ||
| } | ||
|
|
||
| ~CustomExecutor() override = default; | ||
|
|
||
| void spin() override {}; | ||
|
|
||
| void collect() { | ||
| this->collect_entities(); | ||
| } | ||
|
|
||
| void wait() { | ||
| this->wait_for_work(std::chrono::milliseconds(10)); | ||
| } | ||
|
|
||
| size_t collected_timers() const { | ||
| return this->current_collection_.timers.size(); | ||
| } | ||
|
|
||
| rclcpp::AnyExecutable next() { | ||
| rclcpp::AnyExecutable ret; | ||
| this->get_next_ready_executable(ret); | ||
| return ret; | ||
| } | ||
| }; | ||
|
|
||
|
|
||
| TEST(TestCallbackGroup, ValidCbg) | ||
wjwwood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| rclcpp::init(0, nullptr); | ||
|
|
||
| // Create a timer associated with a callback group | ||
| auto node = std::make_shared<rclcpp::Node>("node"); | ||
|
|
||
| // Add the callback group to the executor | ||
| auto executor = CustomExecutor(); | ||
| executor.add_node(node); | ||
| executor.spin_all(std::chrono::milliseconds(10)); | ||
|
|
||
| auto cbg = node->create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive, true); | ||
| auto timer = node->create_wall_timer(std::chrono::milliseconds(1), [](){}, cbg); | ||
| executor.add_callback_group(cbg, node->get_node_base_interface()); | ||
|
|
||
| executor.spin_all(std::chrono::milliseconds(10)); | ||
|
|
||
| // Collect the entities | ||
| executor.collect(); | ||
| EXPECT_EQ(1u, executor.collected_timers()); | ||
|
|
||
| executor.wait(); | ||
| auto next_executable = executor.next(); | ||
| EXPECT_EQ(timer, next_executable.timer); | ||
| EXPECT_EQ(cbg, next_executable.callback_group); | ||
|
|
||
| EXPECT_EQ(nullptr, next_executable.client); | ||
| EXPECT_EQ(nullptr, next_executable.service); | ||
| EXPECT_EQ(nullptr, next_executable.subscription); | ||
| EXPECT_EQ(nullptr, next_executable.waitable); | ||
| EXPECT_EQ(nullptr, next_executable.data); | ||
|
|
||
| rclcpp::shutdown(); | ||
| } | ||
|
|
||
| TEST(TestCallbackGroup, InvalidCbg) | ||
wjwwood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| rclcpp::init(0, nullptr); | ||
|
|
||
| // Create a timer associated with a callback group | ||
| auto node = std::make_shared<rclcpp::Node>("node"); | ||
|
|
||
| // Add the callback group to the executor | ||
| auto executor = CustomExecutor(); | ||
| executor.add_node(node); | ||
| executor.spin_all(std::chrono::milliseconds(10)); | ||
|
Comment on lines
+105
to
+108
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious why is this needed?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was to clear out all of the node startup transients. |
||
|
|
||
| auto cbg = node->create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive, false); | ||
| auto timer = node->create_wall_timer(std::chrono::milliseconds(1), [](){}, cbg); | ||
| executor.add_callback_group(cbg, nullptr); | ||
|
|
||
| executor.spin_all(std::chrono::milliseconds(10)); | ||
|
|
||
| // Collect the entities | ||
| executor.collect(); | ||
| EXPECT_EQ(1u, executor.collected_timers()); | ||
|
|
||
| executor.wait(); | ||
|
|
||
| cbg.reset(); | ||
|
|
||
| // Since the callback group has been reset, this should not be allowed to | ||
| // be a valid executable (timer and cbg should both be nullptr). | ||
| // In the regression, timer == next_executable.timer whil | ||
| // next_executable.callback_group == nullptr, which was incorrect. | ||
| auto next_executable = executor.next(); | ||
| EXPECT_EQ(nullptr, next_executable.timer); | ||
| EXPECT_EQ(nullptr, next_executable.callback_group); | ||
|
|
||
| EXPECT_EQ(nullptr, next_executable.client); | ||
| EXPECT_EQ(nullptr, next_executable.service); | ||
| EXPECT_EQ(nullptr, next_executable.subscription); | ||
| EXPECT_EQ(nullptr, next_executable.waitable); | ||
| EXPECT_EQ(nullptr, next_executable.data); | ||
|
|
||
| rclcpp::shutdown(); | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.