Skip to content

Commit cf11ea0

Browse files
Janosch MachowinskiJanosch Machowinski
authored andcommitted
test: Added test if service works as expected with executor
Signed-off-by: Janosch Machowinski <J.Machowinski@cellumation.com>
1 parent 430c2dd commit cf11ea0

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

rclcpp/test/rclcpp/executors/test_executors.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,3 +1168,63 @@ TYPED_TEST(TestExecutors, dropSubscriptionDuringCallback)
11681168
// one was deleted during the callback
11691169
ASSERT_TRUE(!sub1_works || !sub2_works);
11701170
}
1171+
1172+
// Check if services work as expected
1173+
TYPED_TEST(TestExecutors, testService)
1174+
{
1175+
using ExecutorType = TypeParam;
1176+
1177+
ExecutorType executor;
1178+
executor.add_node(this->node);
1179+
1180+
bool spin_exited = false;
1181+
1182+
rclcpp::Node::SharedPtr node = this->node;
1183+
1184+
const std::string service_name("/test/test_service");
1185+
1186+
using Service = test_msgs::srv::Empty;
1187+
1188+
std::atomic<bool> gotCallback = false;
1189+
1190+
auto service_cb = [&gotCallback](const std::shared_ptr<Service::Request>/*request*/,
1191+
std::shared_ptr<Service::Response>/*response*/)
1192+
{
1193+
gotCallback = true;
1194+
};
1195+
1196+
auto service = node->create_service<Service>(service_name, service_cb, rclcpp::ServicesQoS());
1197+
1198+
// Long timeout
1199+
std::thread spinner([&spin_exited, &executor]() {
1200+
executor.spin();
1201+
spin_exited = true;
1202+
});
1203+
1204+
std::this_thread::sleep_for(1ms);
1205+
1206+
const std::shared_ptr<Service::Request> req = std::make_shared<Service::Request>();
1207+
1208+
auto client = node->create_client<Service>(service_name);
1209+
1210+
EXPECT_TRUE(client->wait_for_service(30ms));
1211+
1212+
auto handle = client->async_send_request(req);
1213+
1214+
auto retCode = handle.wait_for(500ms);
1215+
EXPECT_EQ(retCode, std::future_status::ready);
1216+
1217+
EXPECT_TRUE(gotCallback);
1218+
1219+
// Force interruption
1220+
rclcpp::shutdown();
1221+
1222+
// Give it time to exit
1223+
auto start = std::chrono::steady_clock::now();
1224+
while (!spin_exited && (std::chrono::steady_clock::now() - start) < 1s) {
1225+
std::this_thread::sleep_for(1ms);
1226+
}
1227+
1228+
EXPECT_TRUE(spin_exited);
1229+
spinner.join();
1230+
}

0 commit comments

Comments
 (0)