-
Notifications
You must be signed in to change notification settings - Fork 717
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
feat!: move diagnostics_module from localization_util to unverse_utils #9714
Merged
kminoda
merged 15 commits into
autowarefoundation:main
from
kminoda:feat/diagnostics_module/move_to_universe_utils
Dec 25, 2024
Merged
Changes from 8 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
fb092e0
feat!: move diagnostics_module from localization_util to unverse_utils
kminoda b9b0453
remove diagnostics module from localization_util
kminoda 0c1c207
style(pre-commit): autofix
pre-commit-ci[bot] ca7cbe0
minor fix in pose_initializer
kminoda 16365d0
Merge remote-tracking branch 'origin/main' into feat/diagnostics_modu…
kminoda 40ff6d3
add test
kminoda fb4c6fc
style(pre-commit): autofix
pre-commit-ci[bot] 6496316
remove unnecessary declaration
kminoda b7a90d8
module -> interface
kminoda 113d770
remove unnecessary equal expression
kminoda 0af8a89
revert the remove of template function
kminoda 82fafa7
style(pre-commit): autofix
pre-commit-ci[bot] c981570
use overload instead
kminoda f062533
Merge branch 'main' into feat/diagnostics_module/move_to_universe_utils
kminoda cb5163f
include what you use -- test_diagnostics_interface.cpp
kminoda 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 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 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 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
176 changes: 176 additions & 0 deletions
176
common/autoware_universe_utils/test/src/ros/test_diagnostics_module.cpp
This file contains 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,176 @@ | ||
// Copyright 2024 Autoware Foundation | ||
// | ||
// 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 "autoware/universe_utils/ros/diagnostics_module.hpp" | ||
|
||
#include <rclcpp/rclcpp.hpp> | ||
|
||
#include <diagnostic_msgs/msg/diagnostic_array.hpp> | ||
#include <diagnostic_msgs/msg/diagnostic_status.hpp> | ||
#include <diagnostic_msgs/msg/key_value.hpp> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
using autoware::universe_utils::DiagnosticsModule; | ||
|
||
class TestDiagnosticsModule : public ::testing::Test | ||
{ | ||
protected: | ||
void SetUp() override | ||
{ | ||
// Create a test node | ||
node_ = std::make_shared<rclcpp::Node>("test_diagnostics_module"); | ||
} | ||
|
||
// Automatically destroyed at the end of each test | ||
std::shared_ptr<rclcpp::Node> node_; | ||
}; | ||
|
||
/* | ||
* Test clear(): | ||
* Verify that calling clear() resets DiagnosticStatus values, level, and message. | ||
*/ | ||
TEST_F(TestDiagnosticsModule, ClearTest) | ||
{ | ||
DiagnosticsModule module(node_.get(), "test_diagnostic"); | ||
|
||
// Add some key-value pairs and update level/message | ||
module.add_key_value("param1", 42); | ||
module.update_level_and_message( | ||
diagnostic_msgs::msg::DiagnosticStatus::WARN, "Something is not OK"); | ||
|
||
// Call clear() | ||
module.clear(); | ||
|
||
// After calling clear(), publish and check the contents of the message | ||
diagnostic_msgs::msg::DiagnosticArray::ConstSharedPtr received_msg; | ||
auto sub = node_->create_subscription<diagnostic_msgs::msg::DiagnosticArray>( | ||
"/diagnostics", 10, | ||
[&](diagnostic_msgs::msg::DiagnosticArray::ConstSharedPtr msg) { received_msg = msg; }); | ||
|
||
// Publish the message | ||
module.publish(node_->now()); | ||
|
||
// Spin to allow the subscriber to receive messages | ||
rclcpp::spin_some(node_); | ||
|
||
ASSERT_NE(nullptr, received_msg); | ||
ASSERT_FALSE(received_msg->status.empty()); | ||
const auto & status = received_msg->status.front(); | ||
|
||
// After clear(), key-value pairs should be empty | ||
EXPECT_TRUE(status.values.empty()); | ||
|
||
// After clear(), level should be OK (=0) | ||
EXPECT_EQ(status.level, diagnostic_msgs::msg::DiagnosticStatus::OK); | ||
|
||
// After clear(), message is empty internally, | ||
// but "OK" is set during publishing when level == OK | ||
EXPECT_EQ(status.message, "OK"); | ||
} | ||
|
||
/* | ||
* Test add_key_value(): | ||
* Verify that adding the same key updates its value instead of adding a duplicate. | ||
*/ | ||
TEST_F(TestDiagnosticsModule, AddKeyValueTest) | ||
{ | ||
DiagnosticsModule module(node_.get(), "test_diagnostic"); | ||
|
||
// Call the template version of add_key_value() with different types | ||
module.add_key_value("string_key", std::string("initial_value")); | ||
module.add_key_value("int_key", 123); | ||
module.add_key_value("bool_key", true); | ||
|
||
// Overwrite the value for "string_key" | ||
module.add_key_value("string_key", std::string("updated_value")); | ||
|
||
// Capture the published message to verify its contents | ||
diagnostic_msgs::msg::DiagnosticArray::ConstSharedPtr received_msg; | ||
auto sub = node_->create_subscription<diagnostic_msgs::msg::DiagnosticArray>( | ||
"/diagnostics", 10, | ||
[&](diagnostic_msgs::msg::DiagnosticArray::ConstSharedPtr msg) { received_msg = msg; }); | ||
module.publish(node_->now()); | ||
rclcpp::spin_some(node_); | ||
|
||
ASSERT_NE(nullptr, received_msg); | ||
ASSERT_FALSE(received_msg->status.empty()); | ||
const auto & status = received_msg->status.front(); | ||
|
||
// Expect 3 key-value pairs | ||
ASSERT_EQ(status.values.size(), 3U); | ||
|
||
// Helper lambda to find a value by key | ||
auto find_value = [&](const std::string & key) -> std::string { | ||
for (const auto & kv : status.values) { | ||
if (kv.key == key) { | ||
return kv.value; | ||
} | ||
} | ||
return ""; | ||
}; | ||
|
||
EXPECT_EQ(find_value("string_key"), "updated_value"); | ||
EXPECT_EQ(find_value("int_key"), "123"); | ||
EXPECT_EQ(find_value("bool_key"), "True"); | ||
|
||
// Status level should still be OK | ||
EXPECT_EQ(status.level, diagnostic_msgs::msg::DiagnosticStatus::OK); | ||
// Message should be "OK" | ||
EXPECT_EQ(status.message, "OK"); | ||
} | ||
|
||
/* | ||
* Test update_level_and_message(): | ||
* Verify that the level is updated to the highest severity and | ||
* that messages are concatenated if level > OK. | ||
*/ | ||
TEST_F(TestDiagnosticsModule, UpdateLevelAndMessageTest) | ||
{ | ||
DiagnosticsModule module(node_.get(), "test_diagnostic"); | ||
|
||
// Initial status is level=OK(0), message="" | ||
module.update_level_and_message(diagnostic_msgs::msg::DiagnosticStatus::OK, "Initial OK"); | ||
// Update to WARN (1) | ||
module.update_level_and_message(diagnostic_msgs::msg::DiagnosticStatus::WARN, "Low battery"); | ||
// Update to ERROR (2) | ||
module.update_level_and_message(diagnostic_msgs::msg::DiagnosticStatus::ERROR, "Sensor failure"); | ||
// Another WARN (1) after ERROR should not downgrade the overall level | ||
module.update_level_and_message( | ||
diagnostic_msgs::msg::DiagnosticStatus::WARN, "Should not override error"); | ||
|
||
diagnostic_msgs::msg::DiagnosticArray::ConstSharedPtr received_msg; | ||
auto sub = node_->create_subscription<diagnostic_msgs::msg::DiagnosticArray>( | ||
"/diagnostics", 10, | ||
[&](diagnostic_msgs::msg::DiagnosticArray::ConstSharedPtr msg) { received_msg = msg; }); | ||
|
||
module.publish(node_->now()); | ||
rclcpp::spin_some(node_); | ||
|
||
ASSERT_NE(nullptr, received_msg); | ||
ASSERT_FALSE(received_msg->status.empty()); | ||
const auto & status = received_msg->status.front(); | ||
|
||
// Final level should be ERROR (2) | ||
EXPECT_EQ(status.level, diagnostic_msgs::msg::DiagnosticStatus::ERROR); | ||
|
||
// The message should contain all parts that were added when level > OK. | ||
// Thus, we expect something like: | ||
// "Low battery; Sensor failure; Should not override error" | ||
const std::string & final_message = status.message; | ||
EXPECT_FALSE(final_message.find("Initial OK") != std::string::npos); | ||
EXPECT_TRUE(final_message.find("Low battery") != std::string::npos); | ||
EXPECT_TRUE(final_message.find("Sensor failure") != std::string::npos); | ||
EXPECT_TRUE(final_message.find("Should not override error") != std::string::npos); | ||
} |
This file contains 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 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 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 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 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 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 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 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 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 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 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
Oops, something went wrong.
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.
IMO DiagnosticInterface is better
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.
Fixed: b7a90d8