Skip to content

Commit

Permalink
Aggregate all component manager API tests. (#876)
Browse files Browse the repository at this point in the history
Signed-off-by: Michel Hidalgo <michel@ekumenlabs.com>
  • Loading branch information
hidmic authored Sep 26, 2019
1 parent b178c47 commit 4afd1cd
Showing 1 changed file with 46 additions and 101 deletions.
147 changes: 46 additions & 101 deletions rclcpp_components/test/test_component_manager_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class TestComponentManager : public ::testing::Test
}
};

TEST_F(TestComponentManager, load_components)
// TODO(hidmic): split up tests once Node bring up/tear down races
// are solved https://github.com/ros2/rclcpp/issues/863
TEST_F(TestComponentManager, components_api)
{
auto exec = std::make_shared<rclcpp::executors::SingleThreadedExecutor>();
auto node = rclcpp::Node::make_shared("test_component_manager");
Expand Down Expand Up @@ -111,34 +113,6 @@ TEST_F(TestComponentManager, load_components)
EXPECT_EQ(result.get()->unique_id, 4u);
}

auto node_names = node->get_node_names();

auto find_in_nodes = [node_names](std::string name) {
return std::find(node_names.begin(), node_names.end(), name) != node_names.end();
};

EXPECT_TRUE(find_in_nodes("/test_component_foo"));
EXPECT_TRUE(find_in_nodes("/test_component_bar"));
EXPECT_TRUE(find_in_nodes("/test_component_baz"));
EXPECT_TRUE(find_in_nodes("/ns/test_component_bing"));
}

TEST_F(TestComponentManager, load_invalid_components)
{
auto exec = std::make_shared<rclcpp::executors::SingleThreadedExecutor>();
auto node = rclcpp::Node::make_shared("test_component_manager");
auto manager = std::make_shared<rclcpp_components::ComponentManager>(exec);

exec->add_node(manager);
exec->add_node(node);

auto client = node->create_client<composition_interfaces::srv::LoadNode>(
"/ComponentManager/_container/load_node");

if (!client->wait_for_service(20s)) {
ASSERT_TRUE(false) << "service not available after waiting";
}

{
// Valid package, but invalid class name.
auto request = std::make_shared<composition_interfaces::srv::LoadNode::Request>();
Expand Down Expand Up @@ -168,40 +142,17 @@ TEST_F(TestComponentManager, load_invalid_components)
EXPECT_EQ(result.get()->full_node_name, "");
EXPECT_EQ(result.get()->unique_id, 0u);
}
}


TEST_F(TestComponentManager, list_components)
{
auto exec = std::make_shared<rclcpp::executors::SingleThreadedExecutor>();
auto node = rclcpp::Node::make_shared("test_component_manager");
auto manager = std::make_shared<rclcpp_components::ComponentManager>(exec);

exec->add_node(manager);
exec->add_node(node);

{
auto client = node->create_client<composition_interfaces::srv::LoadNode>(
"/ComponentManager/_container/load_node");

if (!client->wait_for_service(20s)) {
ASSERT_TRUE(false) << "service not available after waiting";
}
auto node_names = node->get_node_names();

{
auto request = std::make_shared<composition_interfaces::srv::LoadNode::Request>();
request->package_name = "rclcpp_components";
request->plugin_name = "test_rclcpp_components::TestComponentFoo";
auto find_in_nodes = [node_names](std::string name) {
return std::find(node_names.begin(), node_names.end(), name) != node_names.end();
};

auto result = client->async_send_request(request);
auto ret = exec->spin_until_future_complete(result, 5s); // Wait for the result.
EXPECT_EQ(ret, rclcpp::executor::FutureReturnCode::SUCCESS);
EXPECT_EQ(result.get()->success, true);
EXPECT_EQ(result.get()->error_message, "");
EXPECT_EQ(result.get()->full_node_name, "/test_component_foo");
EXPECT_EQ(result.get()->unique_id, 1u);
}
}
EXPECT_TRUE(find_in_nodes("/test_component_foo"));
EXPECT_TRUE(find_in_nodes("/test_component_bar"));
EXPECT_TRUE(find_in_nodes("/test_component_baz"));
EXPECT_TRUE(find_in_nodes("/ns/test_component_bing"));

{
auto client = node->create_client<composition_interfaces::srv::ListNodes>(
Expand All @@ -219,51 +170,18 @@ TEST_F(TestComponentManager, list_components)
auto node_names = result.get()->full_node_names;
auto unique_ids = result.get()->unique_ids;

EXPECT_EQ(node_names.size(), 1u);
EXPECT_EQ(node_names.size(), 4u);
EXPECT_EQ(node_names[0], "/test_component_foo");
EXPECT_EQ(unique_ids.size(), 1u);
EXPECT_EQ(node_names[1], "/test_component_bar");
EXPECT_EQ(node_names[2], "/test_component_baz");
EXPECT_EQ(node_names[3], "/ns/test_component_bing");
EXPECT_EQ(unique_ids.size(), 4u);
EXPECT_EQ(unique_ids[0], 1u);
EXPECT_EQ(unique_ids[1], 2u);
EXPECT_EQ(unique_ids[2], 3u);
EXPECT_EQ(unique_ids[3], 4u);
}
}
}

TEST_F(TestComponentManager, unload_component)
{
auto exec = std::make_shared<rclcpp::executors::SingleThreadedExecutor>();
auto node = rclcpp::Node::make_shared("test_component_manager");
auto manager = std::make_shared<rclcpp_components::ComponentManager>(exec);

exec->add_node(manager);
exec->add_node(node);

{
auto client = node->create_client<composition_interfaces::srv::LoadNode>(
"/ComponentManager/_container/load_node");

if (!client->wait_for_service(20s)) {
ASSERT_TRUE(false) << "service not available after waiting";
}

{
auto request = std::make_shared<composition_interfaces::srv::LoadNode::Request>();
request->package_name = "rclcpp_components";
request->plugin_name = "test_rclcpp_components::TestComponentFoo";

auto result = client->async_send_request(request);
auto ret = exec->spin_until_future_complete(result, 5s); // Wait for the result.
EXPECT_EQ(ret, rclcpp::executor::FutureReturnCode::SUCCESS);
EXPECT_EQ(result.get()->success, true);
EXPECT_EQ(result.get()->error_message, "");
EXPECT_EQ(result.get()->full_node_name, "/test_component_foo");
EXPECT_EQ(result.get()->unique_id, 1u);
}
}

auto node_names = node->get_node_names();
auto find_in_nodes = [node_names](std::string name) {
return std::find(node_names.begin(), node_names.end(), name) != node_names.end();
};
EXPECT_TRUE(find_in_nodes("/test_component_foo"));

{
auto client = node->create_client<composition_interfaces::srv::UnloadNode>(
Expand Down Expand Up @@ -295,4 +213,31 @@ TEST_F(TestComponentManager, unload_component)
EXPECT_EQ(result.get()->error_message, "No node found with unique_id: 1");
}
}

{
auto client = node->create_client<composition_interfaces::srv::ListNodes>(
"/ComponentManager/_container/list_nodes");

if (!client->wait_for_service(20s)) {
ASSERT_TRUE(false) << "service not available after waiting";
}

{
auto request = std::make_shared<composition_interfaces::srv::ListNodes::Request>();
auto result = client->async_send_request(request);
auto ret = exec->spin_until_future_complete(result, 5s); // Wait for the result.
EXPECT_EQ(ret, rclcpp::executor::FutureReturnCode::SUCCESS);
auto node_names = result.get()->full_node_names;
auto unique_ids = result.get()->unique_ids;

EXPECT_EQ(node_names.size(), 3u);
EXPECT_EQ(node_names[0], "/test_component_bar");
EXPECT_EQ(node_names[1], "/test_component_baz");
EXPECT_EQ(node_names[2], "/ns/test_component_bing");
EXPECT_EQ(unique_ids.size(), 3u);
EXPECT_EQ(unique_ids[0], 2u);
EXPECT_EQ(unique_ids[1], 3u);
EXPECT_EQ(unique_ids[2], 4u);
}
}
}

0 comments on commit 4afd1cd

Please sign in to comment.