|
14 | 14 |
|
15 | 15 | #include <gtest/gtest.h> |
16 | 16 |
|
17 | | -#include <string> |
| 17 | +#include <chrono> |
18 | 18 | #include <memory> |
| 19 | +#include <string> |
19 | 20 | #include <utility> |
20 | 21 | #include <vector> |
21 | 22 |
|
|
28 | 29 | #include "../utils/rclcpp_gtest_macros.hpp" |
29 | 30 |
|
30 | 31 | #include "test_msgs/msg/empty.hpp" |
| 32 | +#include "test_msgs/msg/strings.hpp" |
31 | 33 |
|
32 | 34 | class TestPublisher : public ::testing::Test |
33 | 35 | { |
@@ -536,3 +538,78 @@ TEST_F(TestPublisher, get_network_flow_endpoints_errors) { |
536 | 538 | EXPECT_NO_THROW(publisher->get_network_flow_endpoints()); |
537 | 539 | } |
538 | 540 | } |
| 541 | + |
| 542 | +TEST_F(TestPublisher, check_wait_for_all_acked_return) { |
| 543 | + initialize(); |
| 544 | + const rclcpp::QoS publisher_qos(1); |
| 545 | + auto publisher = node->create_publisher<test_msgs::msg::Empty>("topic", publisher_qos); |
| 546 | + |
| 547 | + { |
| 548 | + // Using 'self' instead of 'lib:rclcpp' because `rcl_publisher_wait_for_all_acked` is entirely |
| 549 | + // defined in a header |
| 550 | + auto mock = mocking_utils::patch_and_return( |
| 551 | + "self", rcl_publisher_wait_for_all_acked, RCL_RET_OK); |
| 552 | + EXPECT_TRUE(publisher->wait_for_all_acked(std::chrono::milliseconds(-1))); |
| 553 | + } |
| 554 | + |
| 555 | + { |
| 556 | + // Using 'self' instead of 'lib:rclcpp' because `rcl_publisher_wait_for_all_acked` is entirely |
| 557 | + // defined in a header |
| 558 | + auto mock = mocking_utils::patch_and_return( |
| 559 | + "self", rcl_publisher_wait_for_all_acked, RCL_RET_TIMEOUT); |
| 560 | + EXPECT_FALSE(publisher->wait_for_all_acked(std::chrono::milliseconds(-1))); |
| 561 | + } |
| 562 | + |
| 563 | + { |
| 564 | + // Using 'self' instead of 'lib:rclcpp' because `rcl_publisher_wait_for_all_acked` is entirely |
| 565 | + // defined in a header |
| 566 | + auto mock = mocking_utils::patch_and_return( |
| 567 | + "self", rcl_publisher_wait_for_all_acked, RCL_RET_UNSUPPORTED); |
| 568 | + EXPECT_THROW( |
| 569 | + publisher->wait_for_all_acked(std::chrono::milliseconds(-1)), |
| 570 | + rclcpp::exceptions::RCLError); |
| 571 | + } |
| 572 | + |
| 573 | + { |
| 574 | + // Using 'self' instead of 'lib:rclcpp' because `rcl_publisher_wait_for_all_acked` is entirely |
| 575 | + // defined in a header |
| 576 | + auto mock = mocking_utils::patch_and_return( |
| 577 | + "self", rcl_publisher_wait_for_all_acked, RCL_RET_ERROR); |
| 578 | + EXPECT_THROW( |
| 579 | + publisher->wait_for_all_acked(std::chrono::milliseconds(-1)), |
| 580 | + rclcpp::exceptions::RCLError); |
| 581 | + } |
| 582 | +} |
| 583 | + |
| 584 | +class TestPublisherWaitForAllAcked |
| 585 | + : public TestPublisher, public ::testing::WithParamInterface<std::pair<rclcpp::QoS, rclcpp::QoS>> |
| 586 | +{ |
| 587 | +}; |
| 588 | + |
| 589 | +TEST_P(TestPublisherWaitForAllAcked, check_wait_for_all_acked_with_QosPolicy) { |
| 590 | + initialize(); |
| 591 | + |
| 592 | + auto do_nothing = [](std::shared_ptr<const test_msgs::msg::Strings>) {}; |
| 593 | + auto pub = node->create_publisher<test_msgs::msg::Strings>("topic", std::get<0>(GetParam())); |
| 594 | + auto sub = node->create_subscription<test_msgs::msg::Strings>( |
| 595 | + "topic", |
| 596 | + std::get<1>(GetParam()), |
| 597 | + do_nothing); |
| 598 | + |
| 599 | + auto msg = std::make_shared<test_msgs::msg::Strings>(); |
| 600 | + for (int i = 0; i < 20; i++) { |
| 601 | + ASSERT_NO_THROW(pub->publish(*msg)); |
| 602 | + } |
| 603 | + EXPECT_TRUE(pub->wait_for_all_acked(std::chrono::milliseconds(500))); |
| 604 | +} |
| 605 | + |
| 606 | +INSTANTIATE_TEST_SUITE_P( |
| 607 | + TestWaitForAllAckedWithParm, |
| 608 | + TestPublisherWaitForAllAcked, |
| 609 | + ::testing::Values( |
| 610 | + std::pair<rclcpp::QoS, rclcpp::QoS>( |
| 611 | + rclcpp::QoS(1).reliable(), rclcpp::QoS(1).reliable()), |
| 612 | + std::pair<rclcpp::QoS, rclcpp::QoS>( |
| 613 | + rclcpp::QoS(1).best_effort(), rclcpp::QoS(1).best_effort()), |
| 614 | + std::pair<rclcpp::QoS, rclcpp::QoS>( |
| 615 | + rclcpp::QoS(1).reliable(), rclcpp::QoS(1).best_effort()))); |
0 commit comments