|
| 1 | +// Copyright 2023 Proyectos y Sistemas de Mantenimiento SL (eProsima). |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <limits> |
| 16 | +#include <memory> |
| 17 | +#include <thread> |
| 18 | + |
| 19 | +#include <asio.hpp> |
| 20 | +#include <gtest/gtest.h> |
| 21 | + |
| 22 | +#include <fastdds/dds/log/Log.hpp> |
| 23 | +#include <fastdds/rtps/transport/UDPv4TransportDescriptor.hpp> |
| 24 | +#include <fastdds/utils/IPFinder.hpp> |
| 25 | +#include <fastdds/utils/IPLocator.hpp> |
| 26 | + |
| 27 | +#include <utils/Semaphore.hpp> |
| 28 | + |
| 29 | +#include <MockReceiverResource.h> |
| 30 | +#include <rtps/transport/asio_helpers.hpp> |
| 31 | +#include <rtps/transport/UDPv4Transport.h> |
| 32 | + |
| 33 | +using namespace eprosima::fastdds::rtps; |
| 34 | + |
| 35 | + |
| 36 | +// Regression tests for redmine issue #22210 |
| 37 | + |
| 38 | +template <typename BufferOption, typename SocketType, typename Protocol> |
| 39 | +void test_buffer_setting( |
| 40 | + int initial_buffer_value, |
| 41 | + int minimum_buffer_value) |
| 42 | +{ |
| 43 | + asio::io_service io_service; |
| 44 | + auto socket = std::make_unique<SocketType>(io_service); |
| 45 | + |
| 46 | + // Open the socket with the provided protocol |
| 47 | + socket->open(Protocol::v4()); |
| 48 | + |
| 49 | + uint32_t final_buffer_value = 0; |
| 50 | + |
| 51 | + // Replace this with your actual implementation of try_setting_buffer_size |
| 52 | + ASSERT_TRUE(asio_helpers::try_setting_buffer_size<BufferOption>( |
| 53 | + *socket, initial_buffer_value, minimum_buffer_value, final_buffer_value)); |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + BufferOption option; |
| 58 | + asio::error_code ec; |
| 59 | + socket->get_option(option, ec); |
| 60 | + if (!ec) |
| 61 | + { |
| 62 | + ASSERT_EQ(static_cast<uint32_t>(option.value()), final_buffer_value); |
| 63 | + } |
| 64 | + else |
| 65 | + { |
| 66 | + throw std::runtime_error("Failed to get buffer option"); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// Test that the UDP buffer size is set actually to the value stored as the final value |
| 71 | +TEST(AsioHelpersTests, udp_buffer_size) |
| 72 | +{ |
| 73 | + uint32_t minimum_buffer_value = 0; |
| 74 | + for (uint32_t initial_buffer_value = std::numeric_limits<uint32_t>::max(); initial_buffer_value > 0; |
| 75 | + initial_buffer_value /= 4) |
| 76 | + { |
| 77 | + test_buffer_setting<asio::socket_base::send_buffer_size, asio::ip::udp::socket, asio::ip::udp>( |
| 78 | + initial_buffer_value, minimum_buffer_value); |
| 79 | + test_buffer_setting<asio::socket_base::receive_buffer_size, asio::ip::udp::socket, asio::ip::udp>( |
| 80 | + initial_buffer_value, minimum_buffer_value); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// Test that the TCP buffer size is set actually to the value stored as the final value |
| 85 | +TEST(AsioHelpersTests, tcp_buffer_size) |
| 86 | +{ |
| 87 | + uint32_t minimum_buffer_value = 0; |
| 88 | + for (uint32_t initial_buffer_value = std::numeric_limits<uint32_t>::max(); initial_buffer_value > 0; |
| 89 | + initial_buffer_value /= 4) |
| 90 | + { |
| 91 | + test_buffer_setting<asio::socket_base::send_buffer_size, asio::ip::tcp::socket, asio::ip::tcp>( |
| 92 | + initial_buffer_value, minimum_buffer_value); |
| 93 | + test_buffer_setting<asio::socket_base::receive_buffer_size, asio::ip::tcp::socket, asio::ip::tcp>( |
| 94 | + initial_buffer_value, minimum_buffer_value); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +int main( |
| 99 | + int argc, |
| 100 | + char** argv) |
| 101 | +{ |
| 102 | + testing::InitGoogleTest(&argc, argv); |
| 103 | + return RUN_ALL_TESTS(); |
| 104 | +} |
0 commit comments