Skip to content
Snippets Groups Projects
Unverified Commit 53c2d600 authored by BriceRenaudeau's avatar BriceRenaudeau Committed by GitHub
Browse files

ServiceClient wait_for_service not infinite (#2278)

* change wait_for_service behavior

* Add unit test
parent aeccbe7a
No related branches found
No related tags found
No related merge requests found
......@@ -123,19 +123,13 @@ public:
}
/**
* @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:
......
......@@ -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% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment