From bcb013d159ba691519101c80b9e9c3d463a1e42f Mon Sep 17 00:00:00 2001 From: baila Date: Thu, 25 Jan 2024 23:01:28 +0530 Subject: [PATCH] Adding tests for cleanup service. --- .../test/test_controller_manager_srvs.cpp | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/controller_manager/test/test_controller_manager_srvs.cpp b/controller_manager/test/test_controller_manager_srvs.cpp index 2cd5645bd8..1071fd3d9f 100644 --- a/controller_manager/test/test_controller_manager_srvs.cpp +++ b/controller_manager/test/test_controller_manager_srvs.cpp @@ -434,6 +434,68 @@ TEST_F(TestControllerManagerSrvs, load_controller_srv) cm_->get_loaded_controllers()[0].c->get_state().id()); } +TEST_F(TestControllerManagerSrvs, cleanup_controller_srv) +{ + rclcpp::executors::SingleThreadedExecutor srv_executor; + rclcpp::Node::SharedPtr srv_node = std::make_shared("srv_client"); + srv_executor.add_node(srv_node); + rclcpp::Client::SharedPtr client = + srv_node->create_client( + "test_controller_manager/cleanup_controller"); + + auto request = std::make_shared(); + request->name = test_controller::TEST_CONTROLLER_NAME; + + // variation - 1: + // scenario: call the cleanup service when no controllers are loaded + // expected: it should return ERROR as no controllers will be found to cleanup + auto result = call_service_and_wait(*client, request, srv_executor); + ASSERT_FALSE(result->ok) << "Controller not loaded: " << request->name; + + // variation - 2: + // scenario: call the cleanup service when one controller is loaded + // expected: it should return OK as one controller will be found to cleanup + auto test_controller = std::make_shared(); + auto abstract_test_controller = cm_->add_controller( + test_controller, test_controller::TEST_CONTROLLER_NAME, + test_controller::TEST_CONTROLLER_CLASS_NAME); + EXPECT_EQ(1u, cm_->get_loaded_controllers().size()); + + result = call_service_and_wait(*client, request, srv_executor, true); + ASSERT_TRUE(result->ok); + EXPECT_EQ(1u, cm_->get_loaded_controllers().size()); + + // variation - 3: + // scenario: call the cleanup service when controller state is ACTIVE + // expected: it should return ERROR as cleanup is restricted for ACTIVE controllers + test_controller = std::dynamic_pointer_cast(cm_->load_controller( + test_controller::TEST_CONTROLLER_NAME, test_controller::TEST_CONTROLLER_CLASS_NAME)); + cm_->configure_controller(test_controller::TEST_CONTROLLER_NAME); + // activate controllers + cm_->switch_controller( + {test_controller::TEST_CONTROLLER_NAME}, {}, + controller_manager_msgs::srv::SwitchController::Request::STRICT, true, rclcpp::Duration(0, 0)); + EXPECT_EQ( + lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE, + cm_->get_loaded_controllers()[0].c->get_state().id()); + result = call_service_and_wait(*client, request, srv_executor, true); + ASSERT_FALSE(result->ok) << "Controller can not be cleaned in active state: " << request->name; + EXPECT_EQ(1u, cm_->get_loaded_controllers().size()); + + // variation - 4: + // scenario: call the cleanup service when controller state is INACTIVE + // expected: it should return OK as cleanup will be done for INACTIVE controllers + cm_->switch_controller( + {}, {test_controller::TEST_CONTROLLER_NAME}, + controller_manager_msgs::srv::SwitchController::Request::STRICT, true, rclcpp::Duration(0, 0)); + EXPECT_EQ( + lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE, + cm_->get_loaded_controllers()[0].c->get_state().id()); + result = call_service_and_wait(*client, request, srv_executor, true); + ASSERT_TRUE(result->ok) << "Controller cleaned in inactive state: " << request->name; + EXPECT_EQ(1u, cm_->get_loaded_controllers().size()); +} + TEST_F(TestControllerManagerSrvs, unload_controller_srv) { rclcpp::executors::SingleThreadedExecutor srv_executor;