Skip to content

Commit

Permalink
Test fix: don't keep reference to the controller in the test when it …
Browse files Browse the repository at this point in the history
…should be destroyed in the controller manager (ros-controls#883)
  • Loading branch information
sgmurray authored Jan 18, 2023
1 parent a4f682e commit 70a156e
Showing 1 changed file with 41 additions and 10 deletions.
51 changes: 41 additions & 10 deletions controller_manager/test/test_spawner_unspawner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,29 +99,60 @@ TEST_F(TestLoadController, spawner_test_type_in_param)
EXPECT_EQ(call_spawner("ctrl_1 -c test_controller_manager"), 0);

ASSERT_EQ(cm_->get_loaded_controllers().size(), 1ul);
auto ctrl_1 = cm_->get_loaded_controllers()[0];
ASSERT_EQ(ctrl_1.info.name, "ctrl_1");
ASSERT_EQ(ctrl_1.info.type, test_controller::TEST_CONTROLLER_CLASS_NAME);
ASSERT_EQ(ctrl_1.c->get_state().id(), lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE);
{
auto ctrl_1 = cm_->get_loaded_controllers()[0];
ASSERT_EQ(ctrl_1.info.name, "ctrl_1");
ASSERT_EQ(ctrl_1.info.type, test_controller::TEST_CONTROLLER_CLASS_NAME);
ASSERT_EQ(ctrl_1.c->get_state().id(), lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE);
}

// Try to spawn again, it should fail because already active
EXPECT_NE(call_spawner("ctrl_1 -c test_controller_manager"), 0) << "Cannot configure from active";
ctrl_1.c->get_node()->deactivate();

std::vector<std::string> start_controllers = {};
std::vector<std::string> stop_controllers = {"ctrl_1"};
cm_->switch_controller(
start_controllers, stop_controllers,
controller_manager_msgs::srv::SwitchController::Request::STRICT, true, rclcpp::Duration(0, 0));

// We should be able to reconfigure and activate a configured controller
EXPECT_EQ(call_spawner("ctrl_1 -c test_controller_manager"), 0);
ctrl_1.c->get_node()->deactivate();
ctrl_1.c->get_node()->cleanup();

ASSERT_EQ(cm_->get_loaded_controllers().size(), 1ul);
{
auto ctrl_1 = cm_->get_loaded_controllers()[0];
ASSERT_EQ(ctrl_1.info.name, "ctrl_1");
ASSERT_EQ(ctrl_1.info.type, test_controller::TEST_CONTROLLER_CLASS_NAME);
ASSERT_EQ(ctrl_1.c->get_state().id(), lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE);
}

cm_->switch_controller(
start_controllers, stop_controllers,
controller_manager_msgs::srv::SwitchController::Request::STRICT, true, rclcpp::Duration(0, 0));
cm_->unload_controller("ctrl_1");
cm_->load_controller("ctrl_1");

// We should be able to reconfigure and activate am unconfigured loaded controller
EXPECT_EQ(call_spawner("ctrl_1 -c test_controller_manager"), 0);
ASSERT_EQ(ctrl_1.c->get_state().id(), lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE);
ASSERT_EQ(cm_->get_loaded_controllers().size(), 1ul);
{
auto ctrl_1 = cm_->get_loaded_controllers()[0];
ASSERT_EQ(ctrl_1.info.name, "ctrl_1");
ASSERT_EQ(ctrl_1.info.type, test_controller::TEST_CONTROLLER_CLASS_NAME);
ASSERT_EQ(ctrl_1.c->get_state().id(), lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE);
}

// Unload and reload
EXPECT_EQ(call_unspawner("ctrl_1 -c test_controller_manager"), 0);
ASSERT_EQ(cm_->get_loaded_controllers().size(), 0ul) << "Controller should have been unloaded";
EXPECT_EQ(call_spawner("ctrl_1 -c test_controller_manager"), 0);
ASSERT_EQ(cm_->get_loaded_controllers().size(), 1ul) << "Controller should have been loaded";
ctrl_1 = cm_->get_loaded_controllers()[0];
ASSERT_EQ(ctrl_1.c->get_state().id(), lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE);
{
auto ctrl_1 = cm_->get_loaded_controllers()[0];
ASSERT_EQ(ctrl_1.info.name, "ctrl_1");
ASSERT_EQ(ctrl_1.info.type, test_controller::TEST_CONTROLLER_CLASS_NAME);
ASSERT_EQ(ctrl_1.c->get_state().id(), lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE);
}
}

TEST_F(TestLoadController, spawner_test_type_in_arg)
Expand Down

0 comments on commit 70a156e

Please sign in to comment.