Skip to content

Commit

Permalink
first test for autoware node
Browse files Browse the repository at this point in the history
Signed-off-by: M. Fatih Cırıt <[email protected]>
  • Loading branch information
xmfcx committed Nov 7, 2024
1 parent 30c1cd9 commit 58c0988
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 51 deletions.
2 changes: 1 addition & 1 deletion common/autoware_control_center/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ autoware_package()

ament_auto_add_library(${PROJECT_NAME} SHARED
src/control_center_node.cpp
src/include/control_center_node.hpp
include/autoware/control_center/control_center_node.hpp
)

# In order to instantiate only a single instance, we have to use custom main function
Expand Down
2 changes: 1 addition & 1 deletion common/autoware_control_center/src/control_center_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "include/control_center_node.hpp"
#include "autoware/control_center/control_center_node.hpp"

#include <rclcpp/rclcpp.hpp>

Expand Down
2 changes: 1 addition & 1 deletion common/autoware_control_center/src/control_center_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "include/control_center_node.hpp"
#include "autoware/control_center/control_center_node.hpp"

#include <autoware_utils/ros/uuid_helper.hpp>

Expand Down
2 changes: 1 addition & 1 deletion common/autoware_control_center/test/test_cc_heartbeat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#include "test_utility.hpp"

#include <control_center_node.hpp>
#include <autoware/control_center/control_center_node.hpp>
#include <rclcpp/rclcpp.hpp>
#include <rclcpp_lifecycle/lifecycle_node.hpp>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <control_center_node.hpp>
#include <autoware/control_center/control_center_node.hpp>
#include <rclcpp/rclcpp.hpp>
#include <rclcpp_lifecycle/lifecycle_node.hpp>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#include "test_utility.hpp"

#include <control_center_node.hpp>
#include <autoware/control_center/control_center_node.hpp>
#include <rclcpp/rclcpp.hpp>
#include <rclcpp_lifecycle/lifecycle_node.hpp>

Expand Down
21 changes: 17 additions & 4 deletions common/autoware_node/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,23 @@ autoware_package()
ament_auto_add_library(${PROJECT_NAME} src/node.cpp)

if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
ament_lint_auto_find_test_dependencies()
ament_add_ros_isolated_gtest(test_autoware_node test/test_node.cpp)
target_link_libraries(test_autoware_node ${PROJECT_NAME})
file(GLOB_RECURSE TEST_FILES test/*.cpp)

foreach(TEST_FILE ${TEST_FILES})
# Get the test name without directory and extension
get_filename_component(TEST_NAME ${TEST_FILE} NAME_WE)

# Add each test separately
ament_add_ros_isolated_gtest(${TEST_NAME} ${TEST_FILE} TIMEOUT 10)
target_include_directories(${TEST_NAME} PRIVATE src/include)
target_link_libraries(${TEST_NAME} ${PROJECT_NAME})
ament_target_dependencies(${TEST_NAME}
rclcpp
rclcpp_lifecycle
autoware_utils
autoware_control_center_msgs
autoware_control_center)
endforeach()
endif()

ament_auto_package()
3 changes: 3 additions & 0 deletions common/autoware_node/config/node.param.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/**:
ros__parameters:
period_timer_register_ms: 100
1 change: 0 additions & 1 deletion common/autoware_node/include/autoware/node/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ class Node : public rclcpp_lifecycle::LifecycleNode
rclcpp::TimerBase::SharedPtr timer_registration_;
// rclcpp::TimerBase::SharedPtr timer_heartbeat_;

const std::string full_name_;
bool is_registered_;
unique_identifier_msgs::msg::UUID uuid_node_;
// uint16_t sequence_number_;
Expand Down
5 changes: 2 additions & 3 deletions common/autoware_node/src/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ namespace autoware::node
Node::Node(
const std::string & node_name, const std::string & ns, const rclcpp::NodeOptions & options)
: LifecycleNode(node_name, ns, options),
full_name_{get_node_base_interface()->get_fully_qualified_name()},
is_registered_{false}
{
RCLCPP_DEBUG(get_logger(), "Node %s constructor", full_name_.c_str());
RCLCPP_DEBUG(get_logger(), "Node %s constructor", get_node_base_interface()->get_fully_qualified_name());
std::chrono::milliseconds period_timer_register(
declare_parameter<int>("period_timer_register_ms"));

Expand Down Expand Up @@ -72,7 +71,7 @@ void Node::on_tick_registration()
}
autoware_control_center_msgs::srv::Register::Request::SharedPtr req =
std::make_shared<autoware_control_center_msgs::srv::Register::Request>();
req->node_name_with_namespace = full_name_;
req->node_name_with_namespace = get_node_base_interface()->get_fully_qualified_name();

cli_register_->async_send_request(
req, std::bind(&Node::on_register, this, std::placeholders::_1));
Expand Down
83 changes: 64 additions & 19 deletions common/autoware_node/test/test_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,69 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "test_node.hpp"

#include "autoware/node/node.hpp"
#include "gtest/gtest.h"
// #include "rclcpp_components/rclcpp.hpp"

// class AutowareNodeTest : public ::testing::Test
// {
// public:
// void SetUp() override
// {
// rclcpp::init(0, nullptr);
// autoware_node_ =
// std::make_shared<autoware_node::AutowareNode(rclcpp::NodeOptions());
// }

// void TearDown() override {rclcpp::shutdown();}
// autoware_node::AutowareNode::SharedPtr autoware_node_;
// }
#include <autoware/control_center/control_center_node.hpp>
#include <autoware/node/node.hpp>
#include <rclcpp/rclcpp.hpp>

#include <lifecycle_msgs/msg/state.hpp>

#include <gtest/gtest.h>

class AutowareNodeRegisterTest : public ::testing::Test
{
public:
void SetUp() override
{
rclcpp::init(0, nullptr);
node_options_an_.append_parameter_override("period_timer_register_ms", 100);

node_options_acc_.append_parameter_override("deadline_ms", 220.0);
node_options_acc_.append_parameter_override("report_publish_rate", 100.0);
}

void TearDown() override { rclcpp::shutdown(); }

rclcpp::NodeOptions node_options_acc_;
rclcpp::NodeOptions node_options_an_;
};

TEST_F(AutowareNodeRegisterTest, NodeInitShutdown)
{
std::shared_ptr<rclcpp::executors::SingleThreadedExecutor> executor;
executor = std::make_shared<rclcpp::executors::SingleThreadedExecutor>();

autoware::node::Node::SharedPtr autoware_node =
std::make_shared<autoware::node::Node>("test_node", "test_ns", node_options_an_);
autoware::node::Node::SharedPtr control_center =
std::make_shared<autoware::control_center::ControlCenter>(node_options_acc_);

executor->add_node(control_center->get_node_base_interface());
executor->add_node(autoware_node->get_node_base_interface());

std::cout << "Info - Added nodes to executor" << std::endl;

std::thread thread_spin = std::thread([&executor]() {
std::cout << "Thread - Spinning executor" << std::endl;
executor->spin();
std::cout << "Thread - Spun executor" << std::endl;
});

std::cout << "Info - Spinning executor" << std::endl;

ASSERT_EQ(
control_center->get_current_state().id(),
lifecycle_msgs::msg::State::PRIMARY_STATE_UNCONFIGURED);

auto state = control_center->shutdown();

ASSERT_EQ(state.id(), lifecycle_msgs::msg::State::PRIMARY_STATE_FINALIZED);

std::this_thread::sleep_for(std::chrono::milliseconds(50));
executor->cancel(); // make sure cancel is called after spin
std::this_thread::sleep_for(std::chrono::milliseconds(50));
if (thread_spin.joinable()) {
thread_spin.join();
}
}

// TEST_F(AutowareNodeTest, Sen)
18 changes: 0 additions & 18 deletions common/autoware_node/test/test_node.hpp

This file was deleted.

0 comments on commit 58c0988

Please sign in to comment.