-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add runid #5553
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
Draft
silanus23
wants to merge
4
commits into
ros-navigation:main
Choose a base branch
from
silanus23:add-runid
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+216
−4
Draft
Add runid #5553
Changes from all commits
Commits
Show all changes
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -196,7 +196,7 @@ bool BtActionServer<ActionT, NodeT>::on_configure() | |
blackboard_->set<std::chrono::milliseconds>( | ||
"wait_for_service_timeout", | ||
wait_for_service_timeout_); | ||
|
||
blackboard_->set<uint64_t>("run_id", run_id_); // NOLINT | ||
return true; | ||
} | ||
|
||
|
@@ -336,6 +336,9 @@ void BtActionServer<ActionT, NodeT>::executeCallback() | |
return; | ||
} | ||
|
||
run_id_++; | ||
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. I think we should use a UUID unique hash instead |
||
blackboard_->set<uint64_t>("run_id", run_id_); | ||
|
||
auto is_canceling = [&]() { | ||
if (action_server_ == nullptr) { | ||
RCLCPP_DEBUG(logger_, "Action server unavailable. Canceling."); | ||
|
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 |
---|---|---|
|
@@ -147,7 +147,13 @@ class FibonacciAction : public nav2_behavior_tree::BtActionNode<test_msgs::actio | |
|
||
BT::NodeStatus on_cancelled() override | ||
{ | ||
config().blackboard->set("sequence", result_.result->sequence); | ||
// Check if result is available before accessing it | ||
if (result_.result) { | ||
config().blackboard->set("sequence", result_.result->sequence); | ||
} else { | ||
// Set empty sequence if no result available | ||
config().blackboard->set("sequence", std::vector<int>()); | ||
} | ||
config().blackboard->set("on_cancelled_triggered", true); | ||
return BT::NodeStatus::SUCCESS; | ||
} | ||
|
@@ -483,6 +489,180 @@ TEST_F(BTActionNodeTestFixture, test_server_cancel) | |
EXPECT_EQ(ticks, 7); | ||
} | ||
|
||
TEST_F(BTActionNodeTestFixture, test_run_id_initialization_and_persistence) | ||
{ | ||
// create tree with is_global="true" to enable RunID checking | ||
std::string xml_txt = | ||
R"( | ||
<root BTCPP_format="4"> | ||
<BehaviorTree ID="MainTree"> | ||
<Fibonacci order="100" is_global="true" /> | ||
</BehaviorTree> | ||
</root>)"; | ||
|
||
config_->blackboard->set<std::chrono::milliseconds>("server_timeout", 100ms); | ||
config_->blackboard->set<std::chrono::milliseconds>("bt_loop_duration", 10ms); | ||
|
||
// Set initial RunID | ||
config_->blackboard->set<uint64_t>("run_id", 1); | ||
|
||
tree_ = std::make_shared<BT::Tree>(factory_->createTreeFromText(xml_txt, config_->blackboard)); | ||
action_server_->setHandleGoalSleepDuration(2ms); | ||
action_server_->setServerLoopRate(50ms); // Slower server rate | ||
|
||
// First tick should initialize with run_id = 1 | ||
auto result = tree_->tickOnce(); | ||
EXPECT_EQ(result, BT::NodeStatus::RUNNING); | ||
|
||
// Subsequent ticks with same RunID should continue without re-initialization | ||
result = tree_->tickOnce(); | ||
EXPECT_EQ(result, BT::NodeStatus::RUNNING); | ||
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. how come all tests have the same |
||
|
||
// Clean up by halting the tree | ||
tree_->haltTree(); | ||
} | ||
|
||
TEST_F(BTActionNodeTestFixture, test_run_id_changes_trigger_reinitialization) | ||
{ | ||
std::string xml_txt = | ||
R"( | ||
<root BTCPP_format="4"> | ||
<BehaviorTree ID="MainTree"> | ||
<Fibonacci order="50" is_global="true" /> | ||
</BehaviorTree> | ||
</root>)"; | ||
|
||
config_->blackboard->set<std::chrono::milliseconds>("server_timeout", 100ms); | ||
config_->blackboard->set<std::chrono::milliseconds>("bt_loop_duration", 10ms); | ||
|
||
tree_ = std::make_shared<BT::Tree>(factory_->createTreeFromText(xml_txt, config_->blackboard)); | ||
action_server_->setHandleGoalSleepDuration(2ms); | ||
action_server_->setServerLoopRate(50ms); | ||
|
||
// Test with multiple run_id changes | ||
for (uint64_t run_id = 1; run_id <= 3; ++run_id) { | ||
config_->blackboard->set<uint64_t>("run_id", run_id); | ||
|
||
// Halt tree to reset state | ||
tree_->haltTree(); | ||
|
||
// First tick with new run_id should start new execution | ||
auto result = tree_->tickOnce(); | ||
EXPECT_EQ(result, BT::NodeStatus::RUNNING) << "Failed on run_id: " << run_id; | ||
} | ||
|
||
// Final cleanup | ||
tree_->haltTree(); | ||
} | ||
|
||
TEST_F(BTActionNodeTestFixture, test_run_id_non_global_mode_unaffected) | ||
{ | ||
// Test without is_global flag (should use old behavior) | ||
std::string xml_txt = | ||
R"( | ||
<root BTCPP_format="4"> | ||
<BehaviorTree ID="MainTree"> | ||
<Fibonacci order="10" /> | ||
</BehaviorTree> | ||
</root>)"; | ||
|
||
config_->blackboard->set<std::chrono::milliseconds>("server_timeout", 100ms); | ||
config_->blackboard->set<std::chrono::milliseconds>("bt_loop_duration", 10ms); | ||
|
||
tree_ = std::make_shared<BT::Tree>(factory_->createTreeFromText(xml_txt, config_->blackboard)); | ||
action_server_->setHandleGoalSleepDuration(2ms); | ||
action_server_->setServerLoopRate(10ns); | ||
|
||
// Should work normally without RunID checking | ||
auto result = tree_->tickOnce(); | ||
EXPECT_EQ(result, BT::NodeStatus::RUNNING); | ||
} | ||
|
||
TEST_F(BTActionNodeTestFixture, test_run_id_missing_from_blackboard) | ||
{ | ||
// Test behavior when run_id is not set - should work like old behavior | ||
std::string xml_txt = | ||
R"( | ||
<root BTCPP_format="4"> | ||
<BehaviorTree ID="MainTree"> | ||
<Fibonacci order="5" is_global="true" /> | ||
</BehaviorTree> | ||
</root>)"; | ||
|
||
config_->blackboard->set<std::chrono::milliseconds>("server_timeout", 100ms); | ||
config_->blackboard->set<std::chrono::milliseconds>("bt_loop_duration", 10ms); | ||
|
||
// Don't set run_id - test graceful handling | ||
tree_ = std::make_shared<BT::Tree>(factory_->createTreeFromText(xml_txt, config_->blackboard)); | ||
action_server_->setHandleGoalSleepDuration(2ms); | ||
action_server_->setServerLoopRate(10ns); | ||
|
||
// Should work like old behavior when RunID is missing | ||
auto result = tree_->tickOnce(); | ||
EXPECT_EQ(result, BT::NodeStatus::RUNNING); | ||
|
||
tree_->haltTree(); | ||
} | ||
|
||
TEST_F(BTActionNodeTestFixture, test_run_id_change_during_execution) | ||
{ | ||
// Test RunID change while node is already running (key preemption scenario) | ||
std::string xml_txt = | ||
R"( | ||
<root BTCPP_format="4"> | ||
<BehaviorTree ID="MainTree"> | ||
<Fibonacci order="100" is_global="true" /> | ||
</BehaviorTree> | ||
</root>)"; | ||
|
||
config_->blackboard->set<std::chrono::milliseconds>("server_timeout", 100ms); | ||
config_->blackboard->set<std::chrono::milliseconds>("bt_loop_duration", 10ms); | ||
config_->blackboard->set<uint64_t>("run_id", 1); | ||
|
||
tree_ = std::make_shared<BT::Tree>(factory_->createTreeFromText(xml_txt, config_->blackboard)); | ||
action_server_->setHandleGoalSleepDuration(2ms); | ||
action_server_->setServerLoopRate(10ns); | ||
|
||
// Start execution with run_id = 1 | ||
auto result = tree_->tickOnce(); | ||
EXPECT_EQ(result, BT::NodeStatus::RUNNING); | ||
|
||
// Change RunID while running (simulates new navigation goal) | ||
config_->blackboard->set<uint64_t>("run_id", 2); | ||
|
||
// Next tick should detect change and reinitialize | ||
result = tree_->tickOnce(); | ||
EXPECT_EQ(result, BT::NodeStatus::RUNNING); | ||
|
||
tree_->haltTree(); | ||
} | ||
|
||
TEST_F(BTActionNodeTestFixture, test_run_id_zero_edge_case) | ||
{ | ||
std::string xml_txt = | ||
R"( | ||
<root BTCPP_format="4"> | ||
<BehaviorTree ID="MainTree"> | ||
<Fibonacci order="5" is_global="true" /> | ||
</BehaviorTree> | ||
</root>)"; | ||
|
||
config_->blackboard->set<std::chrono::milliseconds>("server_timeout", 100ms); | ||
config_->blackboard->set<std::chrono::milliseconds>("bt_loop_duration", 10ms); | ||
|
||
// Test with RunID = 0 (edge case) | ||
config_->blackboard->set<uint64_t>("run_id", 0); | ||
|
||
tree_ = std::make_shared<BT::Tree>(factory_->createTreeFromText(xml_txt, config_->blackboard)); | ||
action_server_->setHandleGoalSleepDuration(2ms); | ||
action_server_->setServerLoopRate(10ms); | ||
|
||
auto result = tree_->tickOnce(); | ||
EXPECT_EQ(result, BT::NodeStatus::RUNNING); | ||
|
||
tree_->haltTree(); | ||
} | ||
|
||
int main(int argc, char ** argv) | ||
{ | ||
::testing::InitGoogleTest(&argc, argv); | ||
|
Oops, something went wrong.
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.
I am not sure about this, that would be a weird surprise. Maybe just let it crash? It would be the same as if
server_timeout
is missing.