Skip to content

Commit

Permalink
ServiceClient wait_for_service not infinite (#2278)
Browse files Browse the repository at this point in the history
* change wait_for_service behavior

* Add unit test
  • Loading branch information
BriceRenaudeau authored Mar 31, 2021
1 parent aeccbe7 commit 53c2d60
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
14 changes: 4 additions & 10 deletions nav2_util/include/nav2_util/service_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,13 @@ class ServiceClient
}

/**
* @brief Block until a service is available
* @brief Block until a service is available or timeout
* @param timeout Maximum timeout to wait for, default infinite
* @return bool true if service is available
*/
void wait_for_service(const std::chrono::nanoseconds timeout = std::chrono::nanoseconds::max())
bool wait_for_service(const std::chrono::nanoseconds timeout = std::chrono::nanoseconds::max())
{
auto sleep_dur = std::chrono::milliseconds(10);
while (!client_->wait_for_service(timeout)) {
if (!rclcpp::ok()) {
throw std::runtime_error(
service_name_ + " service client: interrupted while waiting for service");
}
rclcpp::sleep_for(sleep_dur);
}
return client_->wait_for_service(timeout);
}

protected:
Expand Down
11 changes: 11 additions & 0 deletions nav2_util/test/test_service_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,14 @@ TEST(ServiceClient, can_ServiceClient_invoke_in_callback)
pub_thread.join();
ASSERT_EQ(a, 1);
}

TEST(ServiceClient, can_ServiceClient_timeout)
{
rclcpp::init(0, nullptr);
auto node = rclcpp::Node::make_shared("test_node");
TestServiceClient t("bar", node);
rclcpp::spin_some(node);
bool ready = t.wait_for_service(std::chrono::milliseconds(10));
rclcpp::shutdown();
ASSERT_EQ(ready, false);
}

0 comments on commit 53c2d60

Please sign in to comment.